Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_move.rs

1use rustc_errors::Diagnostic;
2use rustc_feature::template;
3use rustc_hir::attrs::AttributeKind;
4use rustc_session::lint::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES;
5use rustc_span::sym;
6
7use crate::attributes::diagnostic::*;
8use crate::attributes::prelude::*;
9use crate::context::{AcceptContext, Stage};
10use crate::errors::DiagnosticOnMoveOnlyForAdt;
11use crate::parser::ArgParser;
12use crate::target_checking::{ALL_TARGETS, AllowedTargets};
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, S: Stage>(
22        &mut self,
23        cx: &mut AcceptContext<'_, 'sess, S>,
24        args: &ArgParser,
25        mode: Mode,
26    ) {
27        if !cx.features().diagnostic_on_move() {
28            // `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
29            return;
30        }
31
32        let span = cx.attr_span;
33        self.span = Some(span);
34
35        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) {
36            cx.emit_dyn_lint(
37                MISPLACED_DIAGNOSTIC_ATTRIBUTES,
38                move |dcx, level| DiagnosticOnMoveOnlyForAdt.into_diag(dcx, level),
39                span,
40            );
41            return;
42        }
43
44        let Some(items) = parse_list(cx, args, mode) else { return };
45
46        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
47            merge_directives(cx, &mut self.directive, (span, directive));
48        }
49    }
50}
51impl<S: Stage> AttributeParser<S> for OnMoveParser {
52    const ATTRIBUTES: AcceptMapping<Self, S> = &[(
53        &[sym::diagnostic, sym::on_move],
54        ::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 = "...""#]),
55        |this, cx, args| {
56            this.parse(cx, args, Mode::DiagnosticOnMove);
57        },
58    )];
59
60    // "Allowed" for all targets but noop if used on not-adt.
61    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
62
63    fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
64        if let Some(span) = self.span {
65            Some(AttributeKind::OnMove { span, directive: self.directive.map(|d| Box::new(d.1)) })
66        } else {
67            None
68        }
69    }
70}