Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unknown.rs

1use rustc_feature::AttributeStability;
2use rustc_hir::attrs::diagnostic::Directive;
3
4use crate::attributes::diagnostic::*;
5use crate::attributes::prelude::*;
6
7#[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)]
8pub(crate) struct OnUnknownParser {
9    span: Option<Span>,
10    directive: Option<(Span, Directive)>,
11}
12
13impl OnUnknownParser {
14    fn parse<'sess>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
15        if let Some(features) = cx.features
16            && !features.diagnostic_on_unknown()
17        {
18            // `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
19            args.ignore_args();
20            return;
21        }
22        let span = cx.attr_span;
23        self.span = Some(span);
24
25        let Some(items) = parse_list(cx, args, mode) else { return };
26
27        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
28            merge_directives(cx, &mut self.directive, (span, directive));
29        };
30    }
31}
32
33impl AttributeParser for OnUnknownParser {
34    const ATTRIBUTES: AcceptMapping<Self> = &[(
35        &[sym::diagnostic, sym::on_unknown],
36        crate::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 = "...""#]),
37        AttributeStability::Stable, // Unstable, stability checked manually in the parser
38        |this, cx, args| {
39            this.parse(cx, args, Mode::DiagnosticOnUnknown);
40        },
41    )];
42    // "Allowed" for all targets, but noop for all but use statements.
43    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
44        Allow(Target::Use),
45        Allow(Target::Mod),
46        Allow(Target::Crate),
47    ]);
48
49    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
50        if let Some(_span) = self.span {
51            Some(AttributeKind::OnUnknown { directive: self.directive.map(|d| Box::new(d.1)) })
52        } else {
53            None
54        }
55    }
56}