Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_type_error.rs

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