Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unimplemented.rs

1use 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::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                DiagnosticOnUnimplementedOnlyForTraits,
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            AttributeStability::Stable,
43            |this, cx, args| {
44                this.parse(cx, args, Mode::DiagnosticOnUnimplemented);
45            },
46        ),
47        (
48            &[sym::rustc_on_unimplemented],
49            ::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 = "...""#]),
50            AttributeStability::Unstable {
    gate_name: rustc_span::sym::rustc_attrs,
    gate_check: rustc_feature::Features::rustc_attrs,
    notes: &["see `#[diagnostic::on_unimplemented]` for the stable equivalent of this attribute"],
}unstable!(
51                rustc_attrs,
52                "see `#[diagnostic::on_unimplemented]` for the stable equivalent of this attribute"
53            ),
54            |this, cx, args| {
55                this.parse(cx, args, Mode::RustcOnUnimplemented);
56            },
57        ),
58    ];
59    //FIXME attribute is not parsed for non-traits but diagnostics are issued in `check_attr.rs`
60    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
61
62    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
63        if let Some(_span) = self.span {
64            Some(AttributeKind::OnUnimplemented {
65                directive: self.directive.map(|d| Box::new(d.1)),
66            })
67        } else {
68            None
69        }
70    }
71}