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