]> git.lizzy.rs Git - rust.git/blob - crates/hir/src/diagnostics.rs
Merge #10902
[rust.git] / crates / hir / src / diagnostics.rs
1 //! Re-export diagnostics such that clients of `hir` don't have to depend on
2 //! low-level crates.
3 //!
4 //! This probably isn't the best way to do this -- ideally, diagnistics should
5 //! be expressed in terms of hir types themselves.
6 use cfg::{CfgExpr, CfgOptions};
7 use either::Either;
8 use hir_def::{path::ModPath, type_ref::Mutability};
9 use hir_expand::{name::Name, HirFileId, InFile};
10 use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
11
12 use crate::Type;
13
14 macro_rules! diagnostics {
15     ($($diag:ident,)*) => {
16         pub enum AnyDiagnostic {$(
17             $diag(Box<$diag>),
18         )*}
19
20         $(
21             impl From<$diag> for AnyDiagnostic {
22                 fn from(d: $diag) -> AnyDiagnostic {
23                     AnyDiagnostic::$diag(Box::new(d))
24                 }
25             }
26         )*
27     };
28 }
29
30 diagnostics![
31     AddReferenceHere,
32     BreakOutsideOfLoop,
33     InactiveCode,
34     IncorrectCase,
35     InvalidDeriveTarget,
36     MacroError,
37     MalformedDerive,
38     MismatchedArgCount,
39     MissingFields,
40     MissingMatchArms,
41     MissingOkOrSomeInTailExpr,
42     MissingUnsafe,
43     NoSuchField,
44     RemoveThisSemicolon,
45     ReplaceFilterMapNextWithFindMap,
46     UnimplementedBuiltinMacro,
47     UnresolvedExternCrate,
48     UnresolvedImport,
49     UnresolvedMacroCall,
50     UnresolvedModule,
51     UnresolvedProcMacro,
52 ];
53
54 #[derive(Debug)]
55 pub struct UnresolvedModule {
56     pub decl: InFile<AstPtr<ast::Module>>,
57     pub candidate: String,
58 }
59
60 #[derive(Debug)]
61 pub struct UnresolvedExternCrate {
62     pub decl: InFile<AstPtr<ast::ExternCrate>>,
63 }
64
65 #[derive(Debug)]
66 pub struct UnresolvedImport {
67     pub decl: InFile<AstPtr<ast::UseTree>>,
68 }
69
70 #[derive(Debug, Clone, Eq, PartialEq)]
71 pub struct UnresolvedMacroCall {
72     pub macro_call: InFile<AstPtr<ast::MacroCall>>,
73     pub path: ModPath,
74 }
75
76 #[derive(Debug, Clone, Eq, PartialEq)]
77 pub struct InactiveCode {
78     pub node: InFile<SyntaxNodePtr>,
79     pub cfg: CfgExpr,
80     pub opts: CfgOptions,
81 }
82
83 #[derive(Debug, Clone, Eq, PartialEq)]
84 pub struct UnresolvedProcMacro {
85     pub node: InFile<SyntaxNodePtr>,
86     /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange`
87     /// to use instead.
88     pub precise_location: Option<TextRange>,
89     pub macro_name: Option<String>,
90 }
91
92 #[derive(Debug, Clone, Eq, PartialEq)]
93 pub struct MacroError {
94     pub node: InFile<SyntaxNodePtr>,
95     pub message: String,
96 }
97
98 #[derive(Debug)]
99 pub struct UnimplementedBuiltinMacro {
100     pub node: InFile<SyntaxNodePtr>,
101 }
102
103 #[derive(Debug)]
104 pub struct InvalidDeriveTarget {
105     pub node: InFile<SyntaxNodePtr>,
106 }
107
108 #[derive(Debug)]
109 pub struct MalformedDerive {
110     pub node: InFile<SyntaxNodePtr>,
111 }
112
113 #[derive(Debug)]
114 pub struct NoSuchField {
115     pub field: InFile<AstPtr<ast::RecordExprField>>,
116 }
117
118 #[derive(Debug)]
119 pub struct BreakOutsideOfLoop {
120     pub expr: InFile<AstPtr<ast::Expr>>,
121 }
122
123 #[derive(Debug)]
124 pub struct MissingUnsafe {
125     pub expr: InFile<AstPtr<ast::Expr>>,
126 }
127
128 #[derive(Debug)]
129 pub struct MissingFields {
130     pub file: HirFileId,
131     pub field_list_parent: Either<AstPtr<ast::RecordExpr>, AstPtr<ast::RecordPat>>,
132     pub field_list_parent_path: Option<AstPtr<ast::Path>>,
133     pub missed_fields: Vec<Name>,
134 }
135
136 #[derive(Debug)]
137 pub struct ReplaceFilterMapNextWithFindMap {
138     pub file: HirFileId,
139     /// This expression is the whole method chain up to and including `.filter_map(..).next()`.
140     pub next_expr: AstPtr<ast::Expr>,
141 }
142
143 #[derive(Debug)]
144 pub struct MismatchedArgCount {
145     pub call_expr: InFile<AstPtr<ast::Expr>>,
146     pub expected: usize,
147     pub found: usize,
148 }
149
150 #[derive(Debug)]
151 pub struct RemoveThisSemicolon {
152     pub expr: InFile<AstPtr<ast::Expr>>,
153 }
154
155 #[derive(Debug)]
156 pub struct MissingOkOrSomeInTailExpr {
157     pub expr: InFile<AstPtr<ast::Expr>>,
158     // `Some` or `Ok` depending on whether the return type is Result or Option
159     pub required: String,
160     pub expected: Type,
161 }
162
163 #[derive(Debug)]
164 pub struct MissingMatchArms {
165     pub file: HirFileId,
166     pub match_expr: AstPtr<ast::Expr>,
167     pub arms: AstPtr<ast::MatchArmList>,
168 }
169
170 #[derive(Debug)]
171 pub struct AddReferenceHere {
172     pub expr: InFile<AstPtr<ast::Expr>>,
173     pub mutability: Mutability,
174 }
175
176 pub use hir_ty::diagnostics::IncorrectCase;