1 use std::num::IntErrorKind;
5 error_code, fluent, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler,
7 use rustc_macros::SessionDiagnostic;
8 use rustc_session::SessionDiagnostic;
9 use rustc_span::{Span, Symbol};
11 use crate::UnsupportedLiteralReason;
13 #[derive(SessionDiagnostic)]
14 #[diag(attr::expected_one_cfg_pattern, code = "E0536")]
15 pub(crate) struct ExpectedOneCfgPattern {
20 #[derive(SessionDiagnostic)]
21 #[diag(attr::invalid_predicate, code = "E0537")]
22 pub(crate) struct InvalidPredicate {
26 pub predicate: String,
29 #[derive(SessionDiagnostic)]
30 #[diag(attr::multiple_item, code = "E0538")]
31 pub(crate) struct MultipleItem {
38 #[derive(SessionDiagnostic)]
39 #[diag(attr::incorrect_meta_item, code = "E0539")]
40 pub(crate) struct IncorrectMetaItem {
46 pub(crate) struct UnknownMetaItem<'a> {
49 pub expected: &'a [&'a str],
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(
58 fluent::attr::unknown_meta_item,
61 diag.set_arg("item", self.item);
62 diag.set_arg("expected", expected.join(", "));
63 diag.span_label(self.span, fluent::attr::label);
68 #[derive(SessionDiagnostic)]
69 #[diag(attr::missing_since, code = "E0542")]
70 pub(crate) struct MissingSince {
75 #[derive(SessionDiagnostic)]
76 #[diag(attr::missing_note, code = "E0543")]
77 pub(crate) struct MissingNote {
82 #[derive(SessionDiagnostic)]
83 #[diag(attr::multiple_stability_levels, code = "E0544")]
84 pub(crate) struct MultipleStabilityLevels {
89 #[derive(SessionDiagnostic)]
90 #[diag(attr::invalid_issue_string, code = "E0545")]
91 pub(crate) struct InvalidIssueString {
96 pub cause: Option<InvalidIssueStringCause>,
99 // The error kinds of `IntErrorKind` are duplicated here in order to allow the messages to be
101 #[derive(SessionSubdiagnostic)]
102 pub(crate) enum InvalidIssueStringCause {
103 #[label(attr::must_not_be_zero)]
109 #[label(attr::empty)]
115 #[label(attr::invalid_digit)]
121 #[label(attr::pos_overflow)]
127 #[label(attr::neg_overflow)]
134 impl InvalidIssueStringCause {
135 pub fn from_int_error_kind(span: Span, kind: &IntErrorKind) -> Option<Self> {
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 }),
147 #[derive(SessionDiagnostic)]
148 #[diag(attr::missing_feature, code = "E0546")]
149 pub(crate) struct MissingFeature {
154 #[derive(SessionDiagnostic)]
155 #[diag(attr::non_ident_feature, code = "E0546")]
156 pub(crate) struct NonIdentFeature {
161 #[derive(SessionDiagnostic)]
162 #[diag(attr::missing_issue, code = "E0547")]
163 pub(crate) struct MissingIssue {
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 {
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 {
186 #[derive(SessionDiagnostic)]
187 #[diag(attr::invalid_repr_hint_no_paren, code = "E0552")]
188 pub(crate) struct InvalidReprHintNoParen {
195 #[derive(SessionDiagnostic)]
196 #[diag(attr::invalid_repr_hint_no_value, code = "E0552")]
197 pub(crate) struct InvalidReprHintNoValue {
205 pub(crate) struct UnsupportedLiteral {
207 pub reason: UnsupportedLiteralReason,
208 pub is_bytestr: bool,
209 pub start_point_span: Span,
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(
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
222 UnsupportedLiteralReason::DeprecatedKvPair => {
223 fluent::attr::unsupported_literal_deprecated_kv_pair
229 diag.span_suggestion(
230 self.start_point_span,
231 fluent::attr::unsupported_literal_suggestion,
233 Applicability::MaybeIncorrect,
240 #[derive(SessionDiagnostic)]
241 #[diag(attr::invalid_repr_align_need_arg, code = "E0589")]
242 pub(crate) struct InvalidReprAlignNeedArg {
244 #[suggestion(code = "align(...)", applicability = "has-placeholders")]
248 #[derive(SessionDiagnostic)]
249 #[diag(attr::invalid_repr_generic, code = "E0589")]
250 pub(crate) struct InvalidReprGeneric<'a> {
254 pub repr_arg: String,
255 pub error_part: &'a str,
258 #[derive(SessionDiagnostic)]
259 #[diag(attr::incorrect_repr_format_align_one_arg, code = "E0693")]
260 pub(crate) struct IncorrectReprFormatAlignOneArg {
265 #[derive(SessionDiagnostic)]
266 #[diag(attr::incorrect_repr_format_generic, code = "E0693")]
267 pub(crate) struct IncorrectReprFormatGeneric<'a> {
271 pub repr_arg: &'a str,
274 pub cause: Option<IncorrectReprFormatGenericCause<'a>>,
277 #[derive(SessionSubdiagnostic)]
278 pub(crate) enum IncorrectReprFormatGenericCause<'a> {
279 #[suggestion(attr::suggestion, code = "{name}({int})", applicability = "machine-applicable")]
293 code = "{name}({symbol})",
294 applicability = "machine-applicable"
308 impl<'a> IncorrectReprFormatGenericCause<'a> {
309 pub fn from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self> {
311 ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
312 Some(Self::Int { span, name, int: *int })
314 ast::LitKind::Str(symbol, _) => Some(Self::Symbol { span, name, symbol: *symbol }),
320 #[derive(SessionDiagnostic)]
321 #[diag(attr::rustc_promotable_pairing, code = "E0717")]
322 pub(crate) struct RustcPromotablePairing {
327 #[derive(SessionDiagnostic)]
328 #[diag(attr::rustc_allowed_unstable_pairing, code = "E0789")]
329 pub(crate) struct RustcAllowedUnstablePairing {
334 #[derive(SessionDiagnostic)]
335 #[diag(attr::cfg_predicate_identifier)]
336 pub(crate) struct CfgPredicateIdentifier {
341 #[derive(SessionDiagnostic)]
342 #[diag(attr::deprecated_item_suggestion)]
343 pub(crate) struct DeprecatedItemSuggestion {
348 pub is_nightly: Option<()>,
354 #[derive(SessionDiagnostic)]
355 #[diag(attr::expected_single_version_literal)]
356 pub(crate) struct ExpectedSingleVersionLiteral {
361 #[derive(SessionDiagnostic)]
362 #[diag(attr::expected_version_literal)]
363 pub(crate) struct ExpectedVersionLiteral {
368 #[derive(SessionDiagnostic)]
369 #[diag(attr::expects_feature_list)]
370 pub(crate) struct ExpectsFeatureList {
377 #[derive(SessionDiagnostic)]
378 #[diag(attr::expects_features)]
379 pub(crate) struct ExpectsFeatures {
386 #[derive(SessionDiagnostic)]
387 #[diag(attr::soft_no_args)]
388 pub(crate) struct SoftNoArgs {
393 #[derive(SessionDiagnostic)]
394 #[diag(attr::unknown_version_literal)]
395 pub(crate) struct UnknownVersionLiteral {