Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_move.rs

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