Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unimplemented.rs

1use rustc_hir::attrs::diagnostic::Directive;
2
3use crate::attributes::diagnostic::*;
4use crate::attributes::prelude::*;
5
6#[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)]
7pub(crate) struct OnUnimplementedParser {
8    span: Option<Span>,
9    directive: Option<(Span, Directive)>,
10}
11
12impl OnUnimplementedParser {
13    fn parse<'sess, S: Stage>(
14        &mut self,
15        cx: &mut AcceptContext<'_, 'sess, S>,
16        args: &ArgParser,
17        mode: Mode,
18    ) {
19        let span = cx.attr_span;
20        self.span = Some(span);
21
22        // If target is not a trait, returning early will make `finalize` emit a
23        // `AttributeKind::OnUnimplemented {span, directive: None }`, to prevent it being
24        // accidentally used on non-trait items like trait aliases.
25        if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::Trait => true,
    _ => false,
}matches!(cx.target, Target::Trait) {
26            // Lint later emitted in check_attr
27            return;
28        }
29
30        let Some(items) = parse_list(cx, args, mode) else { return };
31
32        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
33            merge_directives(cx, &mut self.directive, (span, directive));
34        };
35    }
36}
37
38impl<S: Stage> AttributeParser<S> for OnUnimplementedParser {
39    const ATTRIBUTES: AcceptMapping<Self, S> = &[
40        (
41            &[sym::diagnostic, sym::on_unimplemented],
42            ::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 = "...""#]),
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            |this, cx, args| {
51                this.parse(cx, args, Mode::RustcOnUnimplemented);
52            },
53        ),
54    ];
55    //FIXME attribute is not parsed for non-traits but diagnostics are issued in `check_attr.rs`
56    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
57
58    fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
59        if let Some(span) = self.span {
60            Some(AttributeKind::OnUnimplemented {
61                span,
62                directive: self.directive.map(|d| Box::new(d.1)),
63            })
64        } else {
65            None
66        }
67    }
68}