Skip to content

Commit

Permalink
Auto merge of #129422 - compiler-errors:repr-rust, r=<try>
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Aug 22, 2024
2 parents 5ad98b4 + 363addc commit 0edd13c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
9 changes: 9 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
match hint.name_or_empty() {
sym::Rust => {
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 => {
is_c = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,28 @@ mod repr {
//~| 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() {}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ LL | |
LL | | }
| |_- 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
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:26:1
|
Expand Down Expand Up @@ -329,7 +344,31 @@ error[E0517]: attribute should be applied to a struct, enum, or union
LL | #[repr(C)] impl S { }
| ^ ---------- 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.
For more information about an error, try `rustc --explain E0517`.

0 comments on commit 0edd13c

Please sign in to comment.