diff options
Diffstat (limited to 'crates/ra_hir/src/diagnostics.rs')
-rw-r--r-- | crates/ra_hir/src/diagnostics.rs | 91 |
1 files changed, 1 insertions, 90 deletions
diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs index 6db499e06..a9040ea3d 100644 --- a/crates/ra_hir/src/diagnostics.rs +++ b/crates/ra_hir/src/diagnostics.rs | |||
@@ -1,93 +1,4 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | |||
3 | use std::any::Any; | ||
4 | |||
5 | use hir_expand::HirFileId; | ||
6 | use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr}; | ||
7 | |||
8 | use crate::{db::AstDatabase, Name, Source}; | ||
9 | |||
10 | pub use hir_def::diagnostics::UnresolvedModule; | 2 | pub use hir_def::diagnostics::UnresolvedModule; |
11 | pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; | 3 | pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; |
12 | 4 | pub use hir_ty::diagnostics::{MissingFields, MissingOkInTailExpr, NoSuchField}; | |
13 | #[derive(Debug)] | ||
14 | pub struct NoSuchField { | ||
15 | pub file: HirFileId, | ||
16 | pub field: AstPtr<ast::RecordField>, | ||
17 | } | ||
18 | |||
19 | impl Diagnostic for NoSuchField { | ||
20 | fn message(&self) -> String { | ||
21 | "no such field".to_string() | ||
22 | } | ||
23 | |||
24 | fn source(&self) -> Source<SyntaxNodePtr> { | ||
25 | Source { file_id: self.file, value: self.field.into() } | ||
26 | } | ||
27 | |||
28 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
29 | self | ||
30 | } | ||
31 | } | ||
32 | |||
33 | #[derive(Debug)] | ||
34 | pub struct MissingFields { | ||
35 | pub file: HirFileId, | ||
36 | pub field_list: AstPtr<ast::RecordFieldList>, | ||
37 | pub missed_fields: Vec<Name>, | ||
38 | } | ||
39 | |||
40 | impl Diagnostic for MissingFields { | ||
41 | fn message(&self) -> String { | ||
42 | use std::fmt::Write; | ||
43 | let mut message = String::from("Missing structure fields:\n"); | ||
44 | for field in &self.missed_fields { | ||
45 | write!(message, "- {}\n", field).unwrap(); | ||
46 | } | ||
47 | message | ||
48 | } | ||
49 | fn source(&self) -> Source<SyntaxNodePtr> { | ||
50 | Source { file_id: self.file, value: self.field_list.into() } | ||
51 | } | ||
52 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
53 | self | ||
54 | } | ||
55 | } | ||
56 | |||
57 | impl AstDiagnostic for MissingFields { | ||
58 | type AST = ast::RecordFieldList; | ||
59 | |||
60 | fn ast(&self, db: &impl AstDatabase) -> Self::AST { | ||
61 | let root = db.parse_or_expand(self.source().file_id).unwrap(); | ||
62 | let node = self.source().value.to_node(&root); | ||
63 | ast::RecordFieldList::cast(node).unwrap() | ||
64 | } | ||
65 | } | ||
66 | |||
67 | #[derive(Debug)] | ||
68 | pub struct MissingOkInTailExpr { | ||
69 | pub file: HirFileId, | ||
70 | pub expr: AstPtr<ast::Expr>, | ||
71 | } | ||
72 | |||
73 | impl Diagnostic for MissingOkInTailExpr { | ||
74 | fn message(&self) -> String { | ||
75 | "wrap return expression in Ok".to_string() | ||
76 | } | ||
77 | fn source(&self) -> Source<SyntaxNodePtr> { | ||
78 | Source { file_id: self.file, value: self.expr.into() } | ||
79 | } | ||
80 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
81 | self | ||
82 | } | ||
83 | } | ||
84 | |||
85 | impl AstDiagnostic for MissingOkInTailExpr { | ||
86 | type AST = ast::Expr; | ||
87 | |||
88 | fn ast(&self, db: &impl AstDatabase) -> Self::AST { | ||
89 | let root = db.parse_or_expand(self.file).unwrap(); | ||
90 | let node = self.source().value.to_node(&root); | ||
91 | ast::Expr::cast(node).unwrap() | ||
92 | } | ||
93 | } | ||