diff options
Diffstat (limited to 'crates/hir_expand/src/diagnostics.rs')
-rw-r--r-- | crates/hir_expand/src/diagnostics.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/crates/hir_expand/src/diagnostics.rs b/crates/hir_expand/src/diagnostics.rs new file mode 100644 index 000000000..59d35debe --- /dev/null +++ b/crates/hir_expand/src/diagnostics.rs | |||
@@ -0,0 +1,95 @@ | |||
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 syntax::SyntaxNodePtr; | ||
20 | |||
21 | use crate::InFile; | ||
22 | |||
23 | pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { | ||
24 | fn message(&self) -> String; | ||
25 | /// Used in highlighting and related purposes | ||
26 | fn display_source(&self) -> InFile<SyntaxNodePtr>; | ||
27 | fn as_any(&self) -> &(dyn Any + Send + 'static); | ||
28 | fn is_experimental(&self) -> bool { | ||
29 | false | ||
30 | } | ||
31 | } | ||
32 | |||
33 | pub struct DiagnosticSink<'a> { | ||
34 | callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>, | ||
35 | filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>, | ||
36 | default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>, | ||
37 | } | ||
38 | |||
39 | impl<'a> DiagnosticSink<'a> { | ||
40 | pub fn push(&mut self, d: impl Diagnostic) { | ||
41 | let d: &dyn Diagnostic = &d; | ||
42 | self._push(d); | ||
43 | } | ||
44 | |||
45 | fn _push(&mut self, d: &dyn Diagnostic) { | ||
46 | for filter in &mut self.filters { | ||
47 | if !filter(d) { | ||
48 | return; | ||
49 | } | ||
50 | } | ||
51 | for cb in &mut self.callbacks { | ||
52 | match cb(d) { | ||
53 | Ok(()) => return, | ||
54 | Err(()) => (), | ||
55 | } | ||
56 | } | ||
57 | (self.default_callback)(d) | ||
58 | } | ||
59 | } | ||
60 | |||
61 | pub struct DiagnosticSinkBuilder<'a> { | ||
62 | callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>, | ||
63 | filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>, | ||
64 | } | ||
65 | |||
66 | impl<'a> DiagnosticSinkBuilder<'a> { | ||
67 | pub fn new() -> Self { | ||
68 | Self { callbacks: Vec::new(), filters: Vec::new() } | ||
69 | } | ||
70 | |||
71 | pub fn filter<F: FnMut(&dyn Diagnostic) -> bool + 'a>(mut self, cb: F) -> Self { | ||
72 | self.filters.push(Box::new(cb)); | ||
73 | self | ||
74 | } | ||
75 | |||
76 | pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> Self { | ||
77 | let cb = move |diag: &dyn Diagnostic| match diag.as_any().downcast_ref::<D>() { | ||
78 | Some(d) => { | ||
79 | cb(d); | ||
80 | Ok(()) | ||
81 | } | ||
82 | None => Err(()), | ||
83 | }; | ||
84 | self.callbacks.push(Box::new(cb)); | ||
85 | self | ||
86 | } | ||
87 | |||
88 | pub fn build<F: FnMut(&dyn Diagnostic) + 'a>(self, default_callback: F) -> DiagnosticSink<'a> { | ||
89 | DiagnosticSink { | ||
90 | callbacks: self.callbacks, | ||
91 | filters: self.filters, | ||
92 | default_callback: Box::new(default_callback), | ||
93 | } | ||
94 | } | ||
95 | } | ||