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).
This commit is contained in:
Jubilee 2024-09-18 14:32:24 -07:00 committed by GitHub
commit 2eb65a6667
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 73 additions and 1 deletions

View File

@ -1779,6 +1779,15 @@ fn check_repr(
match hint.name_or_empty() { match hint.name_or_empty() {
sym::Rust => { sym::Rust => {
is_explicit_rust = true; is_explicit_rust = true;
match target {
Target::Struct | Target::Union | Target::Enum => continue,
_ => {
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
hint_span: hint.span(),
span,
});
}
}
} }
sym::C => { sym::C => {
is_c = true; is_c = true;

View File

@ -164,4 +164,28 @@ mod inner { #![repr(C)] }
//~| NOTE not a struct, enum, or union //~| NOTE not a struct, enum, or union
} }
#[repr(Rust)]
//~^ ERROR: attribute should be applied to a struct, enum, or union
mod repr_rust {
//~^ NOTE not a struct, enum, or union
mod inner { #![repr(Rust)] }
//~^ ERROR: attribute should be applied to a struct, enum, or union
//~| NOTE not a struct, enum, or union
#[repr(Rust)] fn f() { }
//~^ ERROR: attribute should be applied to a struct, enum, or union
//~| NOTE not a struct, enum, or union
struct S;
#[repr(Rust)] type T = S;
//~^ ERROR: attribute should be applied to a struct, enum, or union
//~| NOTE not a struct, enum, or union
#[repr(Rust)] impl S { }
//~^ ERROR: attribute should be applied to a struct, enum, or union
//~| NOTE not a struct, enum, or union
}
fn main() {} fn main() {}

View File

@ -107,6 +107,21 @@ LL | |
LL | | } LL | | }
| |_- not a struct, enum, or union | |_- not a struct, enum, or union
error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:168:8
|
LL | #[repr(Rust)]
| ^^^^
LL |
LL | / mod repr_rust {
LL | |
LL | | mod inner { #![repr(Rust)] }
LL | |
... |
LL | |
LL | | }
| |_- not a struct, enum, or union
error: attribute should be applied to an `extern crate` item error: attribute should be applied to an `extern crate` item
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:26:1 --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:26:1
| |
@ -329,7 +344,31 @@ error[E0517]: attribute should be applied to a struct, enum, or union
LL | #[repr(C)] impl S { } LL | #[repr(C)] impl S { }
| ^ ---------- not a struct, enum, or union | ^ ---------- not a struct, enum, or union
error: aborting due to 39 previous errors error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:172:25
|
LL | mod inner { #![repr(Rust)] }
| --------------------^^^^---- not a struct, enum, or union
error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:176:12
|
LL | #[repr(Rust)] fn f() { }
| ^^^^ ---------- not a struct, enum, or union
error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:182:12
|
LL | #[repr(Rust)] type T = S;
| ^^^^ ----------- not a struct, enum, or union
error[E0517]: attribute should be applied to a struct, enum, or union
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:186:12
|
LL | #[repr(Rust)] impl S { }
| ^^^^ ---------- not a struct, enum, or union
error: aborting due to 44 previous errors
Some errors have detailed explanations: E0517, E0518, E0658. Some errors have detailed explanations: E0517, E0518, E0658.
For more information about an error, try `rustc --explain E0517`. For more information about an error, try `rustc --explain E0517`.