Skip to content

Commit 2eb65a6

Browse files
Rollup merge of #129422 - compiler-errors:repr-rust, r=fmease
Gate `repr(Rust)` correctly on non-ADT items #114201 added `repr(Rust)` but didn't add any attribute validation to it like `repr(C)` has, to only allow it on ADT items. I consider this code to be nonsense, for example: ``` #[repr(Rust)] fn foo() {} ``` Reminder that it's different from `extern "Rust"`, which *is* valid on function items. But also this now disallows `repr(Rust)` on modules, impls, traits, etc. I'll crater it, if it looks bad then I'll add an FCW. --- https://github.com/rust-lang/rust/labels/relnotes: Compatibility (minor breaking change).
2 parents 2a1dd35 + 363addc commit 2eb65a6

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

compiler/rustc_passes/src/check_attr.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17791779
match hint.name_or_empty() {
17801780
sym::Rust => {
17811781
is_explicit_rust = true;
1782+
match target {
1783+
Target::Struct | Target::Union | Target::Enum => continue,
1784+
_ => {
1785+
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1786+
hint_span: hint.span(),
1787+
span,
1788+
});
1789+
}
1790+
}
17821791
}
17831792
sym::C => {
17841793
is_c = true;

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs

+24
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,28 @@ mod repr {
164164
//~| NOTE not a struct, enum, or union
165165
}
166166

167+
168+
#[repr(Rust)]
169+
//~^ ERROR: attribute should be applied to a struct, enum, or union
170+
mod repr_rust {
171+
//~^ NOTE not a struct, enum, or union
172+
mod inner { #![repr(Rust)] }
173+
//~^ ERROR: attribute should be applied to a struct, enum, or union
174+
//~| NOTE not a struct, enum, or union
175+
176+
#[repr(Rust)] fn f() { }
177+
//~^ ERROR: attribute should be applied to a struct, enum, or union
178+
//~| NOTE not a struct, enum, or union
179+
180+
struct S;
181+
182+
#[repr(Rust)] type T = S;
183+
//~^ ERROR: attribute should be applied to a struct, enum, or union
184+
//~| NOTE not a struct, enum, or union
185+
186+
#[repr(Rust)] impl S { }
187+
//~^ ERROR: attribute should be applied to a struct, enum, or union
188+
//~| NOTE not a struct, enum, or union
189+
}
190+
167191
fn main() {}

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr

+40-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ LL | |
107107
LL | | }
108108
| |_- not a struct, enum, or union
109109

110+
error[E0517]: attribute should be applied to a struct, enum, or union
111+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:168:8
112+
|
113+
LL | #[repr(Rust)]
114+
| ^^^^
115+
LL |
116+
LL | / mod repr_rust {
117+
LL | |
118+
LL | | mod inner { #![repr(Rust)] }
119+
LL | |
120+
... |
121+
LL | |
122+
LL | | }
123+
| |_- not a struct, enum, or union
124+
110125
error: attribute should be applied to an `extern crate` item
111126
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:26:1
112127
|
@@ -329,7 +344,31 @@ error[E0517]: attribute should be applied to a struct, enum, or union
329344
LL | #[repr(C)] impl S { }
330345
| ^ ---------- not a struct, enum, or union
331346

332-
error: aborting due to 39 previous errors
347+
error[E0517]: attribute should be applied to a struct, enum, or union
348+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:172:25
349+
|
350+
LL | mod inner { #![repr(Rust)] }
351+
| --------------------^^^^---- not a struct, enum, or union
352+
353+
error[E0517]: attribute should be applied to a struct, enum, or union
354+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:176:12
355+
|
356+
LL | #[repr(Rust)] fn f() { }
357+
| ^^^^ ---------- not a struct, enum, or union
358+
359+
error[E0517]: attribute should be applied to a struct, enum, or union
360+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:182:12
361+
|
362+
LL | #[repr(Rust)] type T = S;
363+
| ^^^^ ----------- not a struct, enum, or union
364+
365+
error[E0517]: attribute should be applied to a struct, enum, or union
366+
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:186:12
367+
|
368+
LL | #[repr(Rust)] impl S { }
369+
| ^^^^ ---------- not a struct, enum, or union
370+
371+
error: aborting due to 44 previous errors
333372

334373
Some errors have detailed explanations: E0517, E0518, E0658.
335374
For more information about an error, try `rustc --explain E0517`.

0 commit comments

Comments
 (0)