]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/errors.rs
Auto merge of #104679 - dvdhrm:rw/dso, r=petrochenkov
[rust.git] / compiler / rustc_ast_passes / src / errors.rs
1 //! Errors emitted by ast_passes.
2
3 use rustc_errors::{fluent, AddToDiagnostic, Applicability, Diagnostic, SubdiagnosticMessage};
4 use rustc_macros::{Diagnostic, Subdiagnostic};
5 use rustc_span::{Span, Symbol};
6
7 use crate::ast_validation::ForbiddenLetReason;
8
9 #[derive(Diagnostic)]
10 #[diag(ast_passes_forbidden_let)]
11 #[note]
12 pub struct ForbiddenLet {
13     #[primary_span]
14     pub span: Span,
15     #[subdiagnostic]
16     pub(crate) reason: ForbiddenLetReason,
17 }
18
19 #[derive(Diagnostic)]
20 #[diag(ast_passes_forbidden_let_stable)]
21 #[note]
22 pub struct ForbiddenLetStable {
23     #[primary_span]
24     pub span: Span,
25 }
26
27 #[derive(Diagnostic)]
28 #[diag(ast_passes_forbidden_assoc_constraint)]
29 pub struct ForbiddenAssocConstraint {
30     #[primary_span]
31     pub span: Span,
32 }
33
34 #[derive(Diagnostic)]
35 #[diag(ast_passes_keyword_lifetime)]
36 pub struct KeywordLifetime {
37     #[primary_span]
38     pub span: Span,
39 }
40
41 #[derive(Diagnostic)]
42 #[diag(ast_passes_invalid_label)]
43 pub struct InvalidLabel {
44     #[primary_span]
45     pub span: Span,
46     pub name: Symbol,
47 }
48
49 #[derive(Diagnostic)]
50 #[diag(ast_passes_invalid_visibility, code = "E0449")]
51 pub struct InvalidVisibility {
52     #[primary_span]
53     pub span: Span,
54     #[label(implied)]
55     pub implied: Option<Span>,
56     #[subdiagnostic]
57     pub note: Option<InvalidVisibilityNote>,
58 }
59
60 #[derive(Subdiagnostic)]
61 pub enum InvalidVisibilityNote {
62     #[note(individual_impl_items)]
63     IndividualImplItems,
64     #[note(individual_foreign_items)]
65     IndividualForeignItems,
66 }
67
68 #[derive(Diagnostic)]
69 #[diag(ast_passes_trait_fn_const, code = "E0379")]
70 pub struct TraitFnConst {
71     #[primary_span]
72     #[label]
73     pub span: Span,
74 }
75
76 #[derive(Diagnostic)]
77 #[diag(ast_passes_forbidden_lifetime_bound)]
78 pub struct ForbiddenLifetimeBound {
79     #[primary_span]
80     pub spans: Vec<Span>,
81 }
82
83 #[derive(Diagnostic)]
84 #[diag(ast_passes_forbidden_non_lifetime_param)]
85 pub struct ForbiddenNonLifetimeParam {
86     #[primary_span]
87     pub spans: Vec<Span>,
88 }
89
90 #[derive(Diagnostic)]
91 #[diag(ast_passes_fn_param_too_many)]
92 pub struct FnParamTooMany {
93     #[primary_span]
94     pub span: Span,
95     pub max_num_args: usize,
96 }
97
98 #[derive(Diagnostic)]
99 #[diag(ast_passes_fn_param_c_var_args_only)]
100 pub struct FnParamCVarArgsOnly {
101     #[primary_span]
102     pub span: Span,
103 }
104
105 #[derive(Diagnostic)]
106 #[diag(ast_passes_fn_param_c_var_args_not_last)]
107 pub struct FnParamCVarArgsNotLast {
108     #[primary_span]
109     pub span: Span,
110 }
111
112 #[derive(Diagnostic)]
113 #[diag(ast_passes_fn_param_doc_comment)]
114 pub struct FnParamDocComment {
115     #[primary_span]
116     #[label]
117     pub span: Span,
118 }
119
120 #[derive(Diagnostic)]
121 #[diag(ast_passes_fn_param_forbidden_attr)]
122 pub struct FnParamForbiddenAttr {
123     #[primary_span]
124     pub span: Span,
125 }
126
127 #[derive(Diagnostic)]
128 #[diag(ast_passes_fn_param_forbidden_self)]
129 #[note]
130 pub struct FnParamForbiddenSelf {
131     #[primary_span]
132     #[label]
133     pub span: Span,
134 }
135
136 #[derive(Diagnostic)]
137 #[diag(ast_passes_forbidden_default)]
138 pub struct ForbiddenDefault {
139     #[primary_span]
140     pub span: Span,
141     #[label]
142     pub def_span: Span,
143 }
144
145 #[derive(Diagnostic)]
146 #[diag(ast_passes_assoc_const_without_body)]
147 pub struct AssocConstWithoutBody {
148     #[primary_span]
149     pub span: Span,
150     #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
151     pub replace_span: Span,
152 }
153
154 #[derive(Diagnostic)]
155 #[diag(ast_passes_assoc_fn_without_body)]
156 pub struct AssocFnWithoutBody {
157     #[primary_span]
158     pub span: Span,
159     #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
160     pub replace_span: Span,
161 }
162
163 #[derive(Diagnostic)]
164 #[diag(ast_passes_assoc_type_without_body)]
165 pub struct AssocTypeWithoutBody {
166     #[primary_span]
167     pub span: Span,
168     #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
169     pub replace_span: Span,
170 }
171
172 #[derive(Diagnostic)]
173 #[diag(ast_passes_const_without_body)]
174 pub struct ConstWithoutBody {
175     #[primary_span]
176     pub span: Span,
177     #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
178     pub replace_span: Span,
179 }
180
181 #[derive(Diagnostic)]
182 #[diag(ast_passes_static_without_body)]
183 pub struct StaticWithoutBody {
184     #[primary_span]
185     pub span: Span,
186     #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
187     pub replace_span: Span,
188 }
189
190 #[derive(Diagnostic)]
191 #[diag(ast_passes_ty_alias_without_body)]
192 pub struct TyAliasWithoutBody {
193     #[primary_span]
194     pub span: Span,
195     #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
196     pub replace_span: Span,
197 }
198
199 #[derive(Diagnostic)]
200 #[diag(ast_passes_fn_without_body)]
201 pub struct FnWithoutBody {
202     #[primary_span]
203     pub span: Span,
204     #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
205     pub replace_span: Span,
206     #[subdiagnostic]
207     pub extern_block_suggestion: Option<ExternBlockSuggestion>,
208 }
209
210 pub struct ExternBlockSuggestion {
211     pub start_span: Span,
212     pub end_span: Span,
213     pub abi: Option<Symbol>,
214 }
215
216 impl AddToDiagnostic for ExternBlockSuggestion {
217     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
218     where
219         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
220     {
221         let start_suggestion = if let Some(abi) = self.abi {
222             format!("extern \"{}\" {{", abi)
223         } else {
224             "extern {".to_owned()
225         };
226         let end_suggestion = " }".to_owned();
227
228         diag.multipart_suggestion(
229             fluent::extern_block_suggestion,
230             vec![(self.start_span, start_suggestion), (self.end_span, end_suggestion)],
231             Applicability::MaybeIncorrect,
232         );
233     }
234 }