1 use std::num::IntErrorKind;
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};
9 use crate::UnsupportedLiteralReason;
11 #[derive(SessionDiagnostic)]
12 #[diag(attr::expected_one_cfg_pattern, code = "E0536")]
13 pub(crate) struct ExpectedOneCfgPattern {
18 #[derive(SessionDiagnostic)]
19 #[diag(attr::invalid_predicate, code = "E0537")]
20 pub(crate) struct InvalidPredicate {
24 pub predicate: String,
27 #[derive(SessionDiagnostic)]
28 #[diag(attr::multiple_item, code = "E0538")]
29 pub(crate) struct MultipleItem {
36 #[derive(SessionDiagnostic)]
37 #[diag(attr::incorrect_meta_item, code = "E0539")]
38 pub(crate) struct IncorrectMetaItem {
44 pub(crate) struct UnknownMetaItem<'a> {
47 pub expected: &'a [&'a str],
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(
56 fluent::attr::unknown_meta_item,
59 diag.set_arg("item", self.item);
60 diag.set_arg("expected", expected.join(", "));
61 diag.span_label(self.span, fluent::attr::label);
66 #[derive(SessionDiagnostic)]
67 #[diag(attr::missing_since, code = "E0542")]
68 pub(crate) struct MissingSince {
73 #[derive(SessionDiagnostic)]
74 #[diag(attr::missing_note, code = "E0543")]
75 pub(crate) struct MissingNote {
80 #[derive(SessionDiagnostic)]
81 #[diag(attr::multiple_stability_levels, code = "E0544")]
82 pub(crate) struct MultipleStabilityLevels {
87 #[derive(SessionDiagnostic)]
88 #[diag(attr::invalid_issue_string, code = "E0545")]
89 pub(crate) struct InvalidIssueString {
94 pub cause: Option<InvalidIssueStringCause>,
97 // The error kinds of `IntErrorKind` are duplicated here in order to allow the messages to be
99 #[derive(SessionSubdiagnostic)]
100 pub(crate) enum InvalidIssueStringCause {
101 #[label(attr::must_not_be_zero)]
107 #[label(attr::empty)]
113 #[label(attr::invalid_digit)]
119 #[label(attr::pos_overflow)]
125 #[label(attr::neg_overflow)]
132 impl InvalidIssueStringCause {
133 pub fn from_int_error_kind(span: Span, kind: &IntErrorKind) -> Option<Self> {
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 }),
145 #[derive(SessionDiagnostic)]
146 #[diag(attr::missing_feature, code = "E0546")]
147 pub(crate) struct MissingFeature {
152 #[derive(SessionDiagnostic)]
153 #[diag(attr::non_ident_feature, code = "E0546")]
154 pub(crate) struct NonIdentFeature {
159 #[derive(SessionDiagnostic)]
160 #[diag(attr::missing_issue, code = "E0547")]
161 pub(crate) struct MissingIssue {
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 {
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 {
184 #[derive(SessionDiagnostic)]
185 #[diag(attr::invalid_repr_hint_no_paren, code = "E0552")]
186 pub(crate) struct InvalidReprHintNoParen {
193 #[derive(SessionDiagnostic)]
194 #[diag(attr::invalid_repr_hint_no_value, code = "E0552")]
195 pub(crate) struct InvalidReprHintNoValue {
203 pub(crate) struct UnsupportedLiteral {
205 pub reason: UnsupportedLiteralReason,
206 pub is_bytestr: bool,
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(
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
219 UnsupportedLiteralReason::DeprecatedKvPair => {
220 fluent::attr::unsupported_literal_deprecated_kv_pair
226 diag.span_suggestion(
227 sess.source_map().start_point(self.span),
228 fluent::attr::unsupported_literal_suggestion,
230 Applicability::MaybeIncorrect,
237 #[derive(SessionDiagnostic)]
238 #[diag(attr::invalid_repr_align_need_arg, code = "E0589")]
239 pub(crate) struct InvalidReprAlignNeedArg {
241 #[suggestion(code = "align(...)", applicability = "has-placeholders")]
245 #[derive(SessionDiagnostic)]
246 #[diag(attr::invalid_repr_generic, code = "E0589")]
247 pub(crate) struct InvalidReprGeneric<'a> {
251 pub repr_arg: String,
252 pub error_part: &'a str,
255 #[derive(SessionDiagnostic)]
256 #[diag(attr::incorrect_repr_format_align_one_arg, code = "E0693")]
257 pub(crate) struct IncorrectReprFormatAlignOneArg {
262 #[derive(SessionDiagnostic)]
263 #[diag(attr::incorrect_repr_format_generic, code = "E0693")]
264 pub(crate) struct IncorrectReprFormatGeneric<'a> {
268 pub repr_arg: &'a str,
271 pub cause: Option<IncorrectReprFormatGenericCause<'a>>,
274 #[derive(SessionSubdiagnostic)]
275 pub(crate) enum IncorrectReprFormatGenericCause<'a> {
276 #[suggestion(attr::suggestion, code = "{name}({int})", applicability = "machine-applicable")]
290 code = "{name}({symbol})",
291 applicability = "machine-applicable"
305 impl<'a> IncorrectReprFormatGenericCause<'a> {
306 pub fn from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self> {
308 ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
309 Some(Self::Int { span, name, int: *int })
311 ast::LitKind::Str(symbol, _) => Some(Self::Symbol { span, name, symbol: *symbol }),
317 #[derive(SessionDiagnostic)]
318 #[diag(attr::rustc_promotable_pairing, code = "E0717")]
319 pub(crate) struct RustcPromotablePairing {
324 #[derive(SessionDiagnostic)]
325 #[diag(attr::rustc_allowed_unstable_pairing, code = "E0789")]
326 pub(crate) struct RustcAllowedUnstablePairing {
331 #[derive(SessionDiagnostic)]
332 #[diag(attr::cfg_predicate_identifier)]
333 pub(crate) struct CfgPredicateIdentifier {
338 #[derive(SessionDiagnostic)]
339 #[diag(attr::deprecated_item_suggestion)]
340 pub(crate) struct DeprecatedItemSuggestion {
345 pub is_nightly: Option<()>,
351 #[derive(SessionDiagnostic)]
352 #[diag(attr::expected_single_version_literal)]
353 pub(crate) struct ExpectedSingleVersionLiteral {
358 #[derive(SessionDiagnostic)]
359 #[diag(attr::expected_version_literal)]
360 pub(crate) struct ExpectedVersionLiteral {
365 #[derive(SessionDiagnostic)]
366 #[diag(attr::expects_feature_list)]
367 pub(crate) struct ExpectsFeatureList {
374 #[derive(SessionDiagnostic)]
375 #[diag(attr::expects_features)]
376 pub(crate) struct ExpectsFeatures {
383 #[derive(SessionDiagnostic)]
384 #[diag(attr::soft_no_args)]
385 pub(crate) struct SoftNoArgs {
390 #[derive(SessionDiagnostic)]
391 #[diag(attr::unknown_version_literal)]
392 pub(crate) struct UnknownVersionLiteral {