From efa069d28818dd074afd2c7cee776907b63ca012 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 14:41:19 +0300 Subject: internal: start new diagnostics API At the moment, this moves only a single diagnostic, but the idea is reafactor the rest to use the same pattern. We are going to have a single file per diagnostic. This file will define diagnostics code, rendering range and fixes, if any. It'll also have all of the tests. This is similar to how we deal with assists. After we refactor all diagnostics to follow this pattern, we'll probably move them to a new `ide_diagnostics` crate. Not that we intentionally want to test all diagnostics on this layer, despite the fact that they are generally emitted in the guts on the compiler. Diagnostics care to much about the end presentation details/fixes to be worth-while "unit" testing. So, we'll unit-test only the primary output of compilation process (types and name res tables), and will use integrated UI tests for diagnostics. --- crates/hir/src/diagnostics.rs | 39 +++++++++++++++++++-------------------- crates/hir/src/lib.rs | 32 ++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 34 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index 8a7c3a4fd..5e2f94698 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -15,31 +15,30 @@ pub use crate::diagnostics_sink::{ Diagnostic, DiagnosticCode, DiagnosticSink, DiagnosticSinkBuilder, }; -// Diagnostic: unresolved-module -// -// This diagnostic is triggered if rust-analyzer is unable to discover referred module. +macro_rules! diagnostics { + ($($diag:ident)*) => { + pub enum AnyDiagnostic {$( + $diag(Box<$diag>), + )*} + + $( + impl From<$diag> for AnyDiagnostic { + fn from(d: $diag) -> AnyDiagnostic { + AnyDiagnostic::$diag(Box::new(d)) + } + } + )* + }; +} + +diagnostics![UnresolvedModule]; + #[derive(Debug)] pub struct UnresolvedModule { - pub file: HirFileId, - pub decl: AstPtr, + pub decl: InFile>, pub candidate: String, } -impl Diagnostic for UnresolvedModule { - fn code(&self) -> DiagnosticCode { - DiagnosticCode("unresolved-module") - } - fn message(&self) -> String { - "unresolved module".to_string() - } - fn display_source(&self) -> InFile { - InFile::new(self.file, self.decl.clone().into()) - } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } -} - // Diagnostic: unresolved-extern-crate // // This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index bd923cba8..ff6c68dbc 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -80,18 +80,18 @@ use tt::{Ident, Leaf, Literal, TokenTree}; use crate::{ db::{DefDatabase, HirDatabase}, - diagnostics::{ - BreakOutsideOfLoop, InactiveCode, InternalBailedOut, MacroError, MismatchedArgCount, - MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr, MissingPatFields, - MissingUnsafe, NoSuchField, RemoveThisSemicolon, ReplaceFilterMapNextWithFindMap, - UnimplementedBuiltinMacro, UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall, - UnresolvedModule, UnresolvedProcMacro, - }, diagnostics_sink::DiagnosticSink, }; pub use crate::{ attrs::{HasAttrs, Namespace}, + diagnostics::{ + AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, InternalBailedOut, MacroError, + MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr, + MissingPatFields, MissingUnsafe, NoSuchField, RemoveThisSemicolon, + ReplaceFilterMapNextWithFindMap, UnimplementedBuiltinMacro, UnresolvedExternCrate, + UnresolvedImport, UnresolvedMacroCall, UnresolvedModule, UnresolvedProcMacro, + }, has_source::HasSource, semantics::{PathResolution, Semantics, SemanticsScope}, }; @@ -460,10 +460,11 @@ impl Module { db: &dyn HirDatabase, sink: &mut DiagnosticSink, internal_diagnostics: bool, - ) { + ) -> Vec { let _p = profile::span("Module::diagnostics").detail(|| { format!("{:?}", self.name(db).map_or("".into(), |name| name.to_string())) }); + let mut acc: Vec = Vec::new(); let def_map = self.id.def_map(db.upcast()); for diag in def_map.diagnostics() { if diag.in_module != self.id.local_id { @@ -473,11 +474,13 @@ impl Module { match &diag.kind { DefDiagnosticKind::UnresolvedModule { ast: declaration, candidate } => { let decl = declaration.to_node(db.upcast()); - sink.push(UnresolvedModule { - file: declaration.file_id, - decl: AstPtr::new(&decl), - candidate: candidate.clone(), - }) + acc.push( + UnresolvedModule { + decl: InFile::new(declaration.file_id, AstPtr::new(&decl)), + candidate: candidate.clone(), + } + .into(), + ) } DefDiagnosticKind::UnresolvedExternCrate { ast } => { let item = ast.to_node(db.upcast()); @@ -610,7 +613,7 @@ impl Module { crate::ModuleDef::Module(m) => { // Only add diagnostics from inline modules if def_map[m.id.local_id].origin.is_inline() { - m.diagnostics(db, sink, internal_diagnostics) + acc.extend(m.diagnostics(db, sink, internal_diagnostics)) } } _ => { @@ -626,6 +629,7 @@ impl Module { } } } + acc } pub fn declarations(self, db: &dyn HirDatabase) -> Vec { -- cgit v1.2.3