diff options
Diffstat (limited to 'crates/hir/src')
-rw-r--r-- | crates/hir/src/diagnostics.rs | 43 | ||||
-rw-r--r-- | crates/hir/src/lib.rs | 33 |
2 files changed, 20 insertions, 56 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 5e2f94698..a5e982b75 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs | |||
@@ -16,7 +16,7 @@ pub use crate::diagnostics_sink::{ | |||
16 | }; | 16 | }; |
17 | 17 | ||
18 | macro_rules! diagnostics { | 18 | macro_rules! diagnostics { |
19 | ($($diag:ident)*) => { | 19 | ($($diag:ident),*) => { |
20 | pub enum AnyDiagnostic {$( | 20 | pub enum AnyDiagnostic {$( |
21 | $diag(Box<$diag>), | 21 | $diag(Box<$diag>), |
22 | )*} | 22 | )*} |
@@ -31,7 +31,7 @@ macro_rules! diagnostics { | |||
31 | }; | 31 | }; |
32 | } | 32 | } |
33 | 33 | ||
34 | diagnostics![UnresolvedModule]; | 34 | diagnostics![UnresolvedModule, MissingFields]; |
35 | 35 | ||
36 | #[derive(Debug)] | 36 | #[derive(Debug)] |
37 | pub struct UnresolvedModule { | 37 | pub struct UnresolvedModule { |
@@ -321,17 +321,6 @@ impl Diagnostic for MissingUnsafe { | |||
321 | } | 321 | } |
322 | } | 322 | } |
323 | 323 | ||
324 | // Diagnostic: missing-structure-fields | ||
325 | // | ||
326 | // This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. | ||
327 | // | ||
328 | // Example: | ||
329 | // | ||
330 | // ```rust | ||
331 | // struct A { a: u8, b: u8 } | ||
332 | // | ||
333 | // let a = A { a: 10 }; | ||
334 | // ``` | ||
335 | #[derive(Debug)] | 324 | #[derive(Debug)] |
336 | pub struct MissingFields { | 325 | pub struct MissingFields { |
337 | pub file: HirFileId, | 326 | pub file: HirFileId, |
@@ -340,34 +329,6 @@ pub struct MissingFields { | |||
340 | pub missed_fields: Vec<Name>, | 329 | pub missed_fields: Vec<Name>, |
341 | } | 330 | } |
342 | 331 | ||
343 | impl Diagnostic for MissingFields { | ||
344 | fn code(&self) -> DiagnosticCode { | ||
345 | DiagnosticCode("missing-structure-fields") | ||
346 | } | ||
347 | fn message(&self) -> String { | ||
348 | let mut buf = String::from("Missing structure fields:\n"); | ||
349 | for field in &self.missed_fields { | ||
350 | format_to!(buf, "- {}\n", field); | ||
351 | } | ||
352 | buf | ||
353 | } | ||
354 | |||
355 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
356 | InFile { | ||
357 | file_id: self.file, | ||
358 | value: self | ||
359 | .field_list_parent_path | ||
360 | .clone() | ||
361 | .map(SyntaxNodePtr::from) | ||
362 | .unwrap_or_else(|| self.field_list_parent.clone().into()), | ||
363 | } | ||
364 | } | ||
365 | |||
366 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
367 | self | ||
368 | } | ||
369 | } | ||
370 | |||
371 | // Diagnostic: missing-pat-fields | 332 | // Diagnostic: missing-pat-fields |
372 | // | 333 | // |
373 | // This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure. | 334 | // This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure. |
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index ff6c68dbc..583d92f20 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -609,23 +609,21 @@ impl Module { | |||
609 | } | 609 | } |
610 | for decl in self.declarations(db) { | 610 | for decl in self.declarations(db) { |
611 | match decl { | 611 | match decl { |
612 | crate::ModuleDef::Function(f) => f.diagnostics(db, sink, internal_diagnostics), | 612 | ModuleDef::Function(f) => acc.extend(f.diagnostics(db, sink, internal_diagnostics)), |
613 | crate::ModuleDef::Module(m) => { | 613 | ModuleDef::Module(m) => { |
614 | // Only add diagnostics from inline modules | 614 | // Only add diagnostics from inline modules |
615 | if def_map[m.id.local_id].origin.is_inline() { | 615 | if def_map[m.id.local_id].origin.is_inline() { |
616 | acc.extend(m.diagnostics(db, sink, internal_diagnostics)) | 616 | acc.extend(m.diagnostics(db, sink, internal_diagnostics)) |
617 | } | 617 | } |
618 | } | 618 | } |
619 | _ => { | 619 | _ => decl.diagnostics(db, sink), |
620 | decl.diagnostics(db, sink); | ||
621 | } | ||
622 | } | 620 | } |
623 | } | 621 | } |
624 | 622 | ||
625 | for impl_def in self.impl_defs(db) { | 623 | for impl_def in self.impl_defs(db) { |
626 | for item in impl_def.items(db) { | 624 | for item in impl_def.items(db) { |
627 | if let AssocItem::Function(f) = item { | 625 | if let AssocItem::Function(f) = item { |
628 | f.diagnostics(db, sink, internal_diagnostics); | 626 | acc.extend(f.diagnostics(db, sink, internal_diagnostics)); |
629 | } | 627 | } |
630 | } | 628 | } |
631 | } | 629 | } |
@@ -1033,7 +1031,8 @@ impl Function { | |||
1033 | db: &dyn HirDatabase, | 1031 | db: &dyn HirDatabase, |
1034 | sink: &mut DiagnosticSink, | 1032 | sink: &mut DiagnosticSink, |
1035 | internal_diagnostics: bool, | 1033 | internal_diagnostics: bool, |
1036 | ) { | 1034 | ) -> Vec<AnyDiagnostic> { |
1035 | let mut acc: Vec<AnyDiagnostic> = Vec::new(); | ||
1037 | let krate = self.module(db).id.krate(); | 1036 | let krate = self.module(db).id.krate(); |
1038 | 1037 | ||
1039 | let source_map = db.body_with_source_map(self.id.into()).1; | 1038 | let source_map = db.body_with_source_map(self.id.into()).1; |
@@ -1114,14 +1113,17 @@ impl Function { | |||
1114 | .into_iter() | 1113 | .into_iter() |
1115 | .map(|idx| variant_data.fields()[idx].name.clone()) | 1114 | .map(|idx| variant_data.fields()[idx].name.clone()) |
1116 | .collect(); | 1115 | .collect(); |
1117 | sink.push(MissingFields { | 1116 | acc.push( |
1118 | file: source_ptr.file_id, | 1117 | MissingFields { |
1119 | field_list_parent: AstPtr::new(record_expr), | 1118 | file: source_ptr.file_id, |
1120 | field_list_parent_path: record_expr | 1119 | field_list_parent: AstPtr::new(record_expr), |
1121 | .path() | 1120 | field_list_parent_path: record_expr |
1122 | .map(|path| AstPtr::new(&path)), | 1121 | .path() |
1123 | missed_fields, | 1122 | .map(|path| AstPtr::new(&path)), |
1124 | }) | 1123 | missed_fields, |
1124 | } | ||
1125 | .into(), | ||
1126 | ) | ||
1125 | } | 1127 | } |
1126 | } | 1128 | } |
1127 | } | 1129 | } |
@@ -1234,6 +1236,7 @@ impl Function { | |||
1234 | for diag in hir_ty::diagnostics::validate_module_item(db, krate, self.id.into()) { | 1236 | for diag in hir_ty::diagnostics::validate_module_item(db, krate, self.id.into()) { |
1235 | sink.push(diag) | 1237 | sink.push(diag) |
1236 | } | 1238 | } |
1239 | acc | ||
1237 | } | 1240 | } |
1238 | 1241 | ||
1239 | /// Whether this function declaration has a definition. | 1242 | /// Whether this function declaration has a definition. |