Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_move.rs

1use rustc_feature::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            return;
24        }
25
26        let span = cx.attr_span;
27        self.span = Some(span);
28
29        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) {
30            cx.emit_lint(MISPLACED_DIAGNOSTIC_ATTRIBUTES, DiagnosticOnMoveOnlyForAdt, span);
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}
41impl AttributeParser for OnMoveParser {
42    const ATTRIBUTES: AcceptMapping<Self> = &[(
43        &[sym::diagnostic, sym::on_move],
44        ::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 = "...""#]),
45        |this, cx, args| {
46            this.parse(cx, args, Mode::DiagnosticOnMove);
47        },
48    )];
49
50    // "Allowed" for all targets but noop if used on not-adt.
51    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
52
53    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
54        if let Some(_span) = self.span {
55            Some(AttributeKind::OnMove { directive: self.directive.map(|d| Box::new(d.1)) })
56        } else {
57            None
58        }
59    }
60}