rustc_attr_parsing/attributes/diagnostic/
on_const.rs1use rustc_feature::AttributeStability;
2use rustc_hir::attrs::diagnostic::Directive;
3use rustc_session::lint::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES;
4
5use crate::attributes::diagnostic::*;
6use crate::attributes::prelude::*;
7use crate::errors::DiagnosticOnConstOnlyForTraitImpls;
8#[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)]
9pub(crate) struct OnConstParser {
10 span: Option<Span>,
11 directive: Option<(Span, Directive)>,
12}
13
14impl AttributeParser for OnConstParser {
15 const ATTRIBUTES: AcceptMapping<Self> = &[(
16 &[sym::diagnostic, sym::on_const],
17 ::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 = "...""#]),
18 AttributeStability::Stable, |this, cx, args| {
20 if !cx.features().diagnostic_on_const() {
21 args.ignore_args();
23 return;
24 }
25
26 let span = cx.attr_span;
27 this.span = Some(span);
28
29 if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
Target::Impl { of_trait: true } => true,
_ => false,
}matches!(cx.target, Target::Impl { of_trait: true }) {
32 let target_span = cx.target_span;
33 cx.emit_lint(
34 MISPLACED_DIAGNOSTIC_ATTRIBUTES,
35 DiagnosticOnConstOnlyForTraitImpls { target_span },
36 span,
37 );
38 return;
39 }
40
41 let mode = Mode::DiagnosticOnConst;
42
43 let Some(items) = parse_list(cx, args, mode) else { return };
44
45 let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
46 return;
47 };
48 merge_directives(cx, &mut this.directive, (span, directive));
49 },
50 )];
51
52 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
55
56 fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
57 if let Some(span) = self.span {
58 Some(AttributeKind::OnConst { span, directive: self.directive.map(|d| Box::new(d.1)) })
59 } else {
60 None
61 }
62 }
63}