aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/expr.rs
diff options
context:
space:
mode:
authorJosh Mcguigan <[email protected]>2020-04-05 18:16:34 +0100
committerJosh Mcguigan <[email protected]>2020-04-07 13:12:08 +0100
commit43dfd894934cf7c9161e473495a4e24965239475 (patch)
tree323ddd9e91ea46713b1b5c30d40cd6474310c6e0 /crates/ra_hir_ty/src/expr.rs
parentb87b7a088f34ff794fc19e57ee2ae1cfe81a12df (diff)
handle non matching enum pattern types
Diffstat (limited to 'crates/ra_hir_ty/src/expr.rs')
-rw-r--r--crates/ra_hir_ty/src/expr.rs30
1 files changed, 22 insertions, 8 deletions
diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs
index 7498d04dc..8c7d1a98e 100644
--- a/crates/ra_hir_ty/src/expr.rs
+++ b/crates/ra_hir_ty/src/expr.rs
@@ -67,7 +67,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
67 fn validate_match( 67 fn validate_match(
68 &mut self, 68 &mut self,
69 id: ExprId, 69 id: ExprId,
70 expr: ExprId, 70 match_expr: ExprId,
71 arms: &[MatchArm], 71 arms: &[MatchArm],
72 db: &dyn HirDatabase, 72 db: &dyn HirDatabase,
73 infer: Arc<InferenceResult>, 73 infer: Arc<InferenceResult>,
@@ -75,18 +75,32 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
75 let (body, source_map): (Arc<Body>, Arc<BodySourceMap>) = 75 let (body, source_map): (Arc<Body>, Arc<BodySourceMap>) =
76 db.body_with_source_map(self.func.into()); 76 db.body_with_source_map(self.func.into());
77 77
78 let match_expr: &hir_def::expr::Expr = &body[expr]; 78 let match_expr_ty = match infer.type_of_expr.get(match_expr) {
79 Some(ty) => ty,
80 // If we can't resolve the type of the match expression
81 // we cannot perform exhaustiveness checks.
82 None => return,
83 };
79 84
80 let cx = MatchCheckCtx { body: body.clone(), match_expr, infer, db }; 85 let cx = MatchCheckCtx { body, infer: infer.clone(), db };
81 let pats = arms.iter().map(|arm| arm.pat); 86 let pats = arms.iter().map(|arm| arm.pat);
82 87
83 let mut seen = Matrix::empty(); 88 let mut seen = Matrix::empty();
84 for pat in pats { 89 for pat in pats {
85 // If we had a NotUsefulMatchArm diagnostic, we could 90 // We skip any patterns whose type we cannot resolve.
86 // check the usefulness of each pattern as we added it 91 if let Some(pat_ty) = infer.type_of_pat.get(pat) {
87 // to the matrix here. 92 // We only include patterns whose type matches the type
88 let v = PatStack::from_pattern(pat); 93 // of the match expression. If we had a InvalidMatchArmPattern
89 seen.push(&cx, v); 94 // diagnostic or similar we could raise that in an else
95 // block here.
96 if pat_ty == match_expr_ty {
97 // If we had a NotUsefulMatchArm diagnostic, we could
98 // check the usefulness of each pattern as we added it
99 // to the matrix here.
100 let v = PatStack::from_pattern(pat);
101 seen.push(&cx, v);
102 }
103 }
90 } 104 }
91 105
92 match is_useful(&cx, &seen, &PatStack::from_wild()) { 106 match is_useful(&cx, &seen, &PatStack::from_wild()) {