rustc_attr_parsing/attributes/diagnostic/
on_const.rs1use rustc_hir::attrs::diagnostic::Directive;
2
3use crate::attributes::diagnostic::*;
4use crate::attributes::prelude::*;
5
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<S: Stage> AttributeParser<S> for OnConstParser {
13 const ATTRIBUTES: AcceptMapping<Self, S> = &[(
14 &[sym::diagnostic, sym::on_const],
15 ::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 = "...""#]),
16 |this, cx, args| {
17 if !cx.features().diagnostic_on_const() {
18 return;
20 }
21
22 let span = cx.attr_span;
23 this.span = Some(span);
24 let mode = Mode::DiagnosticOnConst;
25
26 let Some(items) = parse_list(cx, args, mode) else { return };
27
28 let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
29 return;
30 };
31 merge_directives(cx, &mut this.directive, (span, directive));
32 },
33 )];
34
35 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
37
38 fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
39 if let Some(span) = self.span {
40 Some(AttributeKind::OnConst { span, directive: self.directive.map(|d| Box::new(d.1)) })
41 } else {
42 None
43 }
44 }
45}