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

Mark unions non-const-propagatable in KnownPanicsLint without calling layout #124504

Merged
merged 1 commit into from
Apr 29, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions compiler/rustc_mir_transform/src/known_panics_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,13 +896,19 @@ impl CanConstProp {
};
for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
let ty = body.local_decls[local].ty;
match tcx.layout_of(param_env.and(ty)) {
Ok(layout) if layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) => {}
// Either the layout fails to compute, then we can't use this local anyway
// or the local is too large, then we don't want to.
_ => {
*val = ConstPropMode::NoPropagation;
continue;
if ty.is_union() {
// Do not const prop unions as they can
// ICE during layout calc
gurry marked this conversation as resolved.
Show resolved Hide resolved
*val = ConstPropMode::NoPropagation;
} else {
match tcx.layout_of(param_env.and(ty)) {
Ok(layout) if layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) => {}
// Either the layout fails to compute, then we can't use this local anyway
// or the local is too large, then we don't want to.
_ => {
*val = ConstPropMode::NoPropagation;
continue;
}
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions tests/crashes/123710.rs

This file was deleted.

22 changes: 22 additions & 0 deletions tests/ui/lint/ice-const-prop-unions-known-panics-lint-123710.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Regression test for issue 123710.
// Tests that the we do not ICE in KnownPanicsLint
// when a union contains an enum with an repr(packed),
// which is a repr not supported for enums

#[repr(packed)]
//~^ ERROR attribute should be applied to a struct or union
#[repr(u32)]
enum E {
A,
B,
C,
}

fn main() {
union InvalidTag {
int: u32,
e: E,
//~^ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
}
let _invalid_tag = InvalidTag { int: 4 };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error[E0517]: attribute should be applied to a struct or union
--> $DIR/ice-const-prop-unions-known-panics-lint-123710.rs:6:8
|
LL | #[repr(packed)]
| ^^^^^^
...
LL | / enum E {
LL | | A,
LL | | B,
LL | | C,
LL | | }
| |_- not a struct or union

error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/ice-const-prop-unions-known-panics-lint-123710.rs:18:9
|
LL | e: E,
| ^^^^
|
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: wrap the field type in `ManuallyDrop<...>`
|
LL | e: std::mem::ManuallyDrop<E>,
| +++++++++++++++++++++++ +

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0517, E0740.
For more information about an error, try `rustc --explain E0517`.