1 use std::num::IntErrorKind;
5 error_code, fluent, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic,
7 use rustc_macros::Diagnostic;
8 use rustc_span::{Span, Symbol};
10 use crate::UnsupportedLiteralReason;
13 #[diag(attr_expected_one_cfg_pattern, code = "E0536")]
14 pub(crate) struct ExpectedOneCfgPattern {
20 #[diag(attr_invalid_predicate, code = "E0537")]
21 pub(crate) struct InvalidPredicate {
25 pub predicate: String,
29 #[diag(attr_multiple_item, code = "E0538")]
30 pub(crate) struct MultipleItem {
38 #[diag(attr_incorrect_meta_item, code = "E0539")]
39 pub(crate) struct IncorrectMetaItem {
45 pub(crate) struct UnknownMetaItem<'a> {
48 pub expected: &'a [&'a str],
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(
57 fluent::attr_unknown_meta_item,
60 diag.set_arg("item", self.item);
61 diag.set_arg("expected", expected.join(", "));
62 diag.span_label(self.span, fluent::label);
68 #[diag(attr_missing_since, code = "E0542")]
69 pub(crate) struct MissingSince {
75 #[diag(attr_missing_note, code = "E0543")]
76 pub(crate) struct MissingNote {
82 #[diag(attr_multiple_stability_levels, code = "E0544")]
83 pub(crate) struct MultipleStabilityLevels {
89 #[diag(attr_invalid_issue_string, code = "E0545")]
90 pub(crate) struct InvalidIssueString {
95 pub cause: Option<InvalidIssueStringCause>,
98 // The error kinds of `IntErrorKind` are duplicated here in order to allow the messages to be
100 #[derive(Subdiagnostic)]
101 pub(crate) enum InvalidIssueStringCause {
102 #[label(must_not_be_zero)]
114 #[label(invalid_digit)]
120 #[label(pos_overflow)]
126 #[label(neg_overflow)]
133 impl InvalidIssueStringCause {
134 pub fn from_int_error_kind(span: Span, kind: &IntErrorKind) -> Option<Self> {
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 }),
146 #[derive(Diagnostic)]
147 #[diag(attr_missing_feature, code = "E0546")]
148 pub(crate) struct MissingFeature {
153 #[derive(Diagnostic)]
154 #[diag(attr_non_ident_feature, code = "E0546")]
155 pub(crate) struct NonIdentFeature {
160 #[derive(Diagnostic)]
161 #[diag(attr_missing_issue, code = "E0547")]
162 pub(crate) struct MissingIssue {
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 {
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 {
185 #[derive(Diagnostic)]
186 #[diag(attr_invalid_repr_hint_no_paren, code = "E0552")]
187 pub(crate) struct InvalidReprHintNoParen {
194 #[derive(Diagnostic)]
195 #[diag(attr_invalid_repr_hint_no_value, code = "E0552")]
196 pub(crate) struct InvalidReprHintNoValue {
203 /// Error code: E0565
204 pub(crate) struct UnsupportedLiteral {
206 pub reason: UnsupportedLiteralReason,
207 pub is_bytestr: bool,
208 pub start_point_span: Span,
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(
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
221 UnsupportedLiteralReason::DeprecatedKvPair => {
222 fluent::attr_unsupported_literal_deprecated_kv_pair
228 diag.span_suggestion(
229 self.start_point_span,
230 fluent::attr_unsupported_literal_suggestion,
232 Applicability::MaybeIncorrect,
239 #[derive(Diagnostic)]
240 #[diag(attr_invalid_repr_align_need_arg, code = "E0589")]
241 pub(crate) struct InvalidReprAlignNeedArg {
243 #[suggestion(code = "align(...)", applicability = "has-placeholders")]
247 #[derive(Diagnostic)]
248 #[diag(attr_invalid_repr_generic, code = "E0589")]
249 pub(crate) struct InvalidReprGeneric<'a> {
253 pub repr_arg: String,
254 pub error_part: &'a str,
257 #[derive(Diagnostic)]
258 #[diag(attr_incorrect_repr_format_align_one_arg, code = "E0693")]
259 pub(crate) struct IncorrectReprFormatAlignOneArg {
264 #[derive(Diagnostic)]
265 #[diag(attr_incorrect_repr_format_generic, code = "E0693")]
266 pub(crate) struct IncorrectReprFormatGeneric<'a> {
270 pub repr_arg: &'a str,
273 pub cause: Option<IncorrectReprFormatGenericCause<'a>>,
276 #[derive(Subdiagnostic)]
277 pub(crate) enum IncorrectReprFormatGenericCause<'a> {
278 #[suggestion(suggestion, code = "{name}({int})", applicability = "machine-applicable")]
290 #[suggestion(suggestion, code = "{name}({symbol})", applicability = "machine-applicable")]
303 impl<'a> IncorrectReprFormatGenericCause<'a> {
304 pub fn from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self> {
306 ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
307 Some(Self::Int { span, name, int: *int })
309 ast::LitKind::Str(symbol, _) => Some(Self::Symbol { span, name, symbol: *symbol }),
315 #[derive(Diagnostic)]
316 #[diag(attr_rustc_promotable_pairing, code = "E0717")]
317 pub(crate) struct RustcPromotablePairing {
322 #[derive(Diagnostic)]
323 #[diag(attr_rustc_allowed_unstable_pairing, code = "E0789")]
324 pub(crate) struct RustcAllowedUnstablePairing {
329 #[derive(Diagnostic)]
330 #[diag(attr_cfg_predicate_identifier)]
331 pub(crate) struct CfgPredicateIdentifier {
336 #[derive(Diagnostic)]
337 #[diag(attr_deprecated_item_suggestion)]
338 pub(crate) struct DeprecatedItemSuggestion {
343 pub is_nightly: Option<()>,
349 #[derive(Diagnostic)]
350 #[diag(attr_expected_single_version_literal)]
351 pub(crate) struct ExpectedSingleVersionLiteral {
356 #[derive(Diagnostic)]
357 #[diag(attr_expected_version_literal)]
358 pub(crate) struct ExpectedVersionLiteral {
363 #[derive(Diagnostic)]
364 #[diag(attr_expects_feature_list)]
365 pub(crate) struct ExpectsFeatureList {
372 #[derive(Diagnostic)]
373 #[diag(attr_expects_features)]
374 pub(crate) struct ExpectsFeatures {
381 #[derive(Diagnostic)]
382 #[diag(attr_soft_no_args)]
383 pub(crate) struct SoftNoArgs {
388 #[derive(Diagnostic)]
389 #[diag(attr_unknown_version_literal)]
390 pub(crate) struct UnknownVersionLiteral {