diff options
author | Aleksey Kladov <[email protected]> | 2021-05-23 21:31:59 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-05-25 15:49:59 +0100 |
commit | 5c9f31d4c28478b4373e6cf5ec155745c840ee3f (patch) | |
tree | 6d105121d271c7532170875feafaadcd7ad500ba /crates/hir_expand/src | |
parent | b7414fa14a85f4acd37b5bdfdc2a4ab97a072bd2 (diff) |
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.
Diffstat (limited to 'crates/hir_expand/src')
-rw-r--r-- | crates/hir_expand/src/db.rs | 2 | ||||
-rw-r--r-- | crates/hir_expand/src/diagnostics.rs | 110 | ||||
-rw-r--r-- | crates/hir_expand/src/lib.rs | 7 |
3 files changed, 4 insertions, 115 deletions
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index 625c26f0a..e8f4af309 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs | |||
@@ -186,7 +186,7 @@ fn parse_macro_expansion( | |||
186 | // The final goal we would like to make all parse_macro success, | 186 | // The final goal we would like to make all parse_macro success, |
187 | // such that the following log will not call anyway. | 187 | // such that the following log will not call anyway. |
188 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); | 188 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); |
189 | let node = loc.kind.node(db); | 189 | let node = loc.kind.to_node(db); |
190 | 190 | ||
191 | // collect parent information for warning log | 191 | // collect parent information for warning log |
192 | let parents = | 192 | let parents = |
diff --git a/crates/hir_expand/src/diagnostics.rs b/crates/hir_expand/src/diagnostics.rs deleted file mode 100644 index bf0b85ce9..000000000 --- a/crates/hir_expand/src/diagnostics.rs +++ /dev/null | |||
@@ -1,110 +0,0 @@ | |||
1 | //! Semantic errors and warnings. | ||
2 | //! | ||
3 | //! The `Diagnostic` trait defines a trait object which can represent any | ||
4 | //! diagnostic. | ||
5 | //! | ||
6 | //! `DiagnosticSink` struct is used as an emitter for diagnostic. When creating | ||
7 | //! a `DiagnosticSink`, you supply a callback which can react to a `dyn | ||
8 | //! Diagnostic` or to any concrete diagnostic (downcasting is used internally). | ||
9 | //! | ||
10 | //! Because diagnostics store file offsets, it's a bad idea to store them | ||
11 | //! directly in salsa. For this reason, every hir subsytem defines it's own | ||
12 | //! strongly-typed closed set of diagnostics which use hir ids internally, are | ||
13 | //! stored in salsa and do *not* implement the `Diagnostic` trait. Instead, a | ||
14 | //! subsystem provides a separate, non-query-based API which can walk all stored | ||
15 | //! values and transform them into instances of `Diagnostic`. | ||
16 | |||
17 | use std::{any::Any, fmt}; | ||
18 | |||
19 | use syntax::SyntaxNodePtr; | ||
20 | |||
21 | use crate::InFile; | ||
22 | |||
23 | #[derive(Copy, Clone, Debug, PartialEq)] | ||
24 | pub struct DiagnosticCode(pub &'static str); | ||
25 | |||
26 | impl DiagnosticCode { | ||
27 | pub fn as_str(&self) -> &str { | ||
28 | self.0 | ||
29 | } | ||
30 | } | ||
31 | |||
32 | pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { | ||
33 | fn code(&self) -> DiagnosticCode; | ||
34 | fn message(&self) -> String; | ||
35 | /// Source element that triggered the diagnostics. | ||
36 | /// | ||
37 | /// Note that this should reflect "semantics", rather than specific span we | ||
38 | /// want to highlight. When rendering the diagnostics into an error message, | ||
39 | /// the IDE will fetch the `SyntaxNode` and will narrow the span | ||
40 | /// appropriately. | ||
41 | fn display_source(&self) -> InFile<SyntaxNodePtr>; | ||
42 | fn as_any(&self) -> &(dyn Any + Send + 'static); | ||
43 | fn is_experimental(&self) -> bool { | ||
44 | false | ||
45 | } | ||
46 | } | ||
47 | |||
48 | pub struct DiagnosticSink<'a> { | ||
49 | callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>, | ||
50 | filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>, | ||
51 | default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>, | ||
52 | } | ||
53 | |||
54 | impl<'a> DiagnosticSink<'a> { | ||
55 | pub fn push(&mut self, d: impl Diagnostic) { | ||
56 | let d: &dyn Diagnostic = &d; | ||
57 | self._push(d); | ||
58 | } | ||
59 | |||
60 | fn _push(&mut self, d: &dyn Diagnostic) { | ||
61 | for filter in &mut self.filters { | ||
62 | if !filter(d) { | ||
63 | return; | ||
64 | } | ||
65 | } | ||
66 | for cb in &mut self.callbacks { | ||
67 | match cb(d) { | ||
68 | Ok(()) => return, | ||
69 | Err(()) => (), | ||
70 | } | ||
71 | } | ||
72 | (self.default_callback)(d) | ||
73 | } | ||
74 | } | ||
75 | |||
76 | pub struct DiagnosticSinkBuilder<'a> { | ||
77 | callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>, | ||
78 | filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>, | ||
79 | } | ||
80 | |||
81 | impl<'a> DiagnosticSinkBuilder<'a> { | ||
82 | pub fn new() -> Self { | ||
83 | Self { callbacks: Vec::new(), filters: Vec::new() } | ||
84 | } | ||
85 | |||
86 | pub fn filter<F: FnMut(&dyn Diagnostic) -> bool + 'a>(mut self, cb: F) -> Self { | ||
87 | self.filters.push(Box::new(cb)); | ||
88 | self | ||
89 | } | ||
90 | |||
91 | pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> Self { | ||
92 | let cb = move |diag: &dyn Diagnostic| match diag.as_any().downcast_ref::<D>() { | ||
93 | Some(d) => { | ||
94 | cb(d); | ||
95 | Ok(()) | ||
96 | } | ||
97 | None => Err(()), | ||
98 | }; | ||
99 | self.callbacks.push(Box::new(cb)); | ||
100 | self | ||
101 | } | ||
102 | |||
103 | pub fn build<F: FnMut(&dyn Diagnostic) + 'a>(self, default_callback: F) -> DiagnosticSink<'a> { | ||
104 | DiagnosticSink { | ||
105 | callbacks: self.callbacks, | ||
106 | filters: self.filters, | ||
107 | default_callback: Box::new(default_callback), | ||
108 | } | ||
109 | } | ||
110 | } | ||
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 6be4516a3..10d37234e 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs | |||
@@ -8,7 +8,6 @@ pub mod db; | |||
8 | pub mod ast_id_map; | 8 | pub mod ast_id_map; |
9 | pub mod name; | 9 | pub mod name; |
10 | pub mod hygiene; | 10 | pub mod hygiene; |
11 | pub mod diagnostics; | ||
12 | pub mod builtin_derive; | 11 | pub mod builtin_derive; |
13 | pub mod builtin_macro; | 12 | pub mod builtin_macro; |
14 | pub mod proc_macro; | 13 | pub mod proc_macro; |
@@ -108,7 +107,7 @@ impl HirFileId { | |||
108 | HirFileIdRepr::FileId(_) => None, | 107 | HirFileIdRepr::FileId(_) => None, |
109 | HirFileIdRepr::MacroFile(macro_file) => { | 108 | HirFileIdRepr::MacroFile(macro_file) => { |
110 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); | 109 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); |
111 | Some(loc.kind.node(db)) | 110 | Some(loc.kind.to_node(db)) |
112 | } | 111 | } |
113 | } | 112 | } |
114 | } | 113 | } |
@@ -153,7 +152,7 @@ impl HirFileId { | |||
153 | HirFileIdRepr::MacroFile(macro_file) => { | 152 | HirFileIdRepr::MacroFile(macro_file) => { |
154 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); | 153 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); |
155 | let item = match loc.def.kind { | 154 | let item = match loc.def.kind { |
156 | MacroDefKind::BuiltInDerive(..) => loc.kind.node(db), | 155 | MacroDefKind::BuiltInDerive(..) => loc.kind.to_node(db), |
157 | _ => return None, | 156 | _ => return None, |
158 | }; | 157 | }; |
159 | Some(item.with_value(ast::Item::cast(item.value.clone())?)) | 158 | Some(item.with_value(ast::Item::cast(item.value.clone())?)) |
@@ -269,7 +268,7 @@ impl MacroCallKind { | |||
269 | } | 268 | } |
270 | } | 269 | } |
271 | 270 | ||
272 | fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { | 271 | pub fn to_node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { |
273 | match self { | 272 | match self { |
274 | MacroCallKind::FnLike { ast_id, .. } => { | 273 | MacroCallKind::FnLike { ast_id, .. } => { |
275 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) | 274 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) |