rustc_attr_parsing/attributes/diagnostic/
on_unimplemented.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::DiagnosticOnUnimplementedOnlyForTraits;
8
9#[derive(#[automatically_derived]
impl ::core::default::Default for OnUnimplementedParser {
#[inline]
fn default() -> OnUnimplementedParser {
OnUnimplementedParser {
span: ::core::default::Default::default(),
directive: ::core::default::Default::default(),
}
}
}Default)]
10pub(crate) struct OnUnimplementedParser {
11 span: Option<Span>,
12 directive: Option<(Span, Directive)>,
13}
14
15impl OnUnimplementedParser {
16 fn parse<'sess>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
17 let span = cx.attr_span;
18 self.span = Some(span);
19
20 if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
Target::Trait => true,
_ => false,
}matches!(cx.target, Target::Trait) {
21 cx.emit_lint(
22 MISPLACED_DIAGNOSTIC_ATTRIBUTES,
23 move |dcx, level| DiagnosticOnUnimplementedOnlyForTraits.into_diag(dcx, level),
24 span,
25 );
26 return;
27 }
28
29 let Some(items) = parse_list(cx, args, mode) else { return };
30
31 if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
32 merge_directives(cx, &mut self.directive, (span, directive));
33 };
34 }
35}
36
37impl AttributeParser for OnUnimplementedParser {
38 const ATTRIBUTES: AcceptMapping<Self> = &[
39 (
40 &[sym::diagnostic, sym::on_unimplemented],
41 ::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 = "...""#]),
42 |this, cx, args| {
43 this.parse(cx, args, Mode::DiagnosticOnUnimplemented);
44 },
45 ),
46 (
47 &[sym::rustc_on_unimplemented],
48 ::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 = "...""#]),
49 |this, cx, args| {
50 this.parse(cx, args, Mode::RustcOnUnimplemented);
51 },
52 ),
53 ];
54 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
56
57 fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
58 if let Some(span) = self.span {
59 Some(AttributeKind::OnUnimplemented {
60 span,
61 directive: self.directive.map(|d| Box::new(d.1)),
62 })
63 } else {
64 None
65 }
66 }
67}