rustc_attr_parsing/attributes/diagnostic/
on_const.rs1use 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, |this, cx, args| {
18 if !cx.features().diagnostic_on_const() {
19 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 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
41 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}