Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unimplemented.rs

1use 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, S: Stage>(
17        &mut self,
18        cx: &mut AcceptContext<'_, 'sess, S>,
19        args: &ArgParser,
20        mode: Mode,
21    ) {
22        let span = cx.attr_span;
23        self.span = Some(span);
24
25        if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::Trait => true,
    _ => false,
}matches!(cx.target, Target::Trait) {
26            cx.emit_dyn_lint(
27                MISPLACED_DIAGNOSTIC_ATTRIBUTES,
28                move |dcx, level| DiagnosticOnUnimplementedOnlyForTraits.into_diag(dcx, level),
29                span,
30            );
31            return;
32        }
33
34        let Some(items) = parse_list(cx, args, mode) else { return };
35
36        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
37            merge_directives(cx, &mut self.directive, (span, directive));
38        };
39    }
40}
41
42impl<S: Stage> AttributeParser<S> for OnUnimplementedParser {
43    const ATTRIBUTES: AcceptMapping<Self, S> = &[
44        (
45            &[sym::diagnostic, sym::on_unimplemented],
46            ::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 = "...""#]),
47            |this, cx, args| {
48                this.parse(cx, args, Mode::DiagnosticOnUnimplemented);
49            },
50        ),
51        (
52            &[sym::rustc_on_unimplemented],
53            ::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 = "...""#]),
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<'_, '_, S>) -> Option<AttributeKind> {
63        if let Some(span) = self.span {
64            Some(AttributeKind::OnUnimplemented {
65                span,
66                directive: self.directive.map(|d| Box::new(d.1)),
67            })
68        } else {
69            None
70        }
71    }
72}