Auto merge of #14222 - Veykril:pat-mismatch-diags, r=Veykril

Show pattern mismatch diagnostics
This commit is contained in:
bors 2023-03-03 10:33:54 +00:00
commit c229a836e8
12 changed files with 354 additions and 242 deletions

View file

@ -178,8 +178,7 @@ pub struct MissingMatchArms {
#[derive(Debug)]
pub struct TypeMismatch {
// FIXME: add mismatches in patterns as well
pub expr: InFile<AstPtr<ast::Expr>>,
pub expr_or_pat: Either<InFile<AstPtr<ast::Expr>>, InFile<AstPtr<ast::Pat>>>,
pub expected: Type,
pub actual: Type,
}

View file

@ -1413,14 +1413,22 @@ impl DefWithBody {
}
}
}
for (expr, mismatch) in infer.expr_type_mismatches() {
let expr = match source_map.expr_syntax(expr) {
Ok(expr) => expr,
Err(SyntheticSyntax) => continue,
for (pat_or_expr, mismatch) in infer.type_mismatches() {
let expr_or_pat = match pat_or_expr {
ExprOrPatId::ExprId(expr) => source_map.expr_syntax(expr).map(Either::Left),
ExprOrPatId::PatId(pat) => source_map.pat_syntax(pat).map(Either::Right),
};
let expr_or_pat = match expr_or_pat {
Ok(Either::Left(expr)) => Either::Left(expr),
Ok(Either::Right(InFile { file_id, value: Either::Left(pat) })) => {
Either::Right(InFile { file_id, value: pat })
}
Ok(Either::Right(_)) | Err(SyntheticSyntax) => continue,
};
acc.push(
TypeMismatch {
expr,
expr_or_pat,
expected: Type::new(db, DefWithBodyId::from(self), mismatch.expected.clone()),
actual: Type::new(db, DefWithBodyId::from(self), mismatch.actual.clone()),
}