rustc_attr_parsing/attributes/diagnostic/
on_unknown.rs1use rustc_hir::attrs::diagnostic::Directive;
2
3use crate::attributes::diagnostic::*;
4use crate::attributes::prelude::*;
5
6#[derive(#[automatically_derived]
impl ::core::default::Default for OnUnknownParser {
#[inline]
fn default() -> OnUnknownParser {
OnUnknownParser {
span: ::core::default::Default::default(),
directive: ::core::default::Default::default(),
}
}
}Default)]
7pub(crate) struct OnUnknownParser {
8 span: Option<Span>,
9 directive: Option<(Span, Directive)>,
10}
11
12impl OnUnknownParser {
13 fn parse<'sess, S: Stage>(
14 &mut self,
15 cx: &mut AcceptContext<'_, 'sess, S>,
16 args: &ArgParser,
17 mode: Mode,
18 ) {
19 if let Some(features) = cx.features
20 && !features.diagnostic_on_unknown()
21 {
22 return;
24 }
25 let span = cx.attr_span;
26 self.span = Some(span);
27
28 let Some(items) = parse_list(cx, args, mode) else { return };
29
30 if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
31 merge_directives(cx, &mut self.directive, (span, directive));
32 };
33 }
34}
35
36impl<S: Stage> AttributeParser<S> for OnUnknownParser {
37 const ATTRIBUTES: AcceptMapping<Self, S> = &[(
38 &[sym::diagnostic, sym::on_unknown],
39 ::rustc_feature::AttributeTemplate {
word: false,
list: Some(&[r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &[r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#]),
40 |this, cx, args| {
41 this.parse(cx, args, Mode::DiagnosticOnUnknown);
42 },
43 )];
44 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
46
47 fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
48 if let Some(span) = self.span {
49 Some(AttributeKind::OnUnknown {
50 span,
51 directive: self.directive.map(|d| Box::new(d.1)),
52 })
53 } else {
54 None
55 }
56 }
57}