Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_const.rs

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