]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/errors.rs
Auto merge of #101250 - klensy:bump-deps-08-22, r=Dylan-DPC
[rust.git] / compiler / rustc_ast_passes / src / errors.rs
1 //! Errors emitted by ast_passes.
2
3 use rustc_errors::{fluent, AddSubdiagnostic, Applicability, Diagnostic};
4 use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
5 use rustc_span::{Span, Symbol};
6
7 use crate::ast_validation::ForbiddenLetReason;
8
9 #[derive(SessionDiagnostic)]
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 AddSubdiagnostic 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(SessionDiagnostic)]
34 #[diag(ast_passes::forbidden_let_stable)]
35 #[note]
36 pub struct ForbiddenLetStable {
37     #[primary_span]
38     pub span: Span,
39 }
40
41 #[derive(SessionDiagnostic)]
42 #[diag(ast_passes::forbidden_assoc_constraint)]
43 pub struct ForbiddenAssocConstraint {
44     #[primary_span]
45     pub span: Span,
46 }
47
48 #[derive(SessionDiagnostic)]
49 #[diag(ast_passes::keyword_lifetime)]
50 pub struct KeywordLifetime {
51     #[primary_span]
52     pub span: Span,
53 }
54
55 #[derive(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionSubdiagnostic)]
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(SessionDiagnostic)]
83 #[diag(ast_passes::trait_fn_async, code = "E0706")]
84 #[note]
85 #[note(ast_passes::note2)]
86 pub struct TraitFnAsync {
87     #[primary_span]
88     pub fn_span: Span,
89     #[label]
90     pub span: Span,
91 }
92
93 #[derive(SessionDiagnostic)]
94 #[diag(ast_passes::trait_fn_const, code = "E0379")]
95 pub struct TraitFnConst {
96     #[primary_span]
97     #[label]
98     pub span: Span,
99 }
100
101 #[derive(SessionDiagnostic)]
102 #[diag(ast_passes::forbidden_lifetime_bound)]
103 pub struct ForbiddenLifetimeBound {
104     #[primary_span]
105     pub spans: Vec<Span>,
106 }
107
108 #[derive(SessionDiagnostic)]
109 #[diag(ast_passes::forbidden_non_lifetime_param)]
110 pub struct ForbiddenNonLifetimeParam {
111     #[primary_span]
112     pub spans: Vec<Span>,
113 }
114
115 #[derive(SessionDiagnostic)]
116 #[diag(ast_passes::fn_param_too_many)]
117 pub struct FnParamTooMany {
118     #[primary_span]
119     pub span: Span,
120     pub max_num_args: usize,
121 }
122
123 #[derive(SessionDiagnostic)]
124 #[diag(ast_passes::fn_param_c_var_args_only)]
125 pub struct FnParamCVarArgsOnly {
126     #[primary_span]
127     pub span: Span,
128 }
129
130 #[derive(SessionDiagnostic)]
131 #[diag(ast_passes::fn_param_c_var_args_not_last)]
132 pub struct FnParamCVarArgsNotLast {
133     #[primary_span]
134     pub span: Span,
135 }
136
137 #[derive(SessionDiagnostic)]
138 #[diag(ast_passes::fn_param_doc_comment)]
139 pub struct FnParamDocComment {
140     #[primary_span]
141     #[label]
142     pub span: Span,
143 }
144
145 #[derive(SessionDiagnostic)]
146 #[diag(ast_passes::fn_param_forbidden_attr)]
147 pub struct FnParamForbiddenAttr {
148     #[primary_span]
149     pub span: Span,
150 }
151
152 #[derive(SessionDiagnostic)]
153 #[diag(ast_passes::fn_param_forbidden_self)]
154 #[note]
155 pub struct FnParamForbiddenSelf {
156     #[primary_span]
157     #[label]
158     pub span: Span,
159 }
160
161 #[derive(SessionDiagnostic)]
162 #[diag(ast_passes::forbidden_default)]
163 pub struct ForbiddenDefault {
164     #[primary_span]
165     pub span: Span,
166     #[label]
167     pub def_span: Span,
168 }
169
170 #[derive(SessionDiagnostic)]
171 #[diag(ast_passes::assoc_const_without_body)]
172 pub struct AssocConstWithoutBody {
173     #[primary_span]
174     pub span: Span,
175     #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
176     pub replace_span: Span,
177 }
178
179 #[derive(SessionDiagnostic)]
180 #[diag(ast_passes::assoc_fn_without_body)]
181 pub struct AssocFnWithoutBody {
182     #[primary_span]
183     pub span: Span,
184     #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
185     pub replace_span: Span,
186 }
187
188 #[derive(SessionDiagnostic)]
189 #[diag(ast_passes::assoc_type_without_body)]
190 pub struct AssocTypeWithoutBody {
191     #[primary_span]
192     pub span: Span,
193     #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
194     pub replace_span: Span,
195 }
196
197 #[derive(SessionDiagnostic)]
198 #[diag(ast_passes::const_without_body)]
199 pub struct ConstWithoutBody {
200     #[primary_span]
201     pub span: Span,
202     #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
203     pub replace_span: Span,
204 }
205
206 #[derive(SessionDiagnostic)]
207 #[diag(ast_passes::static_without_body)]
208 pub struct StaticWithoutBody {
209     #[primary_span]
210     pub span: Span,
211     #[suggestion(code = " = <expr>;", applicability = "has-placeholders")]
212     pub replace_span: Span,
213 }
214
215 #[derive(SessionDiagnostic)]
216 #[diag(ast_passes::ty_alias_without_body)]
217 pub struct TyAliasWithoutBody {
218     #[primary_span]
219     pub span: Span,
220     #[suggestion(code = " = <type>;", applicability = "has-placeholders")]
221     pub replace_span: Span,
222 }
223
224 #[derive(SessionDiagnostic)]
225 #[diag(ast_passes::fn_without_body)]
226 pub struct FnWithoutBody {
227     #[primary_span]
228     pub span: Span,
229     #[suggestion(code = " {{ <body> }}", applicability = "has-placeholders")]
230     pub replace_span: Span,
231     #[subdiagnostic]
232     pub extern_block_suggestion: Option<ExternBlockSuggestion>,
233 }
234
235 pub struct ExternBlockSuggestion {
236     pub start_span: Span,
237     pub end_span: Span,
238     pub abi: Option<Symbol>,
239 }
240
241 impl AddSubdiagnostic for ExternBlockSuggestion {
242     fn add_to_diagnostic(self, diag: &mut Diagnostic) {
243         let start_suggestion = if let Some(abi) = self.abi {
244             format!("extern \"{}\" {{", abi)
245         } else {
246             "extern {".to_owned()
247         };
248         let end_suggestion = " }".to_owned();
249
250         diag.multipart_suggestion(
251             fluent::ast_passes::extern_block_suggestion,
252             vec![(self.start_span, start_suggestion), (self.end_span, end_suggestion)],
253             Applicability::MaybeIncorrect,
254         );
255     }
256 }