rust icon indicating copy to clipboard operation
rust copied to clipboard

`#[repr()]` is allowed where it shouldn't

Open Kyuuhachi opened this issue 1 year ago • 2 comments

I tried this code:

#[repr()] const CONST: u32 = 8;
#[repr()] struct Struct;
#[repr()] trait Trait {}
#[repr()] impl Trait for Struct {}
#[repr()] fn func() { #[repr()] let a = 4; }

I expected to see this happen: #[repr] doesn't make sense on other than struct/enum/union, so it shouldn't be permitted. Every case except the struct should be an error. Not sure if that one ought to be legal either, but I guess the current warning is enough there.

Instead, this happened: Plenty of unused attribute warnings, but it compiles.

Meta

Tested on playground, both 1.80.1 and 1.82.0-nightly (2024-08-24 f167efad2f51088d8618).

Kyuuhachi avatar Aug 26 '24 10:08 Kyuuhachi

cc #128444 (which is copied over to #128581)

jieyouxu avatar Aug 26 '24 10:08 jieyouxu

If someone wants to fix this they should check if there's an RFC for #[repr] attribute and any follow-up T-lang discussions/decisions. The PR should also probably also be nominated for T-lang signoff.

jieyouxu avatar Aug 26 '24 10:08 jieyouxu

I'll fold this into #129422 (or probably just do it as a follow-up)

compiler-errors avatar Aug 26 '24 15:08 compiler-errors

If someone wants to fix this they should check if there's an RFC for #[repr] attribute and any follow-up T-lang discussions/decisions. The PR should also probably also be nominated for T-lang signoff.

#[repr()] on functions should be gated behind fn_align: https://github.com/rust-lang/rust/issues/82232

Otherwise #[repr()] should only be allowed on ADTs.

compiler-errors avatar Aug 26 '24 15:08 compiler-errors