Skip to content

Commit

Permalink
Unrolled build for rust-lang#133925
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rust-timer authored Feb 7, 2025
2 parents 942db67 + a4bb0d4 commit a421dce
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 42 deletions.
57 changes: 40 additions & 17 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,34 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
let mut is_simd = false;
let mut is_transparent = false;

// catch `repr()` with no arguments, applied to an item (i.e. not `#![repr()]`)
if hints.is_empty() && item.is_some() {
for attr in attrs.iter().filter(|attr| attr.has_name(sym::repr)) {
match target {
Target::Struct | Target::Union | Target::Enum => {}
Target::Fn | Target::Method(_) => {
feature_err(
&self.tcx.sess,
sym::fn_align,
attr.span,
fluent::passes_repr_align_function,
)
.emit();
}
_ => {
self.dcx().emit_err(
errors::AttrApplication::StructEnumFunctionMethodUnion {
hint_span: attr.span,
span,
},
);
}
}
}

return;
}

for hint in &hints {
if !hint.is_meta_item() {
self.dcx().emit_err(errors::ReprIdent { span: hint.span() });
Expand Down Expand Up @@ -1883,24 +1911,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}
sym::align => {
if let (Target::Fn | Target::Method(MethodKind::Inherent), false) =
(target, self.tcx.features().fn_align())
{
feature_err(
&self.tcx.sess,
sym::fn_align,
hint.span(),
fluent::passes_repr_align_function,
)
.emit();
}

match target {
Target::Struct
| Target::Union
| Target::Enum
| Target::Fn
| Target::Method(_) => {}
Target::Struct | Target::Union | Target::Enum => {}
Target::Fn | Target::Method(_) => {
if !self.tcx.features().fn_align() {
feature_err(
&self.tcx.sess,
sym::fn_align,
hint.span(),
fluent::passes_repr_align_function,
)
.emit();
}
}
_ => {
self.dcx().emit_err(
errors::AttrApplication::StructEnumFunctionMethodUnion {
Expand Down
8 changes: 0 additions & 8 deletions tests/crashes/132391.rs

This file was deleted.

5 changes: 5 additions & 0 deletions tests/ui/feature-gates/feature-gate-fn_align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

#[repr(align(16))] //~ ERROR `repr(align)` attributes on functions are unstable
fn requires_alignment() {}

trait MyTrait {
#[repr(align)] //~ ERROR `repr(align)` attributes on functions are unstable
fn myfun();
}
12 changes: 11 additions & 1 deletion tests/ui/feature-gates/feature-gate-fn_align.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ LL | #[repr(align(16))]
= help: add `#![feature(fn_align)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

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

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
31 changes: 26 additions & 5 deletions tests/ui/repr/attr-usage-repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,39 @@ struct SSimd([f64; 2]);
struct SInt(f64, f64);

#[repr(C)]
enum EExtern { A, B }
enum EExtern {
A,
B,
}

#[repr(align(8))]
enum EAlign { A, B }
enum EAlign {
A,
B,
}

#[repr(packed)] //~ ERROR: attribute should be applied to a struct
enum EPacked { A, B }
enum EPacked {
A,
B,
}

#[repr(simd)] //~ ERROR: attribute should be applied to a struct
enum ESimd { A, B }
enum ESimd {
A,
B,
}

#[repr(i8)]
enum EInt { A, B }
enum EInt {
A,
B,
}

#[repr()] //~ attribute should be applied to a struct, enum, function, associated function, or union [E0517]
type SirThisIsAType = i32;

#[repr()]
struct EmptyReprArgumentList(i32);

fn main() {}
36 changes: 25 additions & 11 deletions tests/ui/repr/attr-usage-repr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,35 @@ LL | struct SInt(f64, f64);
| ---------------------- not an enum

error[E0517]: attribute should be applied to a struct or union
--> $DIR/attr-usage-repr.rs:24:8
--> $DIR/attr-usage-repr.rs:30:8
|
LL | #[repr(packed)]
| ^^^^^^
LL | enum EPacked { A, B }
| --------------------- not a struct or union
LL | #[repr(packed)]
| ^^^^^^
LL | / enum EPacked {
LL | | A,
LL | | B,
LL | | }
| |_- not a struct or union

error[E0517]: attribute should be applied to a struct
--> $DIR/attr-usage-repr.rs:27:8
--> $DIR/attr-usage-repr.rs:36:8
|
LL | #[repr(simd)]
| ^^^^
LL | enum ESimd { A, B }
| ------------------- not a struct
LL | #[repr(simd)]
| ^^^^
LL | / enum ESimd {
LL | | A,
LL | | B,
LL | | }
| |_- not a struct

error: aborting due to 4 previous errors
error[E0517]: attribute should be applied to a struct, enum, function, associated function, or union
--> $DIR/attr-usage-repr.rs:48:1
|
LL | #[repr()]
| ^^^^^^^^^
LL | type SirThisIsAType = i32;
| -------------------------- not a struct, enum, function, associated function, or union

error: aborting due to 5 previous errors

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

0 comments on commit a421dce

Please sign in to comment.