aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/expr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/expr.rs')
-rw-r--r--crates/ra_hir_ty/src/expr.rs158
1 files changed, 91 insertions, 67 deletions
diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs
index fb779cbef..e45e9ea14 100644
--- a/crates/ra_hir_ty/src/expr.rs
+++ b/crates/ra_hir_ty/src/expr.rs
@@ -2,12 +2,8 @@
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use hir_def::{ 5use hir_def::{path::path, resolver::HasResolver, AdtId, FunctionId};
6 path::{path, Path}, 6use hir_expand::diagnostics::DiagnosticSink;
7 resolver::HasResolver,
8 AdtId, FunctionId,
9};
10use hir_expand::{diagnostics::DiagnosticSink, name::Name};
11use ra_syntax::{ast, AstPtr}; 7use ra_syntax::{ast, AstPtr};
12use rustc_hash::FxHashSet; 8use rustc_hash::FxHashSet;
13 9
@@ -28,7 +24,7 @@ pub use hir_def::{
28 ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp, 24 ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp,
29 MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp, 25 MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp,
30 }, 26 },
31 VariantId, 27 LocalStructFieldId, VariantId,
32}; 28};
33 29
34pub struct ExprValidator<'a, 'b: 'a> { 30pub struct ExprValidator<'a, 'b: 'a> {
@@ -49,14 +45,37 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
49 pub fn validate_body(&mut self, db: &dyn HirDatabase) { 45 pub fn validate_body(&mut self, db: &dyn HirDatabase) {
50 let body = db.body(self.func.into()); 46 let body = db.body(self.func.into());
51 47
52 for e in body.exprs.iter() { 48 for (id, expr) in body.exprs.iter() {
53 if let (id, Expr::RecordLit { path, fields, spread }) = e { 49 if let Some((variant_def, missed_fields, true)) =
54 self.validate_record_literal(id, path, fields, *spread, db); 50 record_literal_missing_fields(db, &self.infer, id, expr)
55 } else if let (id, Expr::Match { expr, arms }) = e { 51 {
52 // XXX: only look at source_map if we do have missing fields
53 let (_, source_map) = db.body_with_source_map(self.func.into());
54
55 if let Ok(source_ptr) = source_map.expr_syntax(id) {
56 if let Some(expr) = source_ptr.value.left() {
57 let root = source_ptr.file_syntax(db.upcast());
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 }
75 if let Expr::Match { expr, arms } = expr {
56 self.validate_match(id, *expr, arms, db, self.infer.clone()); 76 self.validate_match(id, *expr, arms, db, self.infer.clone());
57 } 77 }
58 } 78 }
59
60 let body_expr = &body[body.body_expr]; 79 let body_expr = &body[body.body_expr];
61 if let Expr::Block { tail: Some(t), .. } = body_expr { 80 if let Expr::Block { tail: Some(t), .. } = body_expr {
62 self.validate_results_in_tail_expr(body.body_expr, *t, db); 81 self.validate_results_in_tail_expr(body.body_expr, *t, db);
@@ -145,61 +164,6 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
145 } 164 }
146 } 165 }
147 166
148 fn validate_record_literal(
149 &mut self,
150 id: ExprId,
151 _path: &Option<Path>,
152 fields: &[RecordLitField],
153 spread: Option<ExprId>,
154 db: &dyn HirDatabase,
155 ) {
156 if spread.is_some() {
157 return;
158 };
159 let variant_def: VariantId = match self.infer.variant_resolution_for_expr(id) {
160 Some(VariantId::UnionId(_)) | None => return,
161 Some(it) => it,
162 };
163 if let VariantId::UnionId(_) = variant_def {
164 return;
165 }
166
167 let variant_data = variant_data(db.upcast(), variant_def);
168
169 let lit_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect();
170 let missed_fields: Vec<Name> = variant_data
171 .fields()
172 .iter()
173 .filter_map(|(_f, d)| {
174 let name = d.name.clone();
175 if lit_fields.contains(&name) {
176 None
177 } else {
178 Some(name)
179 }
180 })
181 .collect();
182 if missed_fields.is_empty() {
183 return;
184 }
185 let (_, source_map) = db.body_with_source_map(self.func.into());
186
187 if let Ok(source_ptr) = source_map.expr_syntax(id) {
188 if let Some(expr) = source_ptr.value.left() {
189 let root = source_ptr.file_syntax(db.upcast());
190 if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) {
191 if let Some(field_list) = record_lit.record_field_list() {
192 self.sink.push(MissingFields {
193 file: source_ptr.file_id,
194 field_list: AstPtr::new(&field_list),
195 missed_fields,
196 })
197 }
198 }
199 }
200 }
201 }
202
203 fn validate_results_in_tail_expr(&mut self, body_id: ExprId, id: ExprId, db: &dyn HirDatabase) { 167 fn validate_results_in_tail_expr(&mut self, body_id: ExprId, id: ExprId, db: &dyn HirDatabase) {
204 // the mismatch will be on the whole block currently 168 // the mismatch will be on the whole block currently
205 let mismatch = match self.infer.type_mismatch_for_expr(body_id) { 169 let mismatch = match self.infer.type_mismatch_for_expr(body_id) {
@@ -232,3 +196,63 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
232 } 196 }
233 } 197 }
234} 198}
199
200pub fn record_literal_missing_fields(
201 db: &dyn HirDatabase,
202 infer: &InferenceResult,
203 id: ExprId,
204 expr: &Expr,
205) -> Option<(VariantId, Vec<LocalStructFieldId>, /*exhaustive*/ bool)> {
206 let (fields, exhausitve) = match expr {
207 Expr::RecordLit { path: _, fields, spread } => (fields, spread.is_none()),
208 _ => return None,
209 };
210
211 let variant_def = infer.variant_resolution_for_expr(id)?;
212 if let VariantId::UnionId(_) = variant_def {
213 return None;
214 }
215
216 let variant_data = variant_data(db.upcast(), variant_def);
217
218 let specified_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect();
219 let missed_fields: Vec<LocalStructFieldId> = variant_data
220 .fields()
221 .iter()
222 .filter_map(|(f, d)| if specified_fields.contains(&d.name) { None } else { Some(f) })
223 .collect();
224 if missed_fields.is_empty() {
225 return None;
226 }
227 Some((variant_def, missed_fields, exhausitve))
228}
229
230pub fn record_pattern_missing_fields(
231 db: &dyn HirDatabase,
232 infer: &InferenceResult,
233 id: PatId,
234 pat: &Pat,
235) -> Option<(VariantId, Vec<LocalStructFieldId>)> {
236 let fields = match pat {
237 Pat::Record { path: _, args } => args,
238 _ => return None,
239 };
240
241 let variant_def = infer.variant_resolution_for_pat(id)?;
242 if let VariantId::UnionId(_) = variant_def {
243 return None;
244 }
245
246 let variant_data = variant_data(db.upcast(), variant_def);
247
248 let specified_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect();
249 let missed_fields: Vec<LocalStructFieldId> = variant_data
250 .fields()
251 .iter()
252 .filter_map(|(f, d)| if specified_fields.contains(&d.name) { None } else { Some(f) })
253 .collect();
254 if missed_fields.is_empty() {
255 return None;
256 }
257 Some((variant_def, missed_fields))
258}