rustc_attr_parsing/attributes/diagnostic/
on_type_error.rs1use 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 args.ignore_args();
23 return;
24 }
25
26 let span = cx.attr_span;
27 self.span = Some(span);
28
29 let Some(items) = parse_list(cx, args, mode) else { return };
30
31 if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
32 merge_directives(cx, &mut self.directive, (span, directive));
33 }
34 }
35}
36
37impl AttributeParser for OnTypeErrorParser {
38 const ATTRIBUTES: AcceptMapping<Self> = &[(
39 &[sym::diagnostic, sym::on_type_error],
40 crate::AttributeTemplate {
word: false,
list: Some(&[r#"note = "...""#]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &[r#"note = "...""#]),
41 AttributeStability::Stable,
42 |this, cx, args| {
43 this.parse(cx, args, Mode::DiagnosticOnTypeError);
44 },
45 )];
46
47 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
48 Allow(Target::Enum),
49 Allow(Target::Struct),
50 Allow(Target::Union),
51 ]);
52
53 fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
54 if let Some(span) = self.span {
55 Some(AttributeKind::OnTypeError {
56 span,
57 directive: self.directive.map(|d| Box::new(d.1)),
58 })
59 } else {
60 None
61 }
62 }
63}