aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/diagnostics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/diagnostics.rs')
-rw-r--r--crates/ra_hir/src/diagnostics.rs62
1 files changed, 58 insertions, 4 deletions
diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs
index 82aff9cee..46a3fdd47 100644
--- a/crates/ra_hir/src/diagnostics.rs
+++ b/crates/ra_hir/src/diagnostics.rs
@@ -1,6 +1,60 @@
1use crate::{expr::ExprId}; 1use std::{fmt, any::Any};
2 2
3#[derive(Clone, Debug, PartialEq, Eq)] 3use ra_syntax::{SyntaxNodePtr, AstPtr, ast};
4pub enum FunctionDiagnostic { 4
5 NoSuchField { expr: ExprId, field: usize }, 5use crate::HirFileId;
6
7pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
8 fn file(&self) -> HirFileId;
9 fn syntax_node(&self) -> SyntaxNodePtr;
10 fn message(&self) -> String;
11 fn as_any(&self) -> &(Any + Send + 'static);
12}
13
14impl dyn Diagnostic {
15 pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
16 self.as_any().downcast_ref()
17 }
18}
19
20#[derive(Debug, Default)]
21pub struct Diagnostics {
22 data: Vec<Box<dyn Diagnostic>>,
23}
24
25impl Diagnostics {
26 pub fn push(&mut self, d: impl Diagnostic) {
27 self.data.push(Box::new(d))
28 }
29
30 pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a dyn Diagnostic> + 'a {
31 self.data.iter().map(|it| it.as_ref())
32 }
33}
34
35#[derive(Debug)]
36pub struct NoSuchField {
37 pub(crate) file: HirFileId,
38 pub(crate) field: AstPtr<ast::NamedField>,
39}
40
41impl NoSuchField {
42 pub fn field(&self) -> AstPtr<ast::NamedField> {
43 self.field
44 }
45}
46
47impl Diagnostic for NoSuchField {
48 fn file(&self) -> HirFileId {
49 self.file
50 }
51 fn syntax_node(&self) -> SyntaxNodePtr {
52 self.field.into()
53 }
54 fn message(&self) -> String {
55 "no such field".to_string()
56 }
57 fn as_any(&self) -> &(Any + Send + 'static) {
58 self
59 }
6} 60}