Skip to content

Commit a421dce

Browse files
authored
Unrolled build for rust-lang#133925
Rollup merge of rust-lang#133925 - folkertdev:improve-repr-warnings, r=compiler-errors disallow `repr()` on invalid items fixes rust-lang#129606 fixes rust-lang#132391 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` cc `@jdonszelmann` who claimed rust-lang#132391 and generally has been working on attributes
2 parents 942db67 + a4bb0d4 commit a421dce

File tree

6 files changed

+107
-42
lines changed

6 files changed

+107
-42
lines changed

compiler/rustc_passes/src/check_attr.rs

+40-17
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,34 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18511851
let mut is_simd = false;
18521852
let mut is_transparent = false;
18531853

1854+
// catch `repr()` with no arguments, applied to an item (i.e. not `#![repr()]`)
1855+
if hints.is_empty() && item.is_some() {
1856+
for attr in attrs.iter().filter(|attr| attr.has_name(sym::repr)) {
1857+
match target {
1858+
Target::Struct | Target::Union | Target::Enum => {}
1859+
Target::Fn | Target::Method(_) => {
1860+
feature_err(
1861+
&self.tcx.sess,
1862+
sym::fn_align,
1863+
attr.span,
1864+
fluent::passes_repr_align_function,
1865+
)
1866+
.emit();
1867+
}
1868+
_ => {
1869+
self.dcx().emit_err(
1870+
errors::AttrApplication::StructEnumFunctionMethodUnion {
1871+
hint_span: attr.span,
1872+
span,
1873+
},
1874+
);
1875+
}
1876+
}
1877+
}
1878+
1879+
return;
1880+
}
1881+
18541882
for hint in &hints {
18551883
if !hint.is_meta_item() {
18561884
self.dcx().emit_err(errors::ReprIdent { span: hint.span() });
@@ -1883,24 +1911,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18831911
}
18841912
}
18851913
sym::align => {
1886-
if let (Target::Fn | Target::Method(MethodKind::Inherent), false) =
1887-
(target, self.tcx.features().fn_align())
1888-
{
1889-
feature_err(
1890-
&self.tcx.sess,
1891-
sym::fn_align,
1892-
hint.span(),
1893-
fluent::passes_repr_align_function,
1894-
)
1895-
.emit();
1896-
}
1897-
18981914
match target {
1899-
Target::Struct
1900-
| Target::Union
1901-
| Target::Enum
1902-
| Target::Fn
1903-
| Target::Method(_) => {}
1915+
Target::Struct | Target::Union | Target::Enum => {}
1916+
Target::Fn | Target::Method(_) => {
1917+
if !self.tcx.features().fn_align() {
1918+
feature_err(
1919+
&self.tcx.sess,
1920+
sym::fn_align,
1921+
hint.span(),
1922+
fluent::passes_repr_align_function,
1923+
)
1924+
.emit();
1925+
}
1926+
}
19041927
_ => {
19051928
self.dcx().emit_err(
19061929
errors::AttrApplication::StructEnumFunctionMethodUnion {

tests/crashes/132391.rs

-8
This file was deleted.

tests/ui/feature-gates/feature-gate-fn_align.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22

33
#[repr(align(16))] //~ ERROR `repr(align)` attributes on functions are unstable
44
fn requires_alignment() {}
5+
6+
trait MyTrait {
7+
#[repr(align)] //~ ERROR `repr(align)` attributes on functions are unstable
8+
fn myfun();
9+
}

tests/ui/feature-gates/feature-gate-fn_align.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ LL | #[repr(align(16))]
88
= help: add `#![feature(fn_align)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

11-
error: aborting due to 1 previous error
11+
error[E0658]: `repr(align)` attributes on functions are unstable
12+
--> $DIR/feature-gate-fn_align.rs:7:12
13+
|
14+
LL | #[repr(align)]
15+
| ^^^^^
16+
|
17+
= note: see issue #82232 <https://github.com/rust-lang/rust/issues/82232> for more information
18+
= help: add `#![feature(fn_align)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error: aborting due to 2 previous errors
1222

1323
For more information about this error, try `rustc --explain E0658`.

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)