]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/errors.rs
Rollup merge of #102830 - compiler-errors:constness-parity, r=fee1-dead
[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 impl AddToDiagnostic for ForbiddenLetReason {
20     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
21     where
22         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
23     {
24         match self {
25             Self::GenericForbidden => {}
26             Self::NotSupportedOr(span) => {
27                 diag.span_note(span, fluent::ast_passes::not_supported_or);
28             }
29             Self::NotSupportedParentheses(span) => {
30                 diag.span_note(span, fluent::ast_passes::not_supported_parentheses);
31             }
32         }
33     }
34 }
35
36 #[derive(Diagnostic)]
37 #[diag(ast_passes::forbidden_let_stable)]
38 #[note]
39 pub struct ForbiddenLetStable {
40     #[primary_span]
41     pub span: Span,
42 }
43
44 #[derive(Diagnostic)]
45 #[diag(ast_passes::forbidden_assoc_constraint)]
46 pub struct ForbiddenAssocConstraint {
47     #[primary_span]
48     pub span: Span,
49 }
50
51 #[derive(Diagnostic)]
52 #[diag(ast_passes::keyword_lifetime)]
53 pub struct KeywordLifetime {
54     #[primary_span]
55     pub span: Span,
56 }
57
58 #[derive(Diagnostic)]
59 #[diag(ast_passes::invalid_label)]
60 pub struct InvalidLabel {
61     #[primary_span]
62     pub span: Span,
63     pub name: Symbol,
64 }
65
66 #[derive(Diagnostic)]
67 #[diag(ast_passes::invalid_visibility, code = "E0449")]
68 pub struct InvalidVisibility {
69     #[primary_span]
70     pub span: Span,
71     #[label(ast_passes::implied)]
72     pub implied: Option<Span>,
73     #[subdiagnostic]
74     pub note: Option<InvalidVisibilityNote>,
75 }
76
77 #[derive(Subdiagnostic)]
78 pub enum InvalidVisibilityNote {
79     #[note(ast_passes::individual_impl_items)]
80     IndividualImplItems,
81     #[note(ast_passes::individual_foreign_items)]
82     IndividualForeignItems,
83 }
84
85 #[derive(Diagnostic)]
86 #[diag(ast_passes::trait_fn_const, code = "E0379")]
87 pub struct TraitFnConst {
88     #[primary_span]
89     #[label]
90     pub span: Span,
91 }
92
93 #[derive(Diagnostic)]
94 #[diag(ast_passes::forbidden_lifetime_bound)]
95 pub struct ForbiddenLifetimeBound {
96     #[primary_span]
97     pub spans: Vec<Span>,
98 }
99
100 #[derive(Diagnostic)]
101 #[diag(ast_passes::forbidden_non_lifetime_param)]
102 pub struct ForbiddenNonLifetimeParam {
103     #[primary_span]
104     pub spans: Vec<Span>,
105 }
106
107 #[derive(Diagnostic)]
108 #[diag(ast_passes::fn_param_too_many)]
109 pub struct FnParamTooMany {
110     #[primary_span]
111     pub span: Span,
112     pub max_num_args: usize,
113 }
114
115 #[derive(Diagnostic)]
116 #[diag(ast_passes::fn_param_c_var_args_only)]
117 pub struct FnParamCVarArgsOnly {
118     #[primary_span]
119     pub span: Span,
120 }
121
122 #[derive(Diagnostic)]
123 #[diag(ast_passes::fn_param_c_var_args_not_last)]
124 pub struct FnParamCVarArgsNotLast {
125     #[primary_span]
126     pub span: Span,
127 }
128
129 #[derive(Diagnostic)]
130 #[diag(ast_passes::fn_param_doc_comment)]
131 pub struct FnParamDocComment {
132     #[primary_span]
133     #[label]
134     pub span: Span,
135 }
136
137 #[derive(Diagnostic)]
138 #[diag(ast_passes::fn_param_forbidden_attr)]
139 pub struct FnParamForbiddenAttr {
140     #[primary_span]
141     pub span: Span,
142 }
143
144 #[derive(Diagnostic)]
145 #[diag(ast_passes::fn_param_forbidden_self)]
146 #[note]
147 pub struct FnParamForbiddenSelf {
148     #[primary_span]
149     #[label]
150     pub span: Span,
151 }
152
153 #[derive(Diagnostic)]
154 #[diag(ast_passes::forbidden_default)]
155 pub struct ForbiddenDefault {
156     #[primary_span]
157     pub span: Span,
158     #[label]
159     pub def_span: Span,
160 }
161
162 #[derive(Diagnostic)]
163 #[diag(ast_passes::assoc_const_without_body)]
164 pub struct AssocConstWithoutBody {
165     #[primary_span]
166     pub span: Span,
167     #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
168     pub replace_span: Span,
169 }
170
171 #[derive(Diagnostic)]
172 #[diag(ast_passes::assoc_fn_without_body)]
173 pub struct AssocFnWithoutBody {
174     #[primary_span]
175     pub span: Span,
176     #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
177     pub replace_span: Span,
178 }
179
180 #[derive(Diagnostic)]
181 #[diag(ast_passes::assoc_type_without_body)]
182 pub struct AssocTypeWithoutBody {
183     #[primary_span]
184     pub span: Span,
185     #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
186     pub replace_span: Span,
187 }
188
189 #[derive(Diagnostic)]
190 #[diag(ast_passes::const_without_body)]
191 pub struct ConstWithoutBody {
192     #[primary_span]
193     pub span: Span,
194     #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
195     pub replace_span: Span,
196 }
197
198 #[derive(Diagnostic)]
199 #[diag(ast_passes::static_without_body)]
200 pub struct StaticWithoutBody {
201     #[primary_span]
202     pub span: Span,
203     #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
204     pub replace_span: Span,
205 }
206
207 #[derive(Diagnostic)]
208 #[diag(ast_passes::ty_alias_without_body)]
209 pub struct TyAliasWithoutBody {
210     #[primary_span]
211     pub span: Span,
212     #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
213     pub replace_span: Span,
214 }
215
216 #[derive(Diagnostic)]
217 #[diag(ast_passes::fn_without_body)]
218 pub struct FnWithoutBody {
219     #[primary_span]
220     pub span: Span,
221     #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
222     pub replace_span: Span,
223     #[subdiagnostic]
224     pub extern_block_suggestion: Option<ExternBlockSuggestion>,
225 }
226
227 pub struct ExternBlockSuggestion {
228     pub start_span: Span,
229     pub end_span: Span,
230     pub abi: Option<Symbol>,
231 }
232
233 impl AddToDiagnostic for ExternBlockSuggestion {
234     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
235     where
236         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
237     {
238         let start_suggestion = if let Some(abi) = self.abi {
239             format!("extern \"{}\" {{", abi)
240         } else {
241             "extern {".to_owned()
242         };
243         let end_suggestion = " }".to_owned();
244
245         diag.multipart_suggestion(
246             fluent::ast_passes::extern_block_suggestion,
247             vec![(self.start_span, start_suggestion), (self.end_span, end_suggestion)],
248             Applicability::MaybeIncorrect,
249         );
250     }
251 }