Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_move.rs

1use rustc_feature::{AttributeStability, template};
2use rustc_hir::attrs::AttributeKind;
3use rustc_session::lint::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES;
4use rustc_span::sym;
5
6use crate::attributes::diagnostic::*;
7use crate::attributes::prelude::*;
8use crate::context::AcceptContext;
9use crate::errors::DiagnosticOnMoveOnlyForAdt;
10use crate::parser::ArgParser;
11use crate::target_checking::{ALL_TARGETS, AllowedTargets};
12
13#[derive(#[automatically_derived]
impl ::core::default::Default for OnMoveParser {
    #[inline]
    fn default() -> OnMoveParser {
        OnMoveParser {
            span: ::core::default::Default::default(),
            directive: ::core::default::Default::default(),
        }
    }
}Default)]
14pub(crate) struct OnMoveParser {
15    span: Option<Span>,
16    directive: Option<(Span, Directive)>,
17}
18
19impl OnMoveParser {
20    fn parse<'sess>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
21        if !cx.features().diagnostic_on_move() {
22            // `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
23            args.ignore_args();
24            return;
25        }
26
27        let span = cx.attr_span;
28        self.span = Some(span);
29
30        if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::Enum | Target::Struct | Target::Union => true,
    _ => false,
}matches!(cx.target, Target::Enum | Target::Struct | Target::Union) {
31            cx.emit_lint(MISPLACED_DIAGNOSTIC_ATTRIBUTES, DiagnosticOnMoveOnlyForAdt, span);
32            return;
33        }
34
35        let Some(items) = parse_list(cx, args, mode) else { return };
36
37        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
38            merge_directives(cx, &mut self.directive, (span, directive));
39        }
40    }
41}
42impl AttributeParser for OnMoveParser {
43    const ATTRIBUTES: AcceptMapping<Self> = &[(
44        &[sym::diagnostic, sym::on_move],
45        ::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 = "...""#]),
46        AttributeStability::Stable, // Unstable, stability checked manually in the parser
47        |this, cx, args| {
48            this.parse(cx, args, Mode::DiagnosticOnMove);
49        },
50    )];
51
52    // "Allowed" for all targets but noop if used on not-adt.
53    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
54
55    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
56        if let Some(_span) = self.span {
57            Some(AttributeKind::OnMove { directive: self.directive.map(|d| Box::new(d.1)) })
58        } else {
59            None
60        }
61    }
62}