]> git.lizzy.rs Git - rust.git/blob - crates/hir/src/diagnostics.rs
Merge #9735
[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     MacroError,
36     MismatchedArgCount,
37     MissingFields,
38     MissingMatchArms,
39     MissingOkOrSomeInTailExpr,
40     MissingUnsafe,
41     NoSuchField,
42     RemoveThisSemicolon,
43     ReplaceFilterMapNextWithFindMap,
44     UnimplementedBuiltinMacro,
45     UnresolvedExternCrate,
46     UnresolvedImport,
47     UnresolvedMacroCall,
48     UnresolvedModule,
49     UnresolvedProcMacro,
50 ];
51
52 #[derive(Debug)]
53 pub struct UnresolvedModule {
54     pub decl: InFile<AstPtr<ast::Module>>,
55     pub candidate: String,
56 }
57
58 #[derive(Debug)]
59 pub struct UnresolvedExternCrate {
60     pub decl: InFile<AstPtr<ast::ExternCrate>>,
61 }
62
63 #[derive(Debug)]
64 pub struct UnresolvedImport {
65     pub decl: InFile<AstPtr<ast::UseTree>>,
66 }
67
68 #[derive(Debug, Clone, Eq, PartialEq)]
69 pub struct UnresolvedMacroCall {
70     pub macro_call: InFile<AstPtr<ast::MacroCall>>,
71     pub path: ModPath,
72 }
73
74 #[derive(Debug, Clone, Eq, PartialEq)]
75 pub struct InactiveCode {
76     pub node: InFile<SyntaxNodePtr>,
77     pub cfg: CfgExpr,
78     pub opts: CfgOptions,
79 }
80
81 #[derive(Debug, Clone, Eq, PartialEq)]
82 pub struct UnresolvedProcMacro {
83     pub node: InFile<SyntaxNodePtr>,
84     /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange`
85     /// to use instead.
86     pub precise_location: Option<TextRange>,
87     pub macro_name: Option<String>,
88 }
89
90 #[derive(Debug, Clone, Eq, PartialEq)]
91 pub struct MacroError {
92     pub node: InFile<SyntaxNodePtr>,
93     pub message: String,
94 }
95
96 #[derive(Debug)]
97 pub struct UnimplementedBuiltinMacro {
98     pub node: InFile<SyntaxNodePtr>,
99 }
100
101 #[derive(Debug)]
102 pub struct NoSuchField {
103     pub field: InFile<AstPtr<ast::RecordExprField>>,
104 }
105
106 #[derive(Debug)]
107 pub struct BreakOutsideOfLoop {
108     pub expr: InFile<AstPtr<ast::Expr>>,
109 }
110
111 #[derive(Debug)]
112 pub struct MissingUnsafe {
113     pub expr: InFile<AstPtr<ast::Expr>>,
114 }
115
116 #[derive(Debug)]
117 pub struct MissingFields {
118     pub file: HirFileId,
119     pub field_list_parent: Either<AstPtr<ast::RecordExpr>, AstPtr<ast::RecordPat>>,
120     pub field_list_parent_path: Option<AstPtr<ast::Path>>,
121     pub missed_fields: Vec<Name>,
122 }
123
124 #[derive(Debug)]
125 pub struct ReplaceFilterMapNextWithFindMap {
126     pub file: HirFileId,
127     /// This expression is the whole method chain up to and including `.filter_map(..).next()`.
128     pub next_expr: AstPtr<ast::Expr>,
129 }
130
131 #[derive(Debug)]
132 pub struct MismatchedArgCount {
133     pub call_expr: InFile<AstPtr<ast::Expr>>,
134     pub expected: usize,
135     pub found: usize,
136 }
137
138 #[derive(Debug)]
139 pub struct RemoveThisSemicolon {
140     pub expr: InFile<AstPtr<ast::Expr>>,
141 }
142
143 #[derive(Debug)]
144 pub struct MissingOkOrSomeInTailExpr {
145     pub expr: InFile<AstPtr<ast::Expr>>,
146     // `Some` or `Ok` depending on whether the return type is Result or Option
147     pub required: String,
148     pub expected: Type,
149 }
150
151 #[derive(Debug)]
152 pub struct MissingMatchArms {
153     pub file: HirFileId,
154     pub match_expr: AstPtr<ast::Expr>,
155     pub arms: AstPtr<ast::MatchArmList>,
156 }
157
158 #[derive(Debug)]
159 pub struct AddReferenceHere {
160     pub expr: InFile<AstPtr<ast::Expr>>,
161     pub mutability: Mutability,
162 }
163
164 pub use hir_ty::diagnostics::IncorrectCase;