aboutsummaryrefslogtreecommitdiff
path: root/crates/hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir')
-rw-r--r--crates/hir/src/diagnostics.rs95
-rw-r--r--crates/hir/src/lib.rs130
2 files changed, 72 insertions, 153 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs
index 5e2f94698..158626dc0 100644
--- a/crates/hir/src/diagnostics.rs
+++ b/crates/hir/src/diagnostics.rs
@@ -6,6 +6,7 @@
6use std::any::Any; 6use std::any::Any;
7 7
8use cfg::{CfgExpr, CfgOptions, DnfExpr}; 8use cfg::{CfgExpr, CfgOptions, DnfExpr};
9use either::Either;
9use hir_def::path::ModPath; 10use hir_def::path::ModPath;
10use hir_expand::{name::Name, HirFileId, InFile}; 11use hir_expand::{name::Name, HirFileId, InFile};
11use stdx::format_to; 12use stdx::format_to;
@@ -16,7 +17,7 @@ pub use crate::diagnostics_sink::{
16}; 17};
17 18
18macro_rules! diagnostics { 19macro_rules! diagnostics {
19 ($($diag:ident)*) => { 20 ($($diag:ident),*) => {
20 pub enum AnyDiagnostic {$( 21 pub enum AnyDiagnostic {$(
21 $diag(Box<$diag>), 22 $diag(Box<$diag>),
22 )*} 23 )*}
@@ -31,7 +32,7 @@ macro_rules! diagnostics {
31 }; 32 };
32} 33}
33 34
34diagnostics![UnresolvedModule]; 35diagnostics![UnresolvedModule, MissingFields];
35 36
36#[derive(Debug)] 37#[derive(Debug)]
37pub struct UnresolvedModule { 38pub struct UnresolvedModule {
@@ -321,102 +322,14 @@ impl Diagnostic for MissingUnsafe {
321 } 322 }
322} 323}
323 324
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)] 325#[derive(Debug)]
336pub struct MissingFields { 326pub struct MissingFields {
337 pub file: HirFileId, 327 pub file: HirFileId,
338 pub field_list_parent: AstPtr<ast::RecordExpr>, 328 pub field_list_parent: Either<AstPtr<ast::RecordExpr>, AstPtr<ast::RecordPat>>,
339 pub field_list_parent_path: Option<AstPtr<ast::Path>>,
340 pub missed_fields: Vec<Name>,
341}
342
343impl 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
372//
373// This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure.
374//
375// Example:
376//
377// ```rust
378// struct A { a: u8, b: u8 }
379//
380// let a = A { a: 10, b: 20 };
381//
382// if let A { a } = a {
383// // ...
384// }
385// ```
386#[derive(Debug)]
387pub struct MissingPatFields {
388 pub file: HirFileId,
389 pub field_list_parent: AstPtr<ast::RecordPat>,
390 pub field_list_parent_path: Option<AstPtr<ast::Path>>, 329 pub field_list_parent_path: Option<AstPtr<ast::Path>>,
391 pub missed_fields: Vec<Name>, 330 pub missed_fields: Vec<Name>,
392} 331}
393 332
394impl Diagnostic for MissingPatFields {
395 fn code(&self) -> DiagnosticCode {
396 DiagnosticCode("missing-pat-fields")
397 }
398 fn message(&self) -> String {
399 let mut buf = String::from("Missing structure fields:\n");
400 for field in &self.missed_fields {
401 format_to!(buf, "- {}\n", field);
402 }
403 buf
404 }
405 fn display_source(&self) -> InFile<SyntaxNodePtr> {
406 InFile {
407 file_id: self.file,
408 value: self
409 .field_list_parent_path
410 .clone()
411 .map(SyntaxNodePtr::from)
412 .unwrap_or_else(|| self.field_list_parent.clone().into()),
413 }
414 }
415 fn as_any(&self) -> &(dyn Any + Send + 'static) {
416 self
417 }
418}
419
420// Diagnostic: replace-filter-map-next-with-find-map 333// Diagnostic: replace-filter-map-next-with-find-map
421// 334//
422// This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`. 335// This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`.
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index ff6c68dbc..373134f62 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -88,9 +88,9 @@ pub use crate::{
88 diagnostics::{ 88 diagnostics::{
89 AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, InternalBailedOut, MacroError, 89 AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, InternalBailedOut, MacroError,
90 MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr, 90 MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr,
91 MissingPatFields, MissingUnsafe, NoSuchField, RemoveThisSemicolon, 91 MissingUnsafe, NoSuchField, RemoveThisSemicolon, ReplaceFilterMapNextWithFindMap,
92 ReplaceFilterMapNextWithFindMap, UnimplementedBuiltinMacro, UnresolvedExternCrate, 92 UnimplementedBuiltinMacro, UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall,
93 UnresolvedImport, UnresolvedMacroCall, UnresolvedModule, UnresolvedProcMacro, 93 UnresolvedModule, UnresolvedProcMacro,
94 }, 94 },
95 has_source::HasSource, 95 has_source::HasSource,
96 semantics::{PathResolution, Semantics, SemanticsScope}, 96 semantics::{PathResolution, Semantics, SemanticsScope},
@@ -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;
@@ -1099,64 +1098,70 @@ impl Function {
1099 BodyValidationDiagnostic::collect(db, self.id.into(), internal_diagnostics) 1098 BodyValidationDiagnostic::collect(db, self.id.into(), internal_diagnostics)
1100 { 1099 {
1101 match diagnostic { 1100 match diagnostic {
1102 BodyValidationDiagnostic::RecordLiteralMissingFields { 1101 BodyValidationDiagnostic::RecordMissingFields {
1103 record_expr, 1102 record,
1104 variant, 1103 variant,
1105 missed_fields, 1104 missed_fields,
1106 } => match source_map.expr_syntax(record_expr) { 1105 } => {
1107 Ok(source_ptr) => { 1106 let variant_data = variant.variant_data(db.upcast());
1108 let root = source_ptr.file_syntax(db.upcast()); 1107 let missed_fields = missed_fields
1109 if let ast::Expr::RecordExpr(record_expr) = &source_ptr.value.to_node(&root) 1108 .into_iter()
1110 { 1109 .map(|idx| variant_data.fields()[idx].name.clone())
1111 if let Some(_) = record_expr.record_expr_field_list() { 1110 .collect();
1112 let variant_data = variant.variant_data(db.upcast()); 1111
1113 let missed_fields = missed_fields 1112 match record {
1114 .into_iter() 1113 Either::Left(record_expr) => match source_map.expr_syntax(record_expr) {
1115 .map(|idx| variant_data.fields()[idx].name.clone()) 1114 Ok(source_ptr) => {
1116 .collect(); 1115 let root = source_ptr.file_syntax(db.upcast());
1117 sink.push(MissingFields { 1116 if let ast::Expr::RecordExpr(record_expr) =
1118 file: source_ptr.file_id, 1117 &source_ptr.value.to_node(&root)
1119 field_list_parent: AstPtr::new(record_expr), 1118 {
1120 field_list_parent_path: record_expr 1119 if let Some(_) = record_expr.record_expr_field_list() {
1121 .path() 1120 acc.push(
1122 .map(|path| AstPtr::new(&path)), 1121 MissingFields {
1123 missed_fields, 1122 file: source_ptr.file_id,
1124 }) 1123 field_list_parent: Either::Left(AstPtr::new(
1124 record_expr,
1125 )),
1126 field_list_parent_path: record_expr
1127 .path()
1128 .map(|path| AstPtr::new(&path)),
1129 missed_fields,
1130 }
1131 .into(),
1132 )
1133 }
1134 }
1125 } 1135 }
1126 } 1136 Err(SyntheticSyntax) => (),
1127 } 1137 },
1128 Err(SyntheticSyntax) => (), 1138 Either::Right(record_pat) => match source_map.pat_syntax(record_pat) {
1129 }, 1139 Ok(source_ptr) => {
1130 BodyValidationDiagnostic::RecordPatMissingFields { 1140 if let Some(expr) = source_ptr.value.as_ref().left() {
1131 record_pat, 1141 let root = source_ptr.file_syntax(db.upcast());
1132 variant, 1142 if let ast::Pat::RecordPat(record_pat) = expr.to_node(&root) {
1133 missed_fields, 1143 if let Some(_) = record_pat.record_pat_field_list() {
1134 } => match source_map.pat_syntax(record_pat) { 1144 acc.push(
1135 Ok(source_ptr) => { 1145 MissingFields {
1136 if let Some(expr) = source_ptr.value.as_ref().left() { 1146 file: source_ptr.file_id,
1137 let root = source_ptr.file_syntax(db.upcast()); 1147 field_list_parent: Either::Right(AstPtr::new(
1138 if let ast::Pat::RecordPat(record_pat) = expr.to_node(&root) { 1148 &record_pat,
1139 if let Some(_) = record_pat.record_pat_field_list() { 1149 )),
1140 let variant_data = variant.variant_data(db.upcast()); 1150 field_list_parent_path: record_pat
1141 let missed_fields = missed_fields 1151 .path()
1142 .into_iter() 1152 .map(|path| AstPtr::new(&path)),
1143 .map(|idx| variant_data.fields()[idx].name.clone()) 1153 missed_fields,
1144 .collect(); 1154 }
1145 sink.push(MissingPatFields { 1155 .into(),
1146 file: source_ptr.file_id, 1156 )
1147 field_list_parent: AstPtr::new(&record_pat), 1157 }
1148 field_list_parent_path: record_pat 1158 }
1149 .path()
1150 .map(|path| AstPtr::new(&path)),
1151 missed_fields,
1152 })
1153 } 1159 }
1154 } 1160 }
1155 } 1161 Err(SyntheticSyntax) => (),
1162 },
1156 } 1163 }
1157 Err(SyntheticSyntax) => (), 1164 }
1158 },
1159
1160 BodyValidationDiagnostic::ReplaceFilterMapNextWithFindMap { method_call_expr } => { 1165 BodyValidationDiagnostic::ReplaceFilterMapNextWithFindMap { method_call_expr } => {
1161 if let Ok(next_source_ptr) = source_map.expr_syntax(method_call_expr) { 1166 if let Ok(next_source_ptr) = source_map.expr_syntax(method_call_expr) {
1162 sink.push(ReplaceFilterMapNextWithFindMap { 1167 sink.push(ReplaceFilterMapNextWithFindMap {
@@ -1234,6 +1239,7 @@ impl Function {
1234 for diag in hir_ty::diagnostics::validate_module_item(db, krate, self.id.into()) { 1239 for diag in hir_ty::diagnostics::validate_module_item(db, krate, self.id.into()) {
1235 sink.push(diag) 1240 sink.push(diag)
1236 } 1241 }
1242 acc
1237 } 1243 }
1238 1244
1239 /// Whether this function declaration has a definition. 1245 /// Whether this function declaration has a definition.