diff options
author | Aleksey Kladov <[email protected]> | 2019-11-02 20:42:38 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-02 20:42:38 +0000 |
commit | 13735d91a78ba51fb202cb7dde1dfe25420afe9a (patch) | |
tree | fba691ea156533fe490d4dfec04b262ae2f73f7d /crates/ra_hir_expand/src | |
parent | b8533413cf082f2d942b78af841e3895db252106 (diff) |
Move diagnostics to hir_expand
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r-- | crates/ra_hir_expand/src/diagnostics.rs | 85 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 1 |
2 files changed, 86 insertions, 0 deletions
diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs new file mode 100644 index 000000000..201884b95 --- /dev/null +++ b/crates/ra_hir_expand/src/diagnostics.rs | |||
@@ -0,0 +1,85 @@ | |||
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 sued 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 ra_syntax::{SyntaxNode, SyntaxNodePtr, TextRange}; | ||
20 | |||
21 | use crate::{db::AstDatabase, Source}; | ||
22 | |||
23 | pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { | ||
24 | fn message(&self) -> String; | ||
25 | fn source(&self) -> Source<SyntaxNodePtr>; | ||
26 | fn highlight_range(&self) -> TextRange { | ||
27 | self.source().ast.range() | ||
28 | } | ||
29 | fn as_any(&self) -> &(dyn Any + Send + 'static); | ||
30 | } | ||
31 | |||
32 | pub trait AstDiagnostic { | ||
33 | type AST; | ||
34 | fn ast(&self, db: &impl AstDatabase) -> Self::AST; | ||
35 | } | ||
36 | |||
37 | impl dyn Diagnostic { | ||
38 | pub fn syntax_node(&self, db: &impl AstDatabase) -> SyntaxNode { | ||
39 | let node = db.parse_or_expand(self.source().file_id).unwrap(); | ||
40 | self.source().ast.to_node(&node) | ||
41 | } | ||
42 | |||
43 | pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { | ||
44 | self.as_any().downcast_ref() | ||
45 | } | ||
46 | } | ||
47 | |||
48 | pub struct DiagnosticSink<'a> { | ||
49 | callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>, | ||
50 | default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>, | ||
51 | } | ||
52 | |||
53 | impl<'a> DiagnosticSink<'a> { | ||
54 | /// FIXME: split `new` and `on` into a separate builder type | ||
55 | pub fn new(cb: impl FnMut(&dyn Diagnostic) + 'a) -> DiagnosticSink<'a> { | ||
56 | DiagnosticSink { callbacks: Vec::new(), default_callback: Box::new(cb) } | ||
57 | } | ||
58 | |||
59 | pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> DiagnosticSink<'a> { | ||
60 | let cb = move |diag: &dyn Diagnostic| match diag.downcast_ref::<D>() { | ||
61 | Some(d) => { | ||
62 | cb(d); | ||
63 | Ok(()) | ||
64 | } | ||
65 | None => Err(()), | ||
66 | }; | ||
67 | self.callbacks.push(Box::new(cb)); | ||
68 | self | ||
69 | } | ||
70 | |||
71 | pub fn push(&mut self, d: impl Diagnostic) { | ||
72 | let d: &dyn Diagnostic = &d; | ||
73 | self._push(d); | ||
74 | } | ||
75 | |||
76 | fn _push(&mut self, d: &dyn Diagnostic) { | ||
77 | for cb in self.callbacks.iter_mut() { | ||
78 | match cb(d) { | ||
79 | Ok(()) => return, | ||
80 | Err(()) => (), | ||
81 | } | ||
82 | } | ||
83 | (self.default_callback)(d) | ||
84 | } | ||
85 | } | ||
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 85c2b22ac..dd07a16b4 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -9,6 +9,7 @@ pub mod ast_id_map; | |||
9 | pub mod either; | 9 | pub mod either; |
10 | pub mod name; | 10 | pub mod name; |
11 | pub mod hygiene; | 11 | pub mod hygiene; |
12 | pub mod diagnostics; | ||
12 | 13 | ||
13 | use std::hash::{Hash, Hasher}; | 14 | use std::hash::{Hash, Hasher}; |
14 | 15 | ||