Skip to main content

rustc_attr_parsing/attributes/diagnostic/
mod.rs

1use std::ops::Range;
2
3use rustc_errors::E0232;
4use rustc_hir::AttrPath;
5use rustc_hir::attrs::diagnostic::{
6    Directive, FilterFormatString, Flag, FormatArg, FormatString, LitOrArg, Name, NameValue,
7    OnUnimplementedCondition, Piece, Predicate,
8};
9use rustc_macros::Diagnostic;
10use rustc_parse_format::{
11    Argument, FormatSpec, ParseError, ParseMode, Parser, Piece as RpfPiece, Position,
12};
13use rustc_session::lint::builtin::{
14    MALFORMED_DIAGNOSTIC_ATTRIBUTES, MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
15};
16use rustc_span::{Ident, InnerSpan, Span, Symbol, kw, sym};
17use thin_vec::{ThinVec, thin_vec};
18
19use crate::context::AcceptContext;
20use crate::errors::{
21    FormatWarning, IgnoredDiagnosticOption, MalFormedDiagnosticAttributeLint,
22    MissingOptionsForDiagnosticAttribute, NonMetaItemDiagnosticAttribute, WrappedParserError,
23};
24use crate::parser::{ArgParser, MetaItemListParser, MetaItemOrLitParser, MetaItemParser};
25
26pub(crate) mod check_cfg;
27pub(crate) mod do_not_recommend;
28pub(crate) mod on_const;
29pub(crate) mod on_move;
30pub(crate) mod on_unimplemented;
31pub(crate) mod on_unknown;
32pub(crate) mod on_unmatch_args;
33
34#[derive(#[automatically_derived]
impl ::core::marker::Copy for Mode { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Mode {
    #[inline]
    fn clone(&self) -> Mode { *self }
}Clone)]
35pub(crate) enum Mode {
36    /// `#[rustc_on_unimplemented]`
37    RustcOnUnimplemented,
38    /// `#[diagnostic::on_unimplemented]`
39    DiagnosticOnUnimplemented,
40    /// `#[diagnostic::on_const]`
41    DiagnosticOnConst,
42    /// `#[diagnostic::on_move]`
43    DiagnosticOnMove,
44    /// `#[diagnostic::on_unknown]`
45    DiagnosticOnUnknown,
46    /// `#[diagnostic::on_unmatch_args]`
47    DiagnosticOnUnmatchArgs,
48}
49
50impl Mode {
51    fn as_str(&self) -> &'static str {
52        match self {
53            Self::RustcOnUnimplemented => "rustc_on_unimplemented",
54            Self::DiagnosticOnUnimplemented => "diagnostic::on_unimplemented",
55            Self::DiagnosticOnConst => "diagnostic::on_const",
56            Self::DiagnosticOnMove => "diagnostic::on_move",
57            Self::DiagnosticOnUnknown => "diagnostic::on_unknown",
58            Self::DiagnosticOnUnmatchArgs => "diagnostic::on_unmatch_args",
59        }
60    }
61
62    fn expected_options(&self) -> &'static str {
63        const DEFAULT: &str =
64            "at least one of the `message`, `note` and `label` options are expected";
65        match self {
66            Self::RustcOnUnimplemented => {
67                "see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>"
68            }
69            Self::DiagnosticOnUnimplemented => DEFAULT,
70            Self::DiagnosticOnConst => DEFAULT,
71            Self::DiagnosticOnMove => DEFAULT,
72            Self::DiagnosticOnUnknown => DEFAULT,
73            Self::DiagnosticOnUnmatchArgs => DEFAULT,
74        }
75    }
76
77    fn allowed_options(&self) -> &'static str {
78        const DEFAULT: &str = "only `message`, `note` and `label` are allowed as options";
79        match self {
80            Self::RustcOnUnimplemented => {
81                "see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented>"
82            }
83            Self::DiagnosticOnUnimplemented => DEFAULT,
84            Self::DiagnosticOnConst => DEFAULT,
85            Self::DiagnosticOnMove => DEFAULT,
86            Self::DiagnosticOnUnknown => DEFAULT,
87            Self::DiagnosticOnUnmatchArgs => DEFAULT,
88        }
89    }
90
91    fn allowed_format_arguments(&self) -> &'static str {
92        match self {
93            Self::RustcOnUnimplemented => {
94                "see <https://rustc-dev-guide.rust-lang.org/diagnostics.html#rustc_on_unimplemented> for allowed format arguments"
95            }
96            Self::DiagnosticOnUnimplemented => {
97                "only `Self` and generics of the trait are allowed as a format argument"
98            }
99            Self::DiagnosticOnConst => {
100                "only `Self` and generics of the implementation are allowed as a format argument"
101            }
102            Self::DiagnosticOnMove => {
103                "only `This`, `Self` and generics of the type are allowed as a format argument"
104            }
105            Self::DiagnosticOnUnknown => {
106                "only `This` is allowed as a format argument, referring to the failed import"
107            }
108            Self::DiagnosticOnUnmatchArgs => {
109                "only `This` is allowed as a format argument, referring to the macro's name"
110            }
111        }
112    }
113}
114
115fn merge_directives(
116    cx: &mut AcceptContext<'_, '_>,
117    first: &mut Option<(Span, Directive)>,
118    later: (Span, Directive),
119) {
120    if let Some((_, first)) = first {
121        if first.is_rustc_attr || later.1.is_rustc_attr {
122            cx.emit_err(DupesNotAllowed);
123        }
124
125        merge(cx, &mut first.message, later.1.message, sym::message);
126        merge(cx, &mut first.label, later.1.label, sym::label);
127        first.notes.extend(later.1.notes);
128    } else {
129        *first = Some(later);
130    }
131}
132
133fn merge<T>(
134    cx: &mut AcceptContext<'_, '_>,
135    first: &mut Option<(Span, T)>,
136    later: Option<(Span, T)>,
137    option_name: Symbol,
138) {
139    match (first, later) {
140        (Some(_) | None, None) => {}
141        (Some((first_span, _)), Some((later_span, _))) => {
142            let first_span = *first_span;
143            cx.emit_lint(
144                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
145                IgnoredDiagnosticOption { first_span, later_span, option_name },
146                later_span,
147            );
148        }
149        (first @ None, Some(later)) => {
150            first.get_or_insert(later);
151        }
152    }
153}
154
155fn parse_list<'p>(
156    cx: &mut AcceptContext<'_, '_>,
157    args: &'p ArgParser,
158    mode: Mode,
159) -> Option<&'p MetaItemListParser> {
160    let span = cx.attr_span;
161    match args {
162        ArgParser::List(items) if items.len() != 0 => return Some(items),
163        ArgParser::List(list) => {
164            // We're dealing with `#[diagnostic::attr()]`.
165            // This can be because that is what the user typed, but that's also what we'd see
166            // if the user used non-metaitem syntax. See `ArgParser::from_attr_args`.
167            cx.emit_lint(
168                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
169                NonMetaItemDiagnosticAttribute,
170                list.span,
171            );
172        }
173        ArgParser::NoArgs => {
174            cx.emit_lint(
175                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
176                MissingOptionsForDiagnosticAttribute {
177                    attribute: mode.as_str(),
178                    options: mode.expected_options(),
179                },
180                span,
181            );
182        }
183        ArgParser::NameValue(_) => {
184            cx.emit_lint(
185                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
186                MalFormedDiagnosticAttributeLint {
187                    attribute: mode.as_str(),
188                    options: mode.allowed_options(),
189                    span,
190                },
191                span,
192            );
193        }
194    }
195    None
196}
197
198fn parse_directive_items<'p>(
199    cx: &mut AcceptContext<'_, '_>,
200    mode: Mode,
201    items: impl Iterator<Item = &'p MetaItemOrLitParser>,
202    is_root: bool,
203) -> Option<Directive> {
204    let condition = None;
205    let mut message: Option<(Span, _)> = None;
206    let mut label: Option<(Span, _)> = None;
207    let mut notes = ThinVec::new();
208    let mut parent_label = None;
209    let mut subcommands = ThinVec::new();
210
211    for item in items {
212        let span = item.span();
213
214        macro malformed() {{
215            cx.emit_lint(
216                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
217                MalFormedDiagnosticAttributeLint {
218                    attribute: mode.as_str(),
219                    options: mode.allowed_options(),
220                    span,
221                },
222                span,
223            );
224            continue;
225        }}
226
227        macro or_malformed($($code:tt)*) {{
228            let Some(ret) = (
229                try {
230                    $($code)*
231                }
232            ) else {
233                malformed!()
234            };
235            ret
236        }}
237
238        macro duplicate($name: ident, $($first_span:tt)*) {{
239            let first_span = $($first_span)*;
240            cx.emit_lint(
241                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
242                IgnoredDiagnosticOption {
243                    first_span,
244                    later_span: span,
245                    option_name: $name,
246                },
247                span,
248            );
249        }}
250
251        let item: &MetaItemParser = {
    let Some(ret) =
        (try {
                item.meta_item()?
            }) else {
            {
                cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
                    MalFormedDiagnosticAttributeLint {
                        attribute: mode.as_str(),
                        options: mode.allowed_options(),
                        span,
                    }, span);
                continue;
            }
        };
    ret
}or_malformed!(item.meta_item()?);
252        let name = {
    let Some(ret) =
        (try {
                item.ident()?
            }) else {
            {
                cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
                    MalFormedDiagnosticAttributeLint {
                        attribute: mode.as_str(),
                        options: mode.allowed_options(),
                        span,
                    }, span);
                continue;
            }
        };
    ret
}or_malformed!(item.ident()?).name;
253
254        // Currently, as of April 2026, all arguments of all diagnostic attrs
255        // must have a value, like `message = "message"`. Thus in a well-formed
256        // diagnostic attribute this is never `None`.
257        //
258        // But we don't assert its presence yet because we don't want to mention it
259        // if someone does something like `#[diagnostic::on_unimplemented(doesnt_exist)]`.
260        // That happens in the big `match` below.
261        let value: Option<Ident> = match item.args().as_name_value() {
262            Some(nv) => Some({
    let Some(ret) =
        (try {
                nv.value_as_ident()?
            }) else {
            {
                cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
                    MalFormedDiagnosticAttributeLint {
                        attribute: mode.as_str(),
                        options: mode.allowed_options(),
                        span,
                    }, span);
                continue;
            }
        };
    ret
}or_malformed!(nv.value_as_ident()?)),
263            None => None,
264        };
265
266        let mut parse_format = |input: Ident| {
267            let snippet = cx.sess.source_map().span_to_snippet(input.span).ok();
268            let is_snippet = snippet.is_some();
269            match parse_format_string(input.name, snippet, input.span, mode) {
270                Ok((f, warnings)) => {
271                    for warning in warnings {
272                        let (FormatWarning::InvalidSpecifier { span }
273                        | FormatWarning::PositionalArgument { span }
274                        | FormatWarning::IndexedArgument { span }
275                        | FormatWarning::DisallowedPlaceholder { span, .. }) = warning;
276                        cx.emit_lint(MALFORMED_DIAGNOSTIC_FORMAT_LITERALS, warning, span);
277                    }
278
279                    f
280                }
281                Err(e) => {
282                    cx.emit_lint(
283                        MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
284                        WrappedParserError {
285                            description: e.description,
286                            label: e.label,
287                            span: slice_span(input.span, e.span.clone(), is_snippet),
288                        },
289                        input.span,
290                    );
291                    // We could not parse the input, just use it as-is.
292                    FormatString {
293                        input: input.name,
294                        span: input.span,
295                        pieces: {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(Piece::Lit(input.name));
    vec
}thin_vec![Piece::Lit(input.name)],
296                    }
297                }
298            }
299        };
300        match (mode, name) {
301            (_, sym::message) => {
302                let value = {
    let Some(ret) =
        (try {
                value?
            }) else {
            {
                cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
                    MalFormedDiagnosticAttributeLint {
                        attribute: mode.as_str(),
                        options: mode.allowed_options(),
                        span,
                    }, span);
                continue;
            }
        };
    ret
}or_malformed!(value?);
303                if let Some(message) = &message {
304                    {
    let first_span = message.0;
    cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
        IgnoredDiagnosticOption {
            first_span,
            later_span: span,
            option_name: name,
        }, span);
}duplicate!(name, message.0)
305                } else {
306                    message = Some((item.span(), parse_format(value)));
307                }
308            }
309            (_, sym::label) => {
310                let value = {
    let Some(ret) =
        (try {
                value?
            }) else {
            {
                cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
                    MalFormedDiagnosticAttributeLint {
                        attribute: mode.as_str(),
                        options: mode.allowed_options(),
                        span,
                    }, span);
                continue;
            }
        };
    ret
}or_malformed!(value?);
311                if let Some(label) = &label {
312                    {
    let first_span = label.0;
    cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
        IgnoredDiagnosticOption {
            first_span,
            later_span: span,
            option_name: name,
        }, span);
}duplicate!(name, label.0)
313                } else {
314                    label = Some((item.span(), parse_format(value)));
315                }
316            }
317            (_, sym::note) => {
318                let value = {
    let Some(ret) =
        (try {
                value?
            }) else {
            {
                cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
                    MalFormedDiagnosticAttributeLint {
                        attribute: mode.as_str(),
                        options: mode.allowed_options(),
                        span,
                    }, span);
                continue;
            }
        };
    ret
}or_malformed!(value?);
319                notes.push(parse_format(value))
320            }
321            (Mode::RustcOnUnimplemented, sym::parent_label) => {
322                let value = {
    let Some(ret) =
        (try {
                value?
            }) else {
            {
                cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
                    MalFormedDiagnosticAttributeLint {
                        attribute: mode.as_str(),
                        options: mode.allowed_options(),
                        span,
                    }, span);
                continue;
            }
        };
    ret
}or_malformed!(value?);
323                if parent_label.is_none() {
324                    parent_label = Some(parse_format(value));
325                } else {
326                    {
    let first_span = span;
    cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
        IgnoredDiagnosticOption {
            first_span,
            later_span: span,
            option_name: name,
        }, span);
}duplicate!(name, span)
327                }
328            }
329            (Mode::RustcOnUnimplemented, sym::on) => {
330                if is_root {
331                    let items = {
    let Some(ret) =
        (try {
                item.args().as_list()?
            }) else {
            {
                cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
                    MalFormedDiagnosticAttributeLint {
                        attribute: mode.as_str(),
                        options: mode.allowed_options(),
                        span,
                    }, span);
                continue;
            }
        };
    ret
}or_malformed!(item.args().as_list()?);
332                    let mut iter = items.mixed();
333                    let condition: &MetaItemOrLitParser = match iter.next() {
334                        Some(c) => c,
335                        None => {
336                            cx.emit_err(InvalidOnClause::Empty { span });
337                            continue;
338                        }
339                    };
340
341                    let condition = parse_condition(condition);
342
343                    if items.len() < 2 {
344                        // Something like `#[rustc_on_unimplemented(on(.., /* nothing */))]`
345                        // There's a condition but no directive behind it, this is a mistake.
346                        {
    cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
        MalFormedDiagnosticAttributeLint {
            attribute: mode.as_str(),
            options: mode.allowed_options(),
            span,
        }, span);
    continue;
};malformed!();
347                    }
348
349                    let mut directive =
350                        {
    let Some(ret) =
        (try {
                parse_directive_items(cx, mode, iter, false)?
            }) else {
            {
                cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
                    MalFormedDiagnosticAttributeLint {
                        attribute: mode.as_str(),
                        options: mode.allowed_options(),
                        span,
                    }, span);
                continue;
            }
        };
    ret
}or_malformed!(parse_directive_items(cx, mode, iter, false)?);
351
352                    match condition {
353                        Ok(c) => {
354                            directive.condition = Some(c);
355                            subcommands.push(directive);
356                        }
357                        Err(e) => {
358                            cx.emit_err(e);
359                        }
360                    }
361                } else {
362                    {
    cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
        MalFormedDiagnosticAttributeLint {
            attribute: mode.as_str(),
            options: mode.allowed_options(),
            span,
        }, span);
    continue;
};malformed!();
363                }
364            }
365
366            _other => {
367                {
    cx.emit_lint(MALFORMED_DIAGNOSTIC_ATTRIBUTES,
        MalFormedDiagnosticAttributeLint {
            attribute: mode.as_str(),
            options: mode.allowed_options(),
            span,
        }, span);
    continue;
};malformed!();
368            }
369        }
370    }
371
372    Some(Directive {
373        is_rustc_attr: #[allow(non_exhaustive_omitted_patterns)] match mode {
    Mode::RustcOnUnimplemented => true,
    _ => false,
}matches!(mode, Mode::RustcOnUnimplemented),
374        condition,
375        subcommands,
376        message,
377        label,
378        notes,
379        parent_label,
380    })
381}
382
383pub(crate) fn parse_format_string(
384    input: Symbol,
385    snippet: Option<String>,
386    span: Span,
387    mode: Mode,
388) -> Result<(FormatString, Vec<FormatWarning>), ParseError> {
389    let s = input.as_str();
390    let mut parser = Parser::new(s, None, snippet, false, ParseMode::Diagnostic);
391    let pieces: Vec<_> = parser.by_ref().collect();
392
393    if let Some(err) = parser.errors.into_iter().next() {
394        return Err(err);
395    }
396    let mut warnings = Vec::new();
397
398    let pieces = pieces
399        .into_iter()
400        .map(|piece| match piece {
401            RpfPiece::Lit(lit) => Piece::Lit(Symbol::intern(lit)),
402            RpfPiece::NextArgument(arg) => {
403                warn_on_format_spec(&arg.format, &mut warnings, span, parser.is_source_literal);
404                let arg = parse_arg(&arg, mode, &mut warnings, span, parser.is_source_literal);
405                Piece::Arg(arg)
406            }
407        })
408        .collect();
409
410    Ok((FormatString { input, pieces, span }, warnings))
411}
412
413fn parse_arg(
414    arg: &Argument<'_>,
415    mode: Mode,
416    warnings: &mut Vec<FormatWarning>,
417    input_span: Span,
418    is_source_literal: bool,
419) -> FormatArg {
420    let span = slice_span(input_span, arg.position_span.clone(), is_source_literal);
421
422    match arg.position {
423        // Something like "hello {name}"
424        Position::ArgumentNamed(name) => match (mode, Symbol::intern(name)) {
425            (Mode::RustcOnUnimplemented, sym::ItemContext) => FormatArg::ItemContext,
426
427            // Like `{This}`, but sugared.
428            // FIXME(mejrs) maybe rename/rework this or something
429            // if we want to apply this to other attrs?
430            (Mode::RustcOnUnimplemented, sym::Trait) => FormatArg::Trait,
431
432            // Some diagnostic attributes can use `{This}` to refer to the annotated item.
433            // For those that don't, we continue and maybe use it as a generic parameter.
434            //
435            // FIXME(mejrs) `DiagnosticOnUnimplemented` is intentionally not here;
436            // that requires lang approval which is best kept for a standalone PR.
437            (
438                Mode::RustcOnUnimplemented
439                | Mode::DiagnosticOnUnknown
440                | Mode::DiagnosticOnMove
441                | Mode::DiagnosticOnUnmatchArgs,
442                sym::This,
443            ) => FormatArg::This,
444
445            // `{Self}`; the self type.
446            // - For trait declaration attributes that's the type that does not implement it.
447            // - for trait impl attributes, the implemented for type.
448            // - For ADT attributes, that's the type (which will be identical to `{This}`)
449            // - For everything else it doesn't make sense.
450            (
451                Mode::RustcOnUnimplemented
452                | Mode::DiagnosticOnUnimplemented
453                | Mode::DiagnosticOnMove
454                | Mode::DiagnosticOnConst,
455                kw::SelfUpper,
456            ) => FormatArg::SelfUpper,
457
458            // Generic parameters.
459            // FIXME(mejrs) unfortunately, all the "special" symbols above might fall through,
460            // but at this time we are not aware of what generic parameters the trait actually has.
461            // If we find `ItemContext` or something we have to assume that's a generic parameter.
462            // We lint against that in `check_attr.rs` though.
463            (
464                Mode::RustcOnUnimplemented
465                | Mode::DiagnosticOnUnimplemented
466                | Mode::DiagnosticOnMove
467                | Mode::DiagnosticOnConst,
468                generic_param,
469            ) => FormatArg::GenericParam { generic_param, span },
470
471            // Generics are explicitly not allowed, we print those back as is.
472            (Mode::DiagnosticOnUnknown | Mode::DiagnosticOnUnmatchArgs, as_is) => {
473                warnings.push(FormatWarning::DisallowedPlaceholder {
474                    span,
475                    attr: mode.as_str(),
476                    allowed: mode.allowed_format_arguments(),
477                });
478                return FormatArg::AsIs(Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{{{0}}}", as_is))
    })format!("{{{as_is}}}")));
479            }
480        },
481
482        // `{:1}` and `{}` are ignored
483        Position::ArgumentIs(idx) => {
484            warnings.push(FormatWarning::IndexedArgument { span });
485            FormatArg::AsIs(Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{{{0}}}", idx))
    })format!("{{{idx}}}")))
