-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Error out of layout calculation if a non-last struct field is unsized #121546
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
79 changes: 79 additions & 0 deletions
79
tests/ui/layout/ice-non-last-unsized-field-issue-121473.rs
This file contains hidden or 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,79 @@ | ||
// 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], | ||
} | ||
|
||
enum BadEnum1 { | ||
Variant1 { | ||
field1: i32, | ||
field2: str, // Unsized | ||
//~^ ERROR the size for values of type `str` cannot be known at compilation time | ||
field3: [u8; 16], | ||
}, | ||
} | ||
|
||
enum BadEnum2 { | ||
Variant1( | ||
i32, | ||
str, // Unsized | ||
//~^ ERROR the size for values of type `str` cannot be known at compilation time | ||
[u8; 16] | ||
), | ||
} | ||
|
||
enum BadEnumMultiVariant { | ||
Variant1(i32), | ||
Variant2 { | ||
field1: i32, | ||
field2: str, // Unsized | ||
//~^ ERROR the size for values of type `str` cannot be known at compilation time | ||
field3: [u8; 16], | ||
}, | ||
Variant3 | ||
} | ||
|
||
union BadUnion { | ||
field1: i32, | ||
field2: str, // Unsized | ||
//~^ ERROR the size for values of type `str` cannot be known at compilation time | ||
//~| ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union | ||
field3: [u8; 16], | ||
} | ||
|
||
// Used to test that projection type fields that normalize | ||
// to a sized type do not cause problems | ||
struct StructWithProjections<'a> | ||
{ | ||
field1: <&'a [i32] as IntoIterator>::IntoIter, | ||
field2: i32 | ||
} | ||
|
||
pub fn main() { | ||
let _a = &size_of::<BadStruct>(); | ||
assert_eq!(size_of::<BadStruct>(), 21); | ||
|
||
let _a = &size_of::<BadEnum1>(); | ||
assert_eq!(size_of::<BadEnum1>(), 21); | ||
|
||
let _a = &size_of::<BadEnum2>(); | ||
assert_eq!(size_of::<BadEnum2>(), 21); | ||
|
||
let _a = &size_of::<BadEnumMultiVariant>(); | ||
assert_eq!(size_of::<BadEnumMultiVariant>(), 21); | ||
|
||
let _a = &size_of::<BadUnion>(); | ||
assert_eq!(size_of::<BadUnion>(), 21); | ||
|
||
let _a = &size_of::<StructWithProjections>(); | ||
assert_eq!(size_of::<StructWithProjections>(), 21); | ||
let _a = StructWithProjections { field1: [1, 3].iter(), field2: 3 }; | ||
} |
106 changes: 106 additions & 0 deletions
106
tests/ui/layout/ice-non-last-unsized-field-issue-121473.stderr
This file contains hidden or 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,106 @@ | ||
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[E0277]: the size for values of type `str` cannot be known at compilation time | ||
--> $DIR/ice-non-last-unsized-field-issue-121473.rs:18:17 | ||
| | ||
LL | field2: str, // Unsized | ||
| ^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `str` | ||
= note: no field of an enum variant 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 | field2: &str, // Unsized | ||
| + | ||
help: the `Box` type always has a statically known size and allocates its contents in the heap | ||
| | ||
LL | field2: Box<str>, // Unsized | ||
| ++++ + | ||
|
||
error[E0277]: the size for values of type `str` cannot be known at compilation time | ||
--> $DIR/ice-non-last-unsized-field-issue-121473.rs:27:9 | ||
| | ||
LL | str, // Unsized | ||
| ^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `str` | ||
= note: no field of an enum variant 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 | &str, // Unsized | ||
| + | ||
help: the `Box` type always has a statically known size and allocates its contents in the heap | ||
| | ||
LL | Box<str>, // Unsized | ||
| ++++ + | ||
|
||
error[E0277]: the size for values of type `str` cannot be known at compilation time | ||
--> $DIR/ice-non-last-unsized-field-issue-121473.rs:37:17 | ||
| | ||
LL | field2: str, // Unsized | ||
| ^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `str` | ||
= note: no field of an enum variant 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 | field2: &str, // Unsized | ||
| + | ||
help: the `Box` type always has a statically known size and allocates its contents in the heap | ||
| | ||
LL | field2: Box<str>, // Unsized | ||
| ++++ + | ||
|
||
error[E0277]: the size for values of type `str` cannot be known at compilation time | ||
--> $DIR/ice-non-last-unsized-field-issue-121473.rs:46:13 | ||
| | ||
LL | field2: str, // Unsized | ||
| ^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `str` | ||
= note: no field of a union 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 | field2: &str, // Unsized | ||
| + | ||
help: the `Box` type always has a statically known size and allocates its contents in the heap | ||
| | ||
LL | field2: Box<str>, // Unsized | ||
| ++++ + | ||
|
||
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union | ||
--> $DIR/ice-non-last-unsized-field-issue-121473.rs:46:5 | ||
| | ||
LL | field2: str, // Unsized | ||
| ^^^^^^^^^^^ | ||
| | ||
= 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 | field2: std::mem::ManuallyDrop<str>, // Unsized | ||
| +++++++++++++++++++++++ + | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0740. | ||
For more information about an error, try `rustc --explain E0277`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.