From 5c9f31d4c28478b4373e6cf5ec155745c840ee3f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 23 May 2021 23:31:59 +0300 Subject: internal: move diagnostics to hir The idea here is to eventually get rid of `dyn Diagnostic` and `DiagnosticSink` infrastructure altogether, and just have a `enum hir::Diagnostic` instead. The problem with `dyn Diagnostic` is that it is defined in the lowest level of the stack (hir_expand), but is used by the highest level (ide). As a first step, we free hir_expand and hir_def from `dyn Diagnostic` and kick the can up to `hir_ty`, as an intermediate state. The plan is then to move DiagnosticSink similarly to the hir crate, and, as final third step, remove its usage from the ide. One currently unsolved problem is testing. You can notice that the test which checks precise diagnostic ranges, unresolved_import_in_use_tree, was moved to the ide layer. Logically, only IDE should have the infra to render a specific range. At the same time, the range is determined with the data produced in hir_def and hir crates, so this layering is rather unfortunate. Working on hir_def shouldn't require compiling `ide` for testing. --- crates/hir_def/src/body/lower.rs | 45 ++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 27 deletions(-) (limited to 'crates/hir_def/src/body/lower.rs') diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 2a7e0205f..da1fdac33 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -8,7 +8,7 @@ use hir_expand::{ ast_id_map::{AstIdMap, FileAstId}, hygiene::Hygiene, name::{name, AsName, Name}, - ExpandError, HirFileId, + ExpandError, HirFileId, InFile, }; use la_arena::Arena; use profile::Count; @@ -23,9 +23,9 @@ use syntax::{ use crate::{ adt::StructKind, body::{Body, BodySourceMap, Expander, LabelSource, PatPtr, SyntheticSyntax}, + body::{BodyDiagnostic, ExprSource, PatSource}, builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, db::DefDatabase, - diagnostics::{InactiveCode, MacroError, UnresolvedMacroCall, UnresolvedProcMacro}, expr::{ dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Label, LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, @@ -38,8 +38,6 @@ use crate::{ AdtId, BlockLoc, ModuleDefId, UnresolvedMacro, }; -use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; - pub struct LowerCtx<'a> { pub db: &'a dyn DefDatabase, hygiene: Hygiene, @@ -592,13 +590,10 @@ impl ExprCollector<'_> { let res = match res { Ok(res) => res, Err(UnresolvedMacro { path }) => { - self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall( - UnresolvedMacroCall { - file: outer_file, - node: syntax_ptr.cast().unwrap(), - path, - }, - )); + self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall { + node: InFile::new(outer_file, syntax_ptr), + path, + }); collector(self, None); return; } @@ -606,21 +601,15 @@ impl ExprCollector<'_> { match &res.err { Some(ExpandError::UnresolvedProcMacro) => { - self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro( - UnresolvedProcMacro { - file: outer_file, - node: syntax_ptr.into(), - precise_location: None, - macro_name: None, - }, - )); + self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro { + node: InFile::new(outer_file, syntax_ptr), + }); } Some(err) => { - self.source_map.diagnostics.push(BodyDiagnostic::MacroError(MacroError { - file: outer_file, - node: syntax_ptr.into(), + self.source_map.diagnostics.push(BodyDiagnostic::MacroError { + node: InFile::new(outer_file, syntax_ptr), message: err.to_string(), - })); + }); } None => {} } @@ -945,12 +934,14 @@ impl ExprCollector<'_> { return Some(()); } - self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode(InactiveCode { - file: self.expander.current_file_id, - node: SyntaxNodePtr::new(owner.syntax()), + self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode { + node: InFile::new( + self.expander.current_file_id, + SyntaxNodePtr::new(owner.syntax()), + ), cfg, opts: self.expander.cfg_options().clone(), - })); + }); None } -- cgit v1.2.3