]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/errors.rs
Auto merge of #98626 - oli-obk:tracing, r=lcnr
[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_assoc_constraint)]
35 pub struct ForbiddenAssocConstraint {
36     #[primary_span]
37     pub span: Span,
38 }
39
40 #[derive(SessionDiagnostic)]
41 #[diag(ast_passes::keyword_lifetime)]
42 pub struct KeywordLifetime {
43     #[primary_span]
44     pub span: Span,
45 }
46
47 #[derive(SessionDiagnostic)]
48 #[diag(ast_passes::invalid_label)]
49 pub struct InvalidLabel {
50     #[primary_span]
51     pub span: Span,
52     pub name: Symbol,
53 }
54
55 #[derive(SessionDiagnostic)]
56 #[diag(ast_passes::invalid_visibility, code = "E0449")]
57 pub struct InvalidVisibility {
58     #[primary_span]
59     pub span: Span,
60     #[label(ast_passes::implied)]
61     pub implied: Option<Span>,
62     #[subdiagnostic]
63     pub note: Option<InvalidVisibilityNote>,
64 }
65
66 #[derive(SessionSubdiagnostic)]
67 pub enum InvalidVisibilityNote {
68     #[note(ast_passes::individual_impl_items)]
69     IndividualImplItems,
70     #[note(ast_passes::individual_foreign_items)]
71     IndividualForeignItems,
72 }
73
74 #[derive(SessionDiagnostic)]
75 #[diag(ast_passes::trait_fn_async, code = "E0706")]
76 #[note]
77 #[note(ast_passes::note2)]
78 pub struct TraitFnAsync {
79     #[primary_span]
80     pub fn_span: Span,
81     #[label]
82     pub span: Span,
83 }
84
85 #[derive(SessionDiagnostic)]
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(SessionDiagnostic)]
94 #[diag(ast_passes::forbidden_lifetime_bound)]
95 pub struct ForbiddenLifetimeBound {
96     #[primary_span]
97     pub spans: Vec<Span>,
98 }
99
100 #[derive(SessionDiagnostic)]
101 #[diag(ast_passes::forbidden_non_lifetime_param)]
102 pub struct ForbiddenNonLifetimeParam {
103     #[primary_span]
104     pub spans: Vec<Span>,
105 }
106
107 #[derive(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
138 #[diag(ast_passes::fn_param_forbidden_attr)]
139 pub struct FnParamForbiddenAttr {
140     #[primary_span]
141     pub span: Span,
142 }
143
144 #[derive(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
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(SessionDiagnostic)]
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 AddSubdiagnostic for ExternBlockSuggestion {
234     fn add_to_diagnostic(self, diag: &mut Diagnostic) {
235         let start_suggestion = if let Some(abi) = self.abi {
236             format!("extern \"{}\" {{", abi)
237         } else {
238             "extern {".to_owned()
239         };
240         let end_suggestion = " }".to_owned();
241
242         diag.multipart_suggestion(
243             fluent::ast_passes::extern_block_suggestion,
244             vec![(self.start_span, start_suggestion), (self.end_span, end_suggestion)],
245             Applicability::MaybeIncorrect,
246         );
247     }
248 }