diff options
| -rw-r--r-- | compiler/rustc_passes/src/check_attr.rs | 57 | ||||
| -rw-r--r-- | tests/ui/repr/attr-usage-repr.rs | 31 | ||||
| -rw-r--r-- | tests/ui/repr/attr-usage-repr.stderr | 36 |
3 files changed, 91 insertions, 33 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 074fe77324f..06d6924b99f 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1791,6 +1791,34 @@ impl<'tcx> CheckAttrVisitor<'tcx> { let mut is_simd = false; let mut is_transparent = false; + // catch `repr()` with no arguments, applied to an item (i.e. not `#![repr()]`) + if hints.is_empty() && item.is_some() { + for attr in attrs.iter().filter(|attr| attr.has_name(sym::repr)) { + match target { + Target::Struct | Target::Union | Target::Enum => {} + Target::Fn | Target::Method(_) => { + feature_err( + &self.tcx.sess, + sym::fn_align, + attr.span, + fluent::passes_repr_align_function, + ) + .emit(); + } + _ => { + self.dcx().emit_err( + errors::AttrApplication::StructEnumFunctionMethodUnion { + hint_span: attr.span, + span, + }, + ); + } + } + } + + return; + } + for hint in &hints { if !hint.is_meta_item() { self.dcx().emit_err(errors::ReprIdent { span: hint.span() }); @@ -1823,24 +1851,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> { } } sym::align => { - if let (Target::Fn | Target::Method(MethodKind::Inherent), false) = - (target, self.tcx.features().fn_align()) - { - feature_err( - &self.tcx.sess, - sym::fn_align, - hint.span(), - fluent::passes_repr_align_function, - ) - .emit(); - } - match target { - Target::Struct - | Target::Union - | Target::Enum - | Target::Fn - | Target::Method(_) => {} + Target::Struct | Target::Union | Target::Enum => {} + Target::Fn | Target::Method(_) => { + if !self.tcx.features().fn_align() { + feature_err( + &self.tcx.sess, + sym::fn_align, + hint.span(), + fluent::passes_repr_align_function, + ) + .emit(); + } + } _ => { self.dcx().emit_err( errors::AttrApplication::StructEnumFunctionMethodUnion { diff --git a/tests/ui/repr/attr-usage-repr.rs b/tests/ui/repr/attr-usage-repr.rs index dc0d4f5be2e..d8515a12e05 100644 --- a/tests/ui/repr/attr-usage-repr.rs +++ b/tests/ui/repr/attr-usage-repr.rs @@ -16,18 +16,39 @@ struct SSimd([f64; 2]); struct SInt(f64, f64); #[repr(C)] -enum EExtern { A, B } +enum EExtern { + A, + B, +} #[repr(align(8))] -enum EAlign { A, B } +enum EAlign { + A, + B, +} #[repr(packed)] //~ ERROR: attribute should be applied to a struct -enum EPacked { A, B } +enum EPacked { + A, + B, +} #[repr(simd)] //~ ERROR: attribute should be applied to a struct -enum ESimd { A, B } +enum ESimd { + A, + B, +} #[repr(i8)] -enum EInt { A, B } +enum EInt { + A, + B, +} + +#[repr()] //~ attribute should be applied to a struct, enum, function, associated function, or union [E0517] +type SirThisIsAType = i32; + +#[repr()] +struct EmptyReprArgumentList(i32); fn main() {} diff --git a/tests/ui/repr/attr-usage-repr.stderr b/tests/ui/repr/attr-usage-repr.stderr index 42f65625a46..a25e68c483f 100644 --- a/tests/ui/repr/attr-usage-repr.stderr +++ b/tests/ui/repr/attr-usage-repr.stderr @@ -15,21 +15,35 @@ LL | struct SInt(f64, f64); | ---------------------- not an enum error[E0517]: attribute should be applied to a struct or union - --> $DIR/attr-usage-repr.rs:24:8 + --> $DIR/attr-usage-repr.rs:30:8 | -LL | #[repr(packed)] - | ^^^^^^ -LL | enum EPacked { A, B } - | --------------------- not a struct or union +LL | #[repr(packed)] + | ^^^^^^ +LL | / enum EPacked { +LL | | A, +LL | | B, +LL | | } + | |_- not a struct or union error[E0517]: attribute should be applied to a struct - --> $DIR/attr-usage-repr.rs:27:8 + --> $DIR/attr-usage-repr.rs:36:8 | -LL | #[repr(simd)] - | ^^^^ -LL | enum ESimd { A, B } - | ------------------- not a struct +LL | #[repr(simd)] + | ^^^^ +LL | / enum ESimd { +LL | | A, +LL | | B, +LL | | } + | |_- not a struct -error: aborting due to 4 previous errors +error[E0517]: attribute should be applied to a struct, enum, function, associated function, or union + --> $DIR/attr-usage-repr.rs:48:1 + | +LL | #[repr()] + | ^^^^^^^^^ +LL | type SirThisIsAType = i32; + | -------------------------- not a struct, enum, function, associated function, or union + +error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0517`. |