486        }
487        Position::ArgumentImplicitlyIs(_) => {
488            warnings.push(FormatWarning::PositionalArgument { span });
489            FormatArg::AsIs(sym::empty_braces)
490        }
491    }
492}
493
494/// `#[rustc_on_unimplemented]` and `#[diagnostic::...]` don't actually do anything
495/// with specifiers, so emit a warning if they are used.
496fn warn_on_format_spec(
497    spec: &FormatSpec<'_>,
498    warnings: &mut Vec<FormatWarning>,
499    input_span: Span,
500    is_source_literal: bool,
501) {
502    if spec.ty != "" {
503        let span = spec
504            .ty_span
505            .as_ref()
506            .map(|inner| slice_span(input_span, inner.clone(), is_source_literal))
507            .unwrap_or(input_span);
508        warnings.push(FormatWarning::InvalidSpecifier { span })
509    }
510}
511
512fn slice_span(input: Span, Range { start, end }: Range<usize>, is_source_literal: bool) -> Span {
513    if is_source_literal { input.from_inner(InnerSpan { start, end }) } else { input }
514}
515
516pub(crate) fn parse_condition(
517    input: &MetaItemOrLitParser,
518) -> Result<OnUnimplementedCondition, InvalidOnClause> {
519    let span = input.span();
520    let pred = parse_predicate(input)?;
521    Ok(OnUnimplementedCondition { span, pred })
522}
523
524fn parse_predicate(input: &MetaItemOrLitParser) -> Result<Predicate, InvalidOnClause> {
525    let Some(meta_item) = input.meta_item() else {
526        return Err(InvalidOnClause::UnsupportedLiteral { span: input.span() });
527    };
528
529    let Some(predicate) = meta_item.ident() else {
530        return Err(InvalidOnClause::ExpectedIdentifier {
531            span: meta_item.path().span(),
532            path: meta_item.path().get_attribute_path(),
533        });
534    };
535
536    match meta_item.args() {
537        ArgParser::List(mis) => match predicate.name {
538            sym::any => Ok(Predicate::Any(parse_predicate_sequence(mis)?)),
539            sym::all => Ok(Predicate::All(parse_predicate_sequence(mis)?)),
540            sym::not => {
541                if let Some(single) = mis.as_single() {
542                    Ok(Predicate::Not(Box::new(parse_predicate(single)?)))
543                } else {
544                    Err(InvalidOnClause::ExpectedOnePredInNot { span: mis.span })
545                }
546            }
547            invalid_pred => {
548                Err(InvalidOnClause::InvalidPredicate { span: predicate.span, invalid_pred })
549            }
550        },
551        ArgParser::NameValue(p) => {
552            let Some(value) = p.value_as_ident() else {
553                return Err(InvalidOnClause::UnsupportedLiteral { span: p.args_span() });
554            };
555            let name = parse_name(predicate.name);
556            let value = parse_filter(value.name);
557            let kv = NameValue { name, value };
558            Ok(Predicate::Match(kv))
559        }
560        ArgParser::NoArgs => {
561            let flag = parse_flag(predicate)?;
562            Ok(Predicate::Flag(flag))
563        }
564    }
565}
566
567fn parse_predicate_sequence(
568    sequence: &MetaItemListParser,
569) -> Result<ThinVec<Predicate>, InvalidOnClause> {
570    sequence.mixed().map(parse_predicate).collect()
571}
572
573fn parse_flag(Ident { name, span }: Ident) -> Result<Flag, InvalidOnClause> {
574    match name {
575        sym::crate_local => Ok(Flag::CrateLocal),
576        sym::direct => Ok(Flag::Direct),
577        sym::from_desugaring => Ok(Flag::FromDesugaring),
578        invalid_flag => Err(InvalidOnClause::InvalidFlag { invalid_flag, span }),
579    }
580}
581
582fn parse_name(name: Symbol) -> Name {
583    match name {
584        kw::SelfUpper => Name::SelfUpper,
585        sym::from_desugaring => Name::FromDesugaring,
586        sym::cause => Name::Cause,
587        generic => Name::GenericArg(generic),
588    }
589}
590
591fn parse_filter(input: Symbol) -> FilterFormatString {
592    let pieces = Parser::new(input.as_str(), None, None, false, ParseMode::Diagnostic)
593        .map(|p| match p {
594            RpfPiece::Lit(s) => LitOrArg::Lit(Symbol::intern(s)),
595            // We just ignore formatspecs here
596            RpfPiece::NextArgument(a) => match a.position {
597                // In `TypeErrCtxt::on_unimplemented_note` we substitute `"{integral}"` even
598                // if the integer type has been resolved, to allow targeting all integers.
599                // `"{integer}"` and `"{float}"` come from numerics that haven't been inferred yet,
600                // from the `Display` impl of `InferTy` to be precise.
601                // `"{union|enum|struct}"` is used as a special selector for ADTs.
602                //
603                // Don't try to format these later!
604                Position::ArgumentNamed(
605                    arg @ ("integer" | "integral" | "float" | "union" | "enum" | "struct"),
606                ) => LitOrArg::Lit(Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{{{0}}}", arg))
    })format!("{{{arg}}}"))),
607
608                Position::ArgumentNamed(arg) => LitOrArg::Arg(Symbol::intern(arg)),
609                Position::ArgumentImplicitlyIs(_) => LitOrArg::Lit(sym::empty_braces),
610                Position::ArgumentIs(idx) => LitOrArg::Lit(Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{{{0}}}", idx))
    })format!("{{{idx}}}"))),
611            },
612        })
613        .collect();
614    FilterFormatString { pieces }
615}
616
617#[derive(const _: () =
    {
        impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for
            InvalidOnClause where G: rustc_errors::EmissionGuarantee {
            #[track_caller]
            fn into_diag(self, dcx: rustc_errors::DiagCtxtHandle<'_sess>,
                level: rustc_errors::Level) -> rustc_errors::Diag<'_sess, G> {
                match self {
                    InvalidOnClause::Empty { span: __binding_0 } => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("empty `on`-clause in `#[rustc_on_unimplemented]`")));
                        diag.code(E0232);
                        ;
                        diag.span(__binding_0);
                        diag.span_label(__binding_0,
                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("empty `on`-clause here")));
                        diag
                    }
                    InvalidOnClause::ExpectedOnePredInNot { span: __binding_0 }
                        => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected a single predicate in `not(..)`")));
                        diag.code(E0232);
                        ;
                        diag.span(__binding_0);
                        diag.span_label(__binding_0,
                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("unexpected quantity of predicates here")));
                        diag
                    }
                    InvalidOnClause::UnsupportedLiteral { span: __binding_0 } =>
                        {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("literals inside `on`-clauses are not supported")));
                        diag.code(E0232);
                        ;
                        diag.span(__binding_0);
                        diag.span_label(__binding_0,
                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("unexpected literal here")));
                        diag
                    }
                    InvalidOnClause::ExpectedIdentifier {
                        span: __binding_0, path: __binding_1 } => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected an identifier inside this `on`-clause")));
                        diag.code(E0232);
                        ;
                        diag.arg("path", __binding_1);
                        diag.span(__binding_0);
                        diag.span_label(__binding_0,
                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected an identifier here, not `{$path}`")));
                        diag
                    }
                    InvalidOnClause::InvalidPredicate {
                        span: __binding_0, invalid_pred: __binding_1 } => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("this predicate is invalid")));
                        diag.code(E0232);
                        ;
                        diag.arg("invalid_pred", __binding_1);
                        diag.span(__binding_0);
                        diag.span_label(__binding_0,
                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected one of `any`, `all` or `not` here, not `{$invalid_pred}`")));
                        diag
                    }
                    InvalidOnClause::InvalidFlag {
                        span: __binding_0, invalid_flag: __binding_1 } => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("invalid flag in `on`-clause")));
                        diag.code(E0232);
                        ;
                        diag.arg("invalid_flag", __binding_1);
                        diag.span(__binding_0);
                        diag.span_label(__binding_0,
                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("expected one of the `crate_local`, `direct` or `from_desugaring` flags, not `{$invalid_flag}`")));
                        diag
                    }
                }
            }
        }
    };Diagnostic)]
618pub(crate) enum InvalidOnClause {
619    #[diag("empty `on`-clause in `#[rustc_on_unimplemented]`", code = E0232)]
620    Empty {
621        #[primary_span]
622        #[label("empty `on`-clause here")]
623        span: Span,
624    },
625    #[diag("expected a single predicate in `not(..)`", code = E0232)]
626    ExpectedOnePredInNot {
627        #[primary_span]
628        #[label("unexpected quantity of predicates here")]
629        span: Span,
630    },
631    #[diag("literals inside `on`-clauses are not supported", code = E0232)]
632    UnsupportedLiteral {
633        #[primary_span]
634        #[label("unexpected literal here")]
635        span: Span,
636    },
637    #[diag("expected an identifier inside this `on`-clause", code = E0232)]
638    ExpectedIdentifier {
639        #[primary_span]
640        #[label("expected an identifier here, not `{$path}`")]
641        span: Span,
642        path: AttrPath,
643    },
644    #[diag("this predicate is invalid", code = E0232)]
645    InvalidPredicate {
646        #[primary_span]
647        #[label("expected one of `any`, `all` or `not` here, not `{$invalid_pred}`")]
648        span: Span,
649        invalid_pred: Symbol,
650    },
651    #[diag("invalid flag in `on`-clause", code = E0232)]
652    InvalidFlag {
653        #[primary_span]
654        #[label(
655            "expected one of the `crate_local`, `direct` or `from_desugaring` flags, not `{$invalid_flag}`"
656        )]
657        span: Span,
658        invalid_flag: Symbol,
659    },
660}
661
662#[derive(const _: () =
    {
        impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for
            DupesNotAllowed where G: rustc_errors::EmissionGuarantee {
            #[track_caller]
            fn into_diag(self, dcx: rustc_errors::DiagCtxtHandle<'_sess>,
                level: rustc_errors::Level) -> rustc_errors::Diag<'_sess, G> {
                match self {
                    DupesNotAllowed => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("using multiple `rustc_on_unimplemented` (or mixing it with `diagnostic::on_unimplemented`) is not supported")));
                        ;
                        diag
                    }
                }
            }
        }
    };Diagnostic)]
663#[diag(
664    "using multiple `rustc_on_unimplemented` (or mixing it with `diagnostic::on_unimplemented`) is not supported"
665)]
666pub(crate) struct DupesNotAllowed;