• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::num::IntErrorKind;
2 
3 use rustc_ast as ast;
4 use rustc_errors::{
5     error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic,
6 };
7 use rustc_macros::Diagnostic;
8 use rustc_span::{Span, Symbol};
9 
10 use crate::fluent_generated as fluent;
11 use crate::UnsupportedLiteralReason;
12 
13 #[derive(Diagnostic)]
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(Diagnostic)]
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(Diagnostic)]
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(Diagnostic)]
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> IntoDiagnostic<'a> for UnknownMetaItem<'_> {
into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed>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(Diagnostic)]
69 #[diag(attr_missing_since, code = "E0542")]
70 pub(crate) struct MissingSince {
71     #[primary_span]
72     pub span: Span,
73 }
74 
75 #[derive(Diagnostic)]
76 #[diag(attr_missing_note, code = "E0543")]
77 pub(crate) struct MissingNote {
78     #[primary_span]
79     pub span: Span,
80 }
81 
82 #[derive(Diagnostic)]
83 #[diag(attr_multiple_stability_levels, code = "E0544")]
84 pub(crate) struct MultipleStabilityLevels {
85     #[primary_span]
86     pub span: Span,
87 }
88 
89 #[derive(Diagnostic)]
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(Subdiagnostic)]
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 {
from_int_error_kind(span: Span, kind: &IntErrorKind) -> Option<Self>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(Diagnostic)]
148 #[diag(attr_missing_feature, code = "E0546")]
149 pub(crate) struct MissingFeature {
150     #[primary_span]
151     pub span: Span,
152 }
153 
154 #[derive(Diagnostic)]
155 #[diag(attr_non_ident_feature, code = "E0546")]
156 pub(crate) struct NonIdentFeature {
157     #[primary_span]
158     pub span: Span,
159 }
160 
161 #[derive(Diagnostic)]
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(Diagnostic)]
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(Diagnostic)]
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(Diagnostic)]
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(Diagnostic)]
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> IntoDiagnostic<'a> for UnsupportedLiteral {
into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed>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(Diagnostic)]
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(Diagnostic)]
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(Diagnostic)]
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(Diagnostic)]
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(Subdiagnostic)]
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(attr_suggestion, code = "{name}({symbol})", applicability = "machine-applicable")]
292     Symbol {
293         #[primary_span]
294         span: Span,
295 
296         #[skip_arg]
297         name: &'a str,
298 
299         #[skip_arg]
300         symbol: Symbol,
301     },
302 }
303 
304 impl<'a> IncorrectReprFormatGenericCause<'a> {
from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self>305     pub fn from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self> {
306         match kind {
307             ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
308                 Some(Self::Int { span, name, int: *int })
309             }
310             ast::LitKind::Str(symbol, _) => Some(Self::Symbol { span, name, symbol: *symbol }),
311             _ => None,
312         }
313     }
314 }
315 
316 #[derive(Diagnostic)]
317 #[diag(attr_rustc_promotable_pairing, code = "E0717")]
318 pub(crate) struct RustcPromotablePairing {
319     #[primary_span]
320     pub span: Span,
321 }
322 
323 #[derive(Diagnostic)]
324 #[diag(attr_rustc_allowed_unstable_pairing, code = "E0789")]
325 pub(crate) struct RustcAllowedUnstablePairing {
326     #[primary_span]
327     pub span: Span,
328 }
329 
330 #[derive(Diagnostic)]
331 #[diag(attr_cfg_predicate_identifier)]
332 pub(crate) struct CfgPredicateIdentifier {
333     #[primary_span]
334     pub span: Span,
335 }
336 
337 #[derive(Diagnostic)]
338 #[diag(attr_deprecated_item_suggestion)]
339 pub(crate) struct DeprecatedItemSuggestion {
340     #[primary_span]
341     pub span: Span,
342 
343     #[help]
344     pub is_nightly: Option<()>,
345 
346     #[note]
347     pub details: (),
348 }
349 
350 #[derive(Diagnostic)]
351 #[diag(attr_expected_single_version_literal)]
352 pub(crate) struct ExpectedSingleVersionLiteral {
353     #[primary_span]
354     pub span: Span,
355 }
356 
357 #[derive(Diagnostic)]
358 #[diag(attr_expected_version_literal)]
359 pub(crate) struct ExpectedVersionLiteral {
360     #[primary_span]
361     pub span: Span,
362 }
363 
364 #[derive(Diagnostic)]
365 #[diag(attr_expects_feature_list)]
366 pub(crate) struct ExpectsFeatureList {
367     #[primary_span]
368     pub span: Span,
369 
370     pub name: String,
371 }
372 
373 #[derive(Diagnostic)]
374 #[diag(attr_expects_features)]
375 pub(crate) struct ExpectsFeatures {
376     #[primary_span]
377     pub span: Span,
378 
379     pub name: String,
380 }
381 
382 #[derive(Diagnostic)]
383 #[diag(attr_soft_no_args)]
384 pub(crate) struct SoftNoArgs {
385     #[primary_span]
386     pub span: Span,
387 }
388 
389 #[derive(Diagnostic)]
390 #[diag(attr_unknown_version_literal)]
391 pub(crate) struct UnknownVersionLiteral {
392     #[primary_span]
393     pub span: Span,
394 }
395