diff options
Diffstat (limited to 'crates/ra_hir_ty/src/expr.rs')
-rw-r--r-- | crates/ra_hir_ty/src/expr.rs | 114 |
1 files changed, 86 insertions, 28 deletions
diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs index e45e9ea14..827b687de 100644 --- a/crates/ra_hir_ty/src/expr.rs +++ b/crates/ra_hir_ty/src/expr.rs | |||
@@ -9,7 +9,7 @@ use rustc_hash::FxHashSet; | |||
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | db::HirDatabase, | 11 | db::HirDatabase, |
12 | diagnostics::{MissingFields, MissingMatchArms, MissingOkInTailExpr}, | 12 | diagnostics::{MissingFields, MissingMatchArms, MissingOkInTailExpr, MissingPatFields}, |
13 | utils::variant_data, | 13 | utils::variant_data, |
14 | ApplicationTy, InferenceResult, Ty, TypeCtor, | 14 | ApplicationTy, InferenceResult, Ty, TypeCtor, |
15 | _match::{is_useful, MatchCheckCtx, Matrix, PatStack, Usefulness}, | 15 | _match::{is_useful, MatchCheckCtx, Matrix, PatStack, Usefulness}, |
@@ -49,39 +49,97 @@ impl<'a, 'b> ExprValidator<'a, 'b> { | |||
49 | if let Some((variant_def, missed_fields, true)) = | 49 | if let Some((variant_def, missed_fields, true)) = |
50 | record_literal_missing_fields(db, &self.infer, id, expr) | 50 | record_literal_missing_fields(db, &self.infer, id, expr) |
51 | { | 51 | { |
52 | // XXX: only look at source_map if we do have missing fields | 52 | self.create_record_literal_missing_fields_diagnostic( |
53 | let (_, source_map) = db.body_with_source_map(self.func.into()); | 53 | id, |
54 | 54 | db, | |
55 | if let Ok(source_ptr) = source_map.expr_syntax(id) { | 55 | variant_def, |
56 | if let Some(expr) = source_ptr.value.left() { | 56 | missed_fields, |
57 | let root = source_ptr.file_syntax(db.upcast()); | 57 | ); |
58 | if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) { | ||
59 | if let Some(field_list) = record_lit.record_field_list() { | ||
60 | let variant_data = variant_data(db.upcast(), variant_def); | ||
61 | let missed_fields = missed_fields | ||
62 | .into_iter() | ||
63 | .map(|idx| variant_data.fields()[idx].name.clone()) | ||
64 | .collect(); | ||
65 | self.sink.push(MissingFields { | ||
66 | file: source_ptr.file_id, | ||
67 | field_list: AstPtr::new(&field_list), | ||
68 | missed_fields, | ||
69 | }) | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | } | 58 | } |
75 | if let Expr::Match { expr, arms } = expr { | 59 | if let Expr::Match { expr, arms } = expr { |
76 | self.validate_match(id, *expr, arms, db, self.infer.clone()); | 60 | self.validate_match(id, *expr, arms, db, self.infer.clone()); |
77 | } | 61 | } |
78 | } | 62 | } |
63 | for (id, pat) in body.pats.iter() { | ||
64 | if let Some((variant_def, missed_fields, true)) = | ||
65 | record_pattern_missing_fields(db, &self.infer, id, pat) | ||
66 | { | ||
67 | self.create_record_pattern_missing_fields_diagnostic( | ||
68 | id, | ||
69 | db, | ||
70 | variant_def, | ||
71 | missed_fields, | ||
72 | ); | ||
73 | } | ||
74 | } | ||
79 | let body_expr = &body[body.body_expr]; | 75 | let body_expr = &body[body.body_expr]; |
80 | if let Expr::Block { tail: Some(t), .. } = body_expr { | 76 | if let Expr::Block { tail: Some(t), .. } = body_expr { |
81 | self.validate_results_in_tail_expr(body.body_expr, *t, db); | 77 | self.validate_results_in_tail_expr(body.body_expr, *t, db); |
82 | } | 78 | } |
83 | } | 79 | } |
84 | 80 | ||
81 | fn create_record_literal_missing_fields_diagnostic( | ||
82 | &mut self, | ||
83 | id: ExprId, | ||
84 | db: &dyn HirDatabase, | ||
85 | variant_def: VariantId, | ||
86 | missed_fields: Vec<LocalStructFieldId>, | ||
87 | ) { | ||
88 | // XXX: only look at source_map if we do have missing fields | ||
89 | let (_, source_map) = db.body_with_source_map(self.func.into()); | ||
90 | |||
91 | if let Ok(source_ptr) = source_map.expr_syntax(id) { | ||
92 | if let Some(expr) = source_ptr.value.as_ref().left() { | ||
93 | let root = source_ptr.file_syntax(db.upcast()); | ||
94 | if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) { | ||
95 | if let Some(field_list) = record_lit.record_field_list() { | ||
96 | let variant_data = variant_data(db.upcast(), variant_def); | ||
97 | let missed_fields = missed_fields | ||
98 | .into_iter() | ||
99 | .map(|idx| variant_data.fields()[idx].name.clone()) | ||
100 | .collect(); | ||
101 | self.sink.push(MissingFields { | ||
102 | file: source_ptr.file_id, | ||
103 | field_list: AstPtr::new(&field_list), | ||
104 | missed_fields, | ||
105 | }) | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | |||
112 | fn create_record_pattern_missing_fields_diagnostic( | ||
113 | &mut self, | ||
114 | id: PatId, | ||
115 | db: &dyn HirDatabase, | ||
116 | variant_def: VariantId, | ||
117 | missed_fields: Vec<LocalStructFieldId>, | ||
118 | ) { | ||
119 | // XXX: only look at source_map if we do have missing fields | ||
120 | let (_, source_map) = db.body_with_source_map(self.func.into()); | ||
121 | |||
122 | if let Ok(source_ptr) = source_map.pat_syntax(id) { | ||
123 | if let Some(expr) = source_ptr.value.as_ref().left() { | ||
124 | let root = source_ptr.file_syntax(db.upcast()); | ||
125 | if let ast::Pat::RecordPat(record_pat) = expr.to_node(&root) { | ||
126 | if let Some(field_list) = record_pat.record_field_pat_list() { | ||
127 | let variant_data = variant_data(db.upcast(), variant_def); | ||
128 | let missed_fields = missed_fields | ||
129 | .into_iter() | ||
130 | .map(|idx| variant_data.fields()[idx].name.clone()) | ||
131 | .collect(); | ||
132 | self.sink.push(MissingPatFields { | ||
133 | file: source_ptr.file_id, | ||
134 | field_list: AstPtr::new(&field_list), | ||
135 | missed_fields, | ||
136 | }) | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | } | ||
142 | |||
85 | fn validate_match( | 143 | fn validate_match( |
86 | &mut self, | 144 | &mut self, |
87 | id: ExprId, | 145 | id: ExprId, |
@@ -147,7 +205,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { | |||
147 | } | 205 | } |
148 | 206 | ||
149 | if let Ok(source_ptr) = source_map.expr_syntax(id) { | 207 | if let Ok(source_ptr) = source_map.expr_syntax(id) { |
150 | if let Some(expr) = source_ptr.value.left() { | 208 | if let Some(expr) = source_ptr.value.as_ref().left() { |
151 | let root = source_ptr.file_syntax(db.upcast()); | 209 | let root = source_ptr.file_syntax(db.upcast()); |
152 | if let ast::Expr::MatchExpr(match_expr) = expr.to_node(&root) { | 210 | if let ast::Expr::MatchExpr(match_expr) = expr.to_node(&root) { |
153 | if let (Some(match_expr), Some(arms)) = | 211 | if let (Some(match_expr), Some(arms)) = |
@@ -232,9 +290,9 @@ pub fn record_pattern_missing_fields( | |||
232 | infer: &InferenceResult, | 290 | infer: &InferenceResult, |
233 | id: PatId, | 291 | id: PatId, |
234 | pat: &Pat, | 292 | pat: &Pat, |
235 | ) -> Option<(VariantId, Vec<LocalStructFieldId>)> { | 293 | ) -> Option<(VariantId, Vec<LocalStructFieldId>, /*exhaustive*/ bool)> { |
236 | let fields = match pat { | 294 | let (fields, exhaustive) = match pat { |
237 | Pat::Record { path: _, args } => args, | 295 | Pat::Record { path: _, args, ellipsis } => (args, !ellipsis), |
238 | _ => return None, | 296 | _ => return None, |
239 | }; | 297 | }; |
240 | 298 | ||
@@ -254,5 +312,5 @@ pub fn record_pattern_missing_fields( | |||
254 | if missed_fields.is_empty() { | 312 | if missed_fields.is_empty() { |
255 | return None; | 313 | return None; |
256 | } | 314 | } |
257 | Some((variant_def, missed_fields)) | 315 | Some((variant_def, missed_fields, exhaustive)) |
258 | } | 316 | } |