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