aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src/diagnostics.rs
diff options
context:
space:
mode:
authorIgor Aleksanov <[email protected]>2020-08-14 05:34:07 +0100
committerIgor Aleksanov <[email protected]>2020-08-14 05:34:07 +0100
commitc26c911ec1e6c2ad1dcb7d155a6a1d528839ad1a (patch)
tree7cff36c38234be0afb65273146d8247083a5cfeb /crates/hir_expand/src/diagnostics.rs
parent3c018bf84de5c693b5ee1c6bec0fed3b201c2060 (diff)
parentf1f73649a686dc6e6449afc35e0fa6fed00e225d (diff)
Merge branch 'master' into add-disable-diagnostics
Diffstat (limited to 'crates/hir_expand/src/diagnostics.rs')
-rw-r--r--crates/hir_expand/src/diagnostics.rs96
1 files changed, 96 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..6c81b2501
--- /dev/null
+++ b/crates/hir_expand/src/diagnostics.rs
@@ -0,0 +1,96 @@
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
17use std::{any::Any, fmt};
18
19use syntax::SyntaxNodePtr;
20
21use crate::InFile;
22
23pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
24 fn name(&self) -> &'static str;
25 fn message(&self) -> String;
26 /// Used in highlighting and related purposes
27 fn display_source(&self) -> InFile<SyntaxNodePtr>;
28 fn as_any(&self) -> &(dyn Any + Send + 'static);
29 fn is_experimental(&self) -> bool {
30 false
31 }
32}
33
34pub struct DiagnosticSink<'a> {
35 callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
36 filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>,
37 default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>,
38}
39
40impl<'a> DiagnosticSink<'a> {
41 pub fn push(&mut self, d: impl Diagnostic) {
42 let d: &dyn Diagnostic = &d;
43 self._push(d);
44 }
45
46 fn _push(&mut self, d: &dyn Diagnostic) {
47 for filter in &mut self.filters {
48 if !filter(d) {
49 return;
50 }
51 }
52 for cb in &mut self.callbacks {
53 match cb(d) {
54 Ok(()) => return,
55 Err(()) => (),
56 }
57 }
58 (self.default_callback)(d)
59 }
60}
61
62pub struct DiagnosticSinkBuilder<'a> {
63 callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
64 filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>,
65}
66
67impl<'a> DiagnosticSinkBuilder<'a> {
68 pub fn new() -> Self {
69 Self { callbacks: Vec::new(), filters: Vec::new() }
70 }
71
72 pub fn filter<F: FnMut(&dyn Diagnostic) -> bool + 'a>(mut self, cb: F) -> Self {
73 self.filters.push(Box::new(cb));
74 self
75 }
76
77 pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> Self {
78 let cb = move |diag: &dyn Diagnostic| match diag.as_any().downcast_ref::<D>() {
79 Some(d) => {
80 cb(d);
81 Ok(())
82 }
83 None => Err(()),
84 };
85 self.callbacks.push(Box::new(cb));
86 self
87 }
88
89 pub fn build<F: FnMut(&dyn Diagnostic) + 'a>(self, default_callback: F) -> DiagnosticSink<'a> {
90 DiagnosticSink {
91 callbacks: self.callbacks,
92 filters: self.filters,
93 default_callback: Box::new(default_callback),
94 }
95 }
96}