]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_attr/src/session_diagnostics.rs
Rollup merge of #101473 - nnethercote:mir-size-assertions, r=lqd
[rust.git] / compiler / rustc_attr / src / session_diagnostics.rs
1 use std::num::IntErrorKind;
2
3 use rustc_ast as ast;
4 use rustc_errors::{
5     error_code, fluent, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler,
6 };
7 use rustc_macros::SessionDiagnostic;
8 use rustc_session::SessionDiagnostic;
9 use rustc_span::{Span, Symbol};
10
11 use crate::UnsupportedLiteralReason;
12
13 #[derive(SessionDiagnostic)]
14 #[diag(attr::expected_one_cfg_pattern, code = "E0536")]
15 pub(crate) struct ExpectedOneCfgPattern {
16     #[primary_span]
17     pub span: Span,
18 }
19
20 #[derive(SessionDiagnostic)]
21 #[diag(attr::invalid_predicate, code = "E0537")]
22 pub(crate) struct InvalidPredicate {
23     #[primary_span]
24     pub span: Span,
25
26     pub predicate: String,
27 }
28
29 #[derive(SessionDiagnostic)]
30 #[diag(attr::multiple_item, code = "E0538")]
31 pub(crate) struct MultipleItem {
32     #[primary_span]
33     pub span: Span,
34
35     pub item: String,
36 }
37
38 #[derive(SessionDiagnostic)]
39 #[diag(attr::incorrect_meta_item, code = "E0539")]
40 pub(crate) struct IncorrectMetaItem {
41     #[primary_span]
42     pub span: Span,
43 }
44
45 // Error code: E0541
46 pub(crate) struct UnknownMetaItem<'a> {
47     pub span: Span,
48     pub item: String,
49     pub expected: &'a [&'a str],
50 }
51
52 // Manual implementation to be able to format `expected` items correctly.
53 impl<'a> SessionDiagnostic<'a> for UnknownMetaItem<'_> {
54     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
55         let expected = self.expected.iter().map(|name| format!("`{}`", name)).collect::<Vec<_>>();
56         let mut diag = handler.struct_span_err_with_code(
57             self.span,
58             fluent::attr::unknown_meta_item,
59             error_code!(E0541),
60         );
61         diag.set_arg("item", self.item);
62         diag.set_arg("expected", expected.join(", "));
63         diag.span_label(self.span, fluent::attr::label);
64         diag
65     }
66 }
67
68 #[derive(SessionDiagnostic)]
69 #[diag(attr::missing_since, code = "E0542")]
70 pub(crate) struct MissingSince {
71     #[primary_span]
72     pub span: Span,
73 }
74
75 #[derive(SessionDiagnostic)]
76 #[diag(attr::missing_note, code = "E0543")]
77 pub(crate) struct MissingNote {
78     #[primary_span]
79     pub span: Span,
80 }
81
82 #[derive(SessionDiagnostic)]
83 #[diag(attr::multiple_stability_levels, code = "E0544")]
84 pub(crate) struct MultipleStabilityLevels {
85     #[primary_span]
86     pub span: Span,
87 }
88
89 #[derive(SessionDiagnostic)]
90 #[diag(attr::invalid_issue_string, code = "E0545")]
91 pub(crate) struct InvalidIssueString {
92     #[primary_span]
93     pub span: Span,
94
95     #[subdiagnostic]
96     pub cause: Option<InvalidIssueStringCause>,
97 }
98
99 // The error kinds of `IntErrorKind` are duplicated here in order to allow the messages to be
100 // translatable.
101 #[derive(SessionSubdiagnostic)]
102 pub(crate) enum InvalidIssueStringCause {
103     #[label(attr::must_not_be_zero)]
104     MustNotBeZero {
105         #[primary_span]
106         span: Span,
107     },
108
109     #[label(attr::empty)]
110     Empty {
111         #[primary_span]
112         span: Span,
113     },
114
115     #[label(attr::invalid_digit)]
116     InvalidDigit {
117         #[primary_span]
118         span: Span,
119     },
120
121     #[label(attr::pos_overflow)]
122     PosOverflow {
123         #[primary_span]
124         span: Span,
125     },
126
127     #[label(attr::neg_overflow)]
128     NegOverflow {
129         #[primary_span]
130         span: Span,
131     },
132 }
133
134 impl InvalidIssueStringCause {
135     pub fn from_int_error_kind(span: Span, kind: &IntErrorKind) -> Option<Self> {
136         match kind {
137             IntErrorKind::Empty => Some(Self::Empty { span }),
138             IntErrorKind::InvalidDigit => Some(Self::InvalidDigit { span }),
139             IntErrorKind::PosOverflow => Some(Self::PosOverflow { span }),
140             IntErrorKind::NegOverflow => Some(Self::NegOverflow { span }),
141             IntErrorKind::Zero => Some(Self::MustNotBeZero { span }),
142             _ => None,
143         }
144     }
145 }
146
147 #[derive(SessionDiagnostic)]
148 #[diag(attr::missing_feature, code = "E0546")]
149 pub(crate) struct MissingFeature {
150     #[primary_span]
151     pub span: Span,
152 }
153
154 #[derive(SessionDiagnostic)]
155 #[diag(attr::non_ident_feature, code = "E0546")]
156 pub(crate) struct NonIdentFeature {
157     #[primary_span]
158     pub span: Span,
159 }
160
161 #[derive(SessionDiagnostic)]
162 #[diag(attr::missing_issue, code = "E0547")]
163 pub(crate) struct MissingIssue {
164     #[primary_span]
165     pub span: Span,
166 }
167
168 // FIXME: This diagnostic is identical to `IncorrectMetaItem`, barring the error code. Consider
169 // changing this to `IncorrectMetaItem`. See #51489.
170 #[derive(SessionDiagnostic)]
171 #[diag(attr::incorrect_meta_item, code = "E0551")]
172 pub(crate) struct IncorrectMetaItem2 {
173     #[primary_span]
174     pub span: Span,
175 }
176
177 // FIXME: Why is this the same error code as `InvalidReprHintNoParen` and `InvalidReprHintNoValue`?
178 // It is more similar to `IncorrectReprFormatGeneric`.
179 #[derive(SessionDiagnostic)]
180 #[diag(attr::incorrect_repr_format_packed_one_or_zero_arg, code = "E0552")]
181 pub(crate) struct IncorrectReprFormatPackedOneOrZeroArg {
182     #[primary_span]
183     pub span: Span,
184 }
185
186 #[derive(SessionDiagnostic)]
187 #[diag(attr::invalid_repr_hint_no_paren, code = "E0552")]
188 pub(crate) struct InvalidReprHintNoParen {
189     #[primary_span]
190     pub span: Span,
191
192     pub name: String,
193 }
194
195 #[derive(SessionDiagnostic)]
196 #[diag(attr::invalid_repr_hint_no_value, code = "E0552")]
197 pub(crate) struct InvalidReprHintNoValue {
198     #[primary_span]
199     pub span: Span,
200
201     pub name: String,
202 }
203
204 // Error code: E0565
205 pub(crate) struct UnsupportedLiteral {
206     pub span: Span,
207     pub reason: UnsupportedLiteralReason,
208     pub is_bytestr: bool,
209     pub start_point_span: Span,
210 }
211
212 impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral {
213     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
214         let mut diag = handler.struct_span_err_with_code(
215             self.span,
216             match self.reason {
217                 UnsupportedLiteralReason::Generic => fluent::attr::unsupported_literal_generic,
218                 UnsupportedLiteralReason::CfgString => fluent::attr::unsupported_literal_cfg_string,
219                 UnsupportedLiteralReason::DeprecatedString => {
220                     fluent::attr::unsupported_literal_deprecated_string
221                 }
222                 UnsupportedLiteralReason::DeprecatedKvPair => {
223                     fluent::attr::unsupported_literal_deprecated_kv_pair
224                 }
225             },
226             error_code!(E0565),
227         );
228         if self.is_bytestr {
229             diag.span_suggestion(
230                 self.start_point_span,
231                 fluent::attr::unsupported_literal_suggestion,
232                 "",
233                 Applicability::MaybeIncorrect,
234             );
235         }
236         diag
237     }
238 }
239
240 #[derive(SessionDiagnostic)]
241 #[diag(attr::invalid_repr_align_need_arg, code = "E0589")]
242 pub(crate) struct InvalidReprAlignNeedArg {
243     #[primary_span]
244     #[suggestion(code = "align(...)", applicability = "has-placeholders")]
245     pub span: Span,
246 }
247
248 #[derive(SessionDiagnostic)]
249 #[diag(attr::invalid_repr_generic, code = "E0589")]
250 pub(crate) struct InvalidReprGeneric<'a> {
251     #[primary_span]
252     pub span: Span,
253
254     pub repr_arg: String,
255     pub error_part: &'a str,
256 }
257
258 #[derive(SessionDiagnostic)]
259 #[diag(attr::incorrect_repr_format_align_one_arg, code = "E0693")]
260 pub(crate) struct IncorrectReprFormatAlignOneArg {
261     #[primary_span]
262     pub span: Span,
263 }
264
265 #[derive(SessionDiagnostic)]
266 #[diag(attr::incorrect_repr_format_generic, code = "E0693")]
267 pub(crate) struct IncorrectReprFormatGeneric<'a> {
268     #[primary_span]
269     pub span: Span,
270
271     pub repr_arg: &'a str,
272
273     #[subdiagnostic]
274     pub cause: Option<IncorrectReprFormatGenericCause<'a>>,
275 }
276
277 #[derive(SessionSubdiagnostic)]
278 pub(crate) enum IncorrectReprFormatGenericCause<'a> {
279     #[suggestion(attr::suggestion, code = "{name}({int})", applicability = "machine-applicable")]
280     Int {
281         #[primary_span]
282         span: Span,
283
284         #[skip_arg]
285         name: &'a str,
286
287         #[skip_arg]
288         int: u128,
289     },
290
291     #[suggestion(
292         attr::suggestion,
293         code = "{name}({symbol})",
294         applicability = "machine-applicable"
295     )]
296     Symbol {
297         #[primary_span]
298         span: Span,
299
300         #[skip_arg]
301         name: &'a str,
302
303         #[skip_arg]
304         symbol: Symbol,
305     },
306 }
307
308 impl<'a> IncorrectReprFormatGenericCause<'a> {
309     pub fn from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self> {
310         match kind {
311             ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
312                 Some(Self::Int { span, name, int: *int })
313             }
314             ast::LitKind::Str(symbol, _) => Some(Self::Symbol { span, name, symbol: *symbol }),
315             _ => None,
316         }
317     }
318 }
319
320 #[derive(SessionDiagnostic)]
321 #[diag(attr::rustc_promotable_pairing, code = "E0717")]
322 pub(crate) struct RustcPromotablePairing {
323     #[primary_span]
324     pub span: Span,
325 }
326
327 #[derive(SessionDiagnostic)]
328 #[diag(attr::rustc_allowed_unstable_pairing, code = "E0789")]
329 pub(crate) struct RustcAllowedUnstablePairing {
330     #[primary_span]
331     pub span: Span,
332 }
333
334 #[derive(SessionDiagnostic)]
335 #[diag(attr::cfg_predicate_identifier)]
336 pub(crate) struct CfgPredicateIdentifier {
337     #[primary_span]
338     pub span: Span,
339 }
340
341 #[derive(SessionDiagnostic)]
342 #[diag(attr::deprecated_item_suggestion)]
343 pub(crate) struct DeprecatedItemSuggestion {
344     #[primary_span]
345     pub span: Span,
346
347     #[help]
348     pub is_nightly: Option<()>,
349
350     #[note]
351     pub details: (),
352 }
353
354 #[derive(SessionDiagnostic)]
355 #[diag(attr::expected_single_version_literal)]
356 pub(crate) struct ExpectedSingleVersionLiteral {
357     #[primary_span]
358     pub span: Span,
359 }
360
361 #[derive(SessionDiagnostic)]
362 #[diag(attr::expected_version_literal)]
363 pub(crate) struct ExpectedVersionLiteral {
364     #[primary_span]
365     pub span: Span,
366 }
367
368 #[derive(SessionDiagnostic)]
369 #[diag(attr::expects_feature_list)]
370 pub(crate) struct ExpectsFeatureList {
371     #[primary_span]
372     pub span: Span,
373
374     pub name: String,
375 }
376
377 #[derive(SessionDiagnostic)]
378 #[diag(attr::expects_features)]
379 pub(crate) struct ExpectsFeatures {
380     #[primary_span]
381     pub span: Span,
382
383     pub name: String,
384 }
385
386 #[derive(SessionDiagnostic)]
387 #[diag(attr::soft_no_args)]
388 pub(crate) struct SoftNoArgs {
389     #[primary_span]
390     pub span: Span,
391 }
392
393 #[derive(SessionDiagnostic)]
394 #[diag(attr::unknown_version_literal)]
395 pub(crate) struct UnknownVersionLiteral {
396     #[primary_span]
397     pub span: Span,
398 }