-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error out of layout calculation if a non-last struct field is unsized
Fixes an ICE that occurs when a struct with an unsized field at a non-last position is const evaluated.
- Loading branch information
Showing
3 changed files
with
62 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/ui/layout/ice-non-last-unsized-field-issue-121473.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Regression test for #121473 | ||
// Checks that no ICE occurs when `size_of` | ||
// is applied to a struct that has an unsized | ||
// field which is not its last field | ||
|
||
use std::mem::size_of; | ||
|
||
pub struct BadStruct { | ||
pub field1: i32, | ||
pub field2: str, // Unsized field that is not the last field | ||
//~^ ERROR the size for values of type `str` cannot be known at compilation time | ||
pub field3: [u8; 16], | ||
} | ||
|
||
pub fn main() { | ||
// The ICE occurs only in promoted MIR | ||
let _x = &size_of::<BadStruct>(); | ||
assert_eq!(size_of::<BadStruct>(), 21); | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/ui/layout/ice-non-last-unsized-field-issue-121473.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error[E0277]: the size for values of type `str` cannot be known at compilation time | ||
--> $DIR/ice-non-last-unsized-field-issue-121473.rs:10:17 | ||
| | ||
LL | pub field2: str, // Unsized field that is not the last field | ||
| ^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `str` | ||
= note: only the last field of a struct may have a dynamically sized type | ||
= help: change the field's type to have a statically known size | ||
help: borrowed types always have a statically known size | ||
| | ||
LL | pub field2: &str, // Unsized field that is not the last field | ||
| + | ||
help: the `Box` type always has a statically known size and allocates its contents in the heap | ||
| | ||
LL | pub field2: Box<str>, // Unsized field that is not the last field | ||
| ++++ + | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |