rustc_attr_parsing/attributes/diagnostic/
on_move.rs1use 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;
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>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
22 if !cx.features().diagnostic_on_move() {
23 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(
32 MISPLACED_DIAGNOSTIC_ATTRIBUTES,
33 move |dcx, level| DiagnosticOnMoveOnlyForAdt.into_diag(dcx, level),
34 span,
35 );
36 return;
37 }
38
39 let Some(items) = parse_list(cx, args, mode) else { return };
40
41 if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
42 merge_directives(cx, &mut self.directive, (span, directive));
43 }
44 }
45}
46impl AttributeParser for OnMoveParser {
47 const ATTRIBUTES: AcceptMapping<Self> = &[(
48 &[sym::diagnostic, sym::on_move],
49 ::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 = "...""#]),
50 |this, cx, args| {
51 this.parse(cx, args, Mode::DiagnosticOnMove);
52 },
53 )];
54
55 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
57
58 fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
59 if let Some(span) = self.span {
60 Some(AttributeKind::OnMove { span, directive: self.directive.map(|d| Box::new(d.1)) })
61 } else {
62 None
63 }
64 }
65}