rustc_attr_parsing/attributes/diagnostic/
on_const.rs1use rustc_errors::Diagnostic;
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<S: Stage> AttributeParser<S> for OnConstParser {
15 const ATTRIBUTES: AcceptMapping<Self, S> = &[(
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 |this, cx, args| {
19 if !cx.features().diagnostic_on_const() {
20 return;
22 }
23
24 let span = cx.attr_span;
25 this.span = Some(span);
26
27 if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
Target::Impl { of_trait: true } => true,
_ => false,
}matches!(cx.target, Target::Impl { of_trait: true }) {
30 let target_span = cx.target_span;
31 cx.emit_lint(
32 MISPLACED_DIAGNOSTIC_ATTRIBUTES,
33 move |dcx, level| {
34 DiagnosticOnConstOnlyForTraitImpls { target_span }.into_diag(dcx, level)
35 },
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<'_, '_, S>) -> 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}