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