Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_type_error.rs

1use rustc_attr_ir::AttributeKind;
2use rustc_span::sym;
3
4use crate::attributes::AttributeStability;
5use crate::attributes::diagnostic::*;
6use crate::attributes::prelude::*;
7use crate::target_checking::AllowedTargets;
8use crate::template;
9
10#[derive(#[automatically_derived]
impl ::core::default::Default for OnTypeErrorParser {
    #[inline]
    fn default() -> OnTypeErrorParser {
        OnTypeErrorParser {
            span: ::core::default::Default::default(),
            directive: ::core::default::Default::default(),
        }
    }
}Default)]
11pub(crate) struct OnTypeErrorParser {
12    span: Option<Span>,
13    directive: Option<(Span, Directive)>,
14}
15
16impl AttributeParser for OnTypeErrorParser {
17    const ATTRIBUTES: AcceptMapping<Self> = &[(
18        &[sym::diagnostic, sym::on_type_error],
19        crate::AttributeTemplate {
    word: false,
    list: Some(&[r#"note = "...""#]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &[r#"note = "...""#]),
20        AttributeStability::Stable, // Unstable, stability checked manually below
21        |this, cx, args| {
22            {
    if let Some(features) = cx.features_option() &&
            !features.diagnostic_on_type_error() {
        args.ignore_args();
        let nightly_build = cx.sess.is_nightly_build();
        let span = cx.attr_span;
        cx.emit_lint(rustc_lint_defs::builtin::UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
            crate::diagnostics::UnstableDiagnosticAttribute {
                feature: sym::diagnostic_on_type_error,
                nightly_build,
            }, span);
        return;
    }
};gate_diagnostic_attr!(diagnostic_on_type_error);
23
24            let span = cx.attr_span;
25            this.span = Some(span);
26            let mode = Mode::DiagnosticOnTypeError;
27            let Some(items) = parse_list(cx, args, mode) else { return };
28
29            if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
30                merge_directives(cx, &mut this.directive, (span, directive));
31            }
32        },
33    )];
34
35    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
36        Allow(Target::Enum),
37        Allow(Target::Struct),
38        Allow(Target::Union),
39    ]);
40
41    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
42        if let Some(span) = self.span {
43            Some(AttributeKind::OnTypeError {
44                span,
45                directive: self.directive.map(|d| Box::new(d.1)),
46            })
47        } else {
48            None
49        }
50    }
51}