Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_type_error.rs

1use rustc_hir::attrs::AttributeKind;
2use rustc_lint_defs::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES;
3use rustc_span::sym;
4
5use crate::attributes::AttributeStability;
6use crate::attributes::diagnostic::*;
7use crate::attributes::prelude::*;
8use crate::context::AcceptContext;
9use crate::diagnostics::DiagnosticOnTypeErrorOnlyForAdt;
10use crate::parser::ArgParser;
11use crate::target_checking::{ALL_TARGETS, AllowedTargets};
12use crate::template;
13
14#[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)]
15pub(crate) struct OnTypeErrorParser {
16    span: Option<Span>,
17    directive: Option<(Span, Directive)>,
18}
19
20impl OnTypeErrorParser {
21    fn parse<'sess>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
22        if !cx.features().diagnostic_on_type_error() {
23            return;
24        }
25
26        let span = cx.attr_span;
27        self.span = Some(span);
28
29        if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::Enum | Target::Struct | Target::Union => true,
    _ => false,
}matches!(cx.target, Target::Enum | Target::Struct | Target::Union) {
30            cx.emit_lint(MISPLACED_DIAGNOSTIC_ATTRIBUTES, DiagnosticOnTypeErrorOnlyForAdt, span);
31            return;
32        }
33
34        let Some(items) = parse_list(cx, args, mode) else { return };
35
36        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
37            merge_directives(cx, &mut self.directive, (span, directive));
38        }
39    }
40}
41
42impl AttributeParser for OnTypeErrorParser {
43    const ATTRIBUTES: AcceptMapping<Self> = &[(
44        &[sym::diagnostic, sym::on_type_error],
45        crate::AttributeTemplate {
    word: false,
    list: Some(&[r#"note = "...""#]),
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(List: &[r#"note = "...""#]),
46        AttributeStability::Stable,
47        |this, cx, args| {
48            this.parse(cx, args, Mode::DiagnosticOnTypeError);
49        },
50    )];
51
52    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
53
54    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
55        if let Some(span) = self.span {
56            Some(AttributeKind::OnTypeError {
57                span,
58                directive: self.directive.map(|d| Box::new(d.1)),
59            })
60        } else {
61            None
62        }
63    }
64}