]> git.lizzy.rs Git - rust.git/blob - crates/hir/src/diagnostics.rs
Rollup merge of #100643 - TaKO8Ki:point-at-type-parameter-shadowing-another-type...
[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 base_db::CrateId;
7 use cfg::{CfgExpr, CfgOptions};
8 use either::Either;
9 use hir_def::path::ModPath;
10 use hir_expand::{name::Name, HirFileId, InFile};
11 use syntax::{ast, AstPtr, SyntaxNodePtr, TextRange};
12
13 use crate::{MacroKind, Type};
14
15 macro_rules! diagnostics {
16     ($($diag:ident,)*) => {
17         #[derive(Debug)]
18         pub enum AnyDiagnostic {$(
19             $diag(Box<$diag>),
20         )*}
21
22         $(
23             impl From<$diag> for AnyDiagnostic {
24                 fn from(d: $diag) -> AnyDiagnostic {
25                     AnyDiagnostic::$diag(Box::new(d))
26                 }
27             }
28         )*
29     };
30 }
31
32 diagnostics![
33     BreakOutsideOfLoop,
34     InactiveCode,
35     IncorrectCase,
36     InvalidDeriveTarget,
37     MacroError,
38     MalformedDerive,
39     MismatchedArgCount,
40     MissingFields,
41     MissingMatchArms,
42     MissingUnsafe,
43     NoSuchField,
44     ReplaceFilterMapNextWithFindMap,
45     TypeMismatch,
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 candidates: Box<[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<SyntaxNodePtr>,
73     pub precise_location: Option<TextRange>,
74     pub path: ModPath,
75     pub is_bang: bool,
76 }
77
78 #[derive(Debug, Clone, Eq, PartialEq)]
79 pub struct InactiveCode {
80     pub node: InFile<SyntaxNodePtr>,
81     pub cfg: CfgExpr,
82     pub opts: CfgOptions,
83 }
84
85 #[derive(Debug, Clone, Eq, PartialEq)]
86 pub struct UnresolvedProcMacro {
87     pub node: InFile<SyntaxNodePtr>,
88     /// If the diagnostic can be pinpointed more accurately than via `node`, this is the `TextRange`
89     /// to use instead.
90     pub precise_location: Option<TextRange>,
91     pub macro_name: Option<String>,
92     pub kind: MacroKind,
93     /// The crate id of the proc-macro this macro belongs to, or `None` if the proc-macro can't be found.
94     pub krate: CrateId,
95 }
96
97 #[derive(Debug, Clone, Eq, PartialEq)]
98 pub struct MacroError {
99     pub node: InFile<SyntaxNodePtr>,
100     pub precise_location: Option<TextRange>,
101     pub message: String,
102 }
103
104 #[derive(Debug)]
105 pub struct UnimplementedBuiltinMacro {
106     pub node: InFile<SyntaxNodePtr>,
107 }
108
109 #[derive(Debug)]
110 pub struct InvalidDeriveTarget {
111     pub node: InFile<SyntaxNodePtr>,
112 }
113
114 #[derive(Debug)]
115 pub struct MalformedDerive {
116     pub node: InFile<SyntaxNodePtr>,
117 }
118
119 #[derive(Debug)]
120 pub struct NoSuchField {
121     pub field: InFile<AstPtr<ast::RecordExprField>>,
122 }
123
124 #[derive(Debug)]
125 pub struct BreakOutsideOfLoop {
126     pub expr: InFile<AstPtr<ast::Expr>>,
127 }
128
129 #[derive(Debug)]
130 pub struct MissingUnsafe {
131     pub expr: InFile<AstPtr<ast::Expr>>,
132 }
133
134 #[derive(Debug)]
135 pub struct MissingFields {
136     pub file: HirFileId,
137     pub field_list_parent: Either<AstPtr<ast::RecordExpr>, AstPtr<ast::RecordPat>>,
138     pub field_list_parent_path: Option<AstPtr<ast::Path>>,
139     pub missed_fields: Vec<Name>,
140 }
141
142 #[derive(Debug)]
143 pub struct ReplaceFilterMapNextWithFindMap {
144     pub file: HirFileId,
145     /// This expression is the whole method chain up to and including `.filter_map(..).next()`.
146     pub next_expr: AstPtr<ast::Expr>,
147 }
148
149 #[derive(Debug)]
150 pub struct MismatchedArgCount {
151     pub call_expr: InFile<AstPtr<ast::Expr>>,
152     pub expected: usize,
153     pub found: usize,
154 }
155
156 #[derive(Debug)]
157 pub struct MissingMatchArms {
158     pub file: HirFileId,
159     pub match_expr: AstPtr<ast::Expr>,
160     pub uncovered_patterns: String,
161 }
162
163 #[derive(Debug)]
164 pub struct TypeMismatch {
165     // FIXME: add mismatches in patterns as well
166     pub expr: InFile<AstPtr<ast::Expr>>,
167     pub expected: Type,
168     pub actual: Type,
169 }
170
171 pub use hir_ty::diagnostics::IncorrectCase;