Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unimplemented.rs

1use rustc_feature::AttributeStability;
2use rustc_hir::attrs::diagnostic::Directive;
3
4use crate::attributes::diagnostic::*;
5use crate::attributes::prelude::*;
6
7#[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)]
8pub(crate) struct OnUnimplementedParser {
9    span: Option<Span>,
10    directive: Option<(Span, Directive)>,
11}
12
13impl OnUnimplementedParser {
14    fn parse<'sess>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
15        let span = cx.attr_span;
16        self.span = Some(span);
17
18        let Some(items) = parse_list(cx, args, mode) else { return };
19
20        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
21            merge_directives(cx, &mut self.directive, (span, directive));
22        };
23    }
24}
25
26impl AttributeParser for OnUnimplementedParser {
27    const ATTRIBUTES: AcceptMapping<Self> = &[
28        (
29            &[sym::diagnostic, sym::on_unimplemented],
30            crate::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 = "...""#]),
31            AttributeStability::Stable,
32            |this, cx, args| {
33                this.parse(cx, args, Mode::DiagnosticOnUnimplemented);
34            },
35        ),
36        (
37            &[sym::rustc_on_unimplemented],
38            crate::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 = "...""#]),
39            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!(
40                rustc_attrs,
41                "see `#[diagnostic::on_unimplemented]` for the stable equivalent of this attribute"
42            ),
43            |this, cx, args| {
44                this.parse(cx, args, Mode::RustcOnUnimplemented);
45            },
46        ),
47    ];
48    const ALLOWED_TARGETS: AllowedTargets =
49        AllowedTargets::AllowListWarnRest(&[Allow(Target::Trait)]);
50
51    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
52        if let Some(_span) = self.span {
53            Some(AttributeKind::OnUnimplemented {
54                directive: self.directive.map(|d| Box::new(d.1)),
55            })
56        } else {
57            None
58        }
59    }
60}