Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_const.rs

1use rustc_attr_ir::diagnostic::Directive;
2use rustc_feature::AttributeStability;
3
4use crate::attributes::diagnostic::*;
5use crate::attributes::prelude::*;
6#[derive(#[automatically_derived]
impl ::core::default::Default for OnConstParser {
    #[inline]
    fn default() -> OnConstParser {
        OnConstParser {
            path_span: ::core::default::Default::default(),
            directive: ::core::default::Default::default(),
        }
    }
}Default)]
7pub(crate) struct OnConstParser {
8    path_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 below
17        |this, cx, args| {
18            {
    if let Some(features) = cx.features_option() &&
            !features.diagnostic_on_const() {
        args.ignore_args();
        let nightly_build = cx.sess.is_nightly_build();
        let span = cx.attr_span;
        cx.emit_lint(rustc_lint_defs::builtin::UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
            crate::diagnostics::UnstableDiagnosticAttribute {
                feature: sym::diagnostic_on_const,
                nightly_build,
            }, span);
        return;
    }
};gate_diagnostic_attr!(diagnostic_on_const);
19
20            let path_span = cx.attr_path.span;
21            this.path_span = Some(path_span);
22
23            let mode = Mode::DiagnosticOnConst;
24
25            let Some(items) = parse_list(cx, args, mode) else { return };
26
27            let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
28                return;
29            };
30            merge_directives(cx, &mut this.directive, (path_span, directive));
31        },
32    )];
33
34    // "Allowed" on all targets; noop on anything but non-const trait impls;
35    // this linted on in parser.
36    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
37        // FIXME(mejrs) no constness field on `Target`,
38        // so non-constness is still checked in check_attr.rs
39        Allow(Target::Impl { of_trait: true }),
40    ]);
41
42    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
43        if let Some(path_span) = self.path_span {
44            Some(AttributeKind::OnConst {
45                span: path_span,
46                directive: self.directive.map(|d| Box::new(d.1)),
47            })
48        } else {
49            None
50        }
51    }
52}