Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_const.rs

1use rustc_feature::AttributeStability;
2use rustc_hir::attrs::diagnostic::Directive;
3
4use crate::attributes::diagnostic::*;
5use crate::attributes::prelude::*;
6#[derive(#[automatically_derived]
impl ::core::default::Default for OnConstParser {
    #[inline]
    fn default() -> OnConstParser {
        OnConstParser {
            span: ::core::default::Default::default(),
            directive: ::core::default::Default::default(),
        }
    }
}Default)]
7pub(crate) struct OnConstParser {
8    span: Option<Span>,
9    directive: Option<(Span, Directive)>,
10}
11
12impl AttributeParser for OnConstParser {
13    const ATTRIBUTES: AcceptMapping<Self> = &[(
14        &[sym::diagnostic, sym::on_const],
15        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 = "...""#]),
16        AttributeStability::Stable, // Unstable, stability checked manually in the parser
17        |this, cx, args| {
18            if !cx.features().diagnostic_on_const() {
19                // `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
20                args.ignore_args();
21                return;
22            }
23
24            let span = cx.attr_span;
25            this.span = Some(span);
26
27            let mode = Mode::DiagnosticOnConst;
28
29            let Some(items) = parse_list(cx, args, mode) else { return };
30
31            let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
32                return;
33            };
34            merge_directives(cx, &mut this.directive, (span, directive));
35        },
36    )];
37
38    // "Allowed" on all targets; noop on anything but non-const trait impls;
39    // this linted on in parser.
40    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
41        // FIXME(mejrs) no constness field on `Target`,
42        // so non-constness is still checked in check_attr.rs
43        Allow(Target::Impl { of_trait: true }),
44    ]);
45
46    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
47        if let Some(span) = self.span {
48            Some(AttributeKind::OnConst { span, directive: self.directive.map(|d| Box::new(d.1)) })
49        } else {
50            None
51        }
52    }
53}