Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disallow repr() on invalid items #133925

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1791,6 +1791,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 @@ -1823,24 +1851,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`.
Loading