Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unknown.rs

1use 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 !cx.features().diagnostic_on_unknown() {
20            // `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
21            return;
22        }
23        let span = cx.attr_span;
24        self.span = Some(span);
25
26        let Some(items) = parse_list(cx, args, mode) else { return };
27
28        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
29            merge_directives(cx, &mut self.directive, (span, directive));
30        };
31    }
32}
33
34impl<S: Stage> AttributeParser<S> for OnUnknownParser {
35    const ATTRIBUTES: AcceptMapping<Self, S> = &[(
36        &[sym::diagnostic, sym::on_unknown],
37        ::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 = "...""#]),
38        |this, cx, args| {
39            this.parse(cx, args, Mode::DiagnosticOnUnknown);
40        },
41    )];
42    //FIXME attribute is not parsed for non-use statements but diagnostics are issued in `check_attr.rs`
43    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
44
45    fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
46        if let Some(span) = self.span {
47            Some(AttributeKind::OnUnknown {
48                span,
49                directive: self.directive.map(|d| Box::new(d.1)),
50            })
51        } else {
52            None
53        }
54    }
55}