Skip to content

Commit 617b810

Browse files
committed
Auto merge of rust-lang#133925 - folkertdev:improve-repr-warnings, r=<try>
disallow `repr()` on invalid items fixes rust-lang#129606 Disallows `repr()` (so a repr with no arguments) on items where that won't ever make sense. Also this generates an error when `repr` is used on a trait method and the `fn_align` feature is not enabled. Looks like that was missed here: https://github.com/rust-lang/rust/pull/110313/files Which first accepts the align attribute on trait methods. r? `@compiler-errors`
2 parents 0e98766 + dfd76c1 commit 617b810

File tree

3 files changed

+91
-33
lines changed

3 files changed

+91
-33
lines changed

compiler/rustc_passes/src/check_attr.rs

+40-17
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,34 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17911791
let mut is_simd = false;
17921792
let mut is_transparent = false;
17931793

1794+
// catch `repr()` with no arguments, applied to an item (i.e. not `#![repr()]`)
1795+
if hints.is_empty() && item.is_some() {
1796+
for attr in attrs.iter().filter(|attr| attr.has_name(sym::repr)) {
1797+
match target {
1798+
Target::Struct | Target::Union | Target::Enum => {}
1799+
Target::Fn | Target::Method(_) => {
1800+
feature_err(
1801+
&self.tcx.sess,
1802+
sym::fn_align,
1803+
attr.span,
1804+
fluent::passes_repr_align_function,
1805+
)
1806+
.emit();
1807+
}
1808+
_ => {
1809+
self.dcx().emit_err(
1810+
errors::AttrApplication::StructEnumFunctionMethodUnion {
1811+
hint_span: attr.span,
1812+
span,
1813+
},
1814+
);
1815+
}
1816+
}
1817+
}
1818+
1819+
return;
1820+
}
1821+
17941822
for hint in &hints {
17951823
if !hint.is_meta_item() {
17961824
self.dcx().emit_err(errors::ReprIdent { span: hint.span() });
@@ -1823,24 +1851,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18231851
}
18241852
}
18251853
sym::align => {
1826-
if let (Target::Fn | Target::Method(MethodKind::Inherent), false) =
1827-
(target, self.tcx.features().fn_align())
1828-
{
1829-
feature_err(
1830-
&self.tcx.sess,
1831-
sym::fn_align,
1832-
hint.span(),
1833-
fluent::passes_repr_align_function,
1834-
)
1835-
.emit();
1836-
}
1837-
18381854
match target {
1839-
Target::Struct
1840-
| Target::Union
1841-
| Target::Enum
1842-
| Target::Fn
1843-
| Target::Method(_) => {}
1855+
Target::Struct | Target::Union | Target::Enum => {}
1856+
Target::Fn | Target::Method(_) => {
1857+
if !self.tcx.features().fn_align() {
1858+
feature_err(
1859+
&self.tcx.sess,
1860+
sym::fn_align,
1861+
hint.span(),
1862+
fluent::passes_repr_align_function,
1863+
)
1864+
.emit();
1865+
}
1866+
}
18441867
_ => {
18451868
self.dcx().emit_err(
18461869
errors::AttrApplication::StructEnumFunctionMethodUnion {

tests/ui/repr/attr-usage-repr.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,39 @@ struct SSimd([f64; 2]);
1616
struct SInt(f64, f64);
1717

1818
#[repr(C)]
19-
enum EExtern { A, B }
19+
enum EExtern {
20+
A,
21+
B,
22+
}
2023

2124
#[repr(align(8))]
22-
enum EAlign { A, B }
25+
enum EAlign {
26+
A,
27+
B,
28+
}
2329

2430
#[repr(packed)] //~ ERROR: attribute should be applied to a struct
25-
enum EPacked { A, B }
31+
enum EPacked {
32+
A,
33+
B,
34+
}
2635

2736
#[repr(simd)] //~ ERROR: attribute should be applied to a struct
28-
enum ESimd { A, B }
37+
enum ESimd {
38+
A,
39+
B,
40+
}
2941

3042
#[repr(i8)]
31-
enum EInt { A, B }
43+
enum EInt {
44+
A,
45+
B,
46+
}
47+
48+
#[repr()] //~ attribute should be applied to a struct, enum, function, associated function, or union [E0517]
49+
type SirThisIsAType = i32;
50+
51+
#[repr()]
52+
struct EmptyReprArgumentList(i32);
3253

3354
fn main() {}

tests/ui/repr/attr-usage-repr.stderr

+25-11
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,35 @@ LL | struct SInt(f64, f64);
1515
| ---------------------- not an enum
1616

1717
error[E0517]: attribute should be applied to a struct or union
18-
--> $DIR/attr-usage-repr.rs:24:8
18+
--> $DIR/attr-usage-repr.rs:30:8
1919
|
20-
LL | #[repr(packed)]
21-
| ^^^^^^
22-
LL | enum EPacked { A, B }
23-
| --------------------- not a struct or union
20+
LL | #[repr(packed)]
21+
| ^^^^^^
22+
LL | / enum EPacked {
23+
LL | | A,
24+
LL | | B,
25+
LL | | }
26+
| |_- not a struct or union
2427

2528
error[E0517]: attribute should be applied to a struct
26-
--> $DIR/attr-usage-repr.rs:27:8
29+
--> $DIR/attr-usage-repr.rs:36:8
2730
|
28-
LL | #[repr(simd)]
29-
| ^^^^
30-
LL | enum ESimd { A, B }
31-
| ------------------- not a struct
31+
LL | #[repr(simd)]
32+
| ^^^^
33+
LL | / enum ESimd {
34+
LL | | A,
35+
LL | | B,
36+
LL | | }
37+
| |_- not a struct
3238

33-
error: aborting due to 4 previous errors
39+
error[E0517]: attribute should be applied to a struct, enum, function, associated function, or union
40+
--> $DIR/attr-usage-repr.rs:48:1
41+
|
42+
LL | #[repr()]
43+
| ^^^^^^^^^
44+
LL | type SirThisIsAType = i32;
45+
| -------------------------- not a struct, enum, function, associated function, or union
46+
47+
error: aborting due to 5 previous errors
3448

3549
For more information about this error, try `rustc --explain E0517`.

0 commit comments

Comments
 (0)