diff options
| author | y21 <30553356+y21@users.noreply.github.com> | 2024-09-07 17:06:32 +0200 |
|---|---|---|
| committer | y21 <30553356+y21@users.noreply.github.com> | 2024-09-07 17:18:55 +0200 |
| commit | ae5326b967d5a3d34d3c562882feeacd9839950c (patch) | |
| tree | 0fbf144b147ddd1245f07043bb27630a4a8db17a /tests/ui | |
| parent | a95afe2d0a2051d97b723b0b197393b7811bc4e4 (diff) | |
| download | rust-ae5326b967d5a3d34d3c562882feeacd9839950c.tar.gz rust-ae5326b967d5a3d34d3c562882feeacd9839950c.zip | |
visit struct fields in uninit fallback check
Diffstat (limited to 'tests/ui')
| -rw-r--r-- | tests/ui/uninit_vec.rs | 32 | ||||
| -rw-r--r-- | tests/ui/uninit_vec.stderr | 24 |
2 files changed, 55 insertions, 1 deletions
diff --git a/tests/ui/uninit_vec.rs b/tests/ui/uninit_vec.rs index 0cc77a8775d..464f88140bd 100644 --- a/tests/ui/uninit_vec.rs +++ b/tests/ui/uninit_vec.rs @@ -150,4 +150,36 @@ fn main() { vec.set_len(10); } } + + fn nested_union<T>() { + let mut vec: Vec<UnsafeCell<MaybeUninit<T>>> = Vec::with_capacity(1); + unsafe { + vec.set_len(1); + } + } + + struct Recursive<T>(*const Recursive<T>, MaybeUninit<T>); + fn recursive_union<T>() { + // Make sure we don't stack overflow on recursive types. + // The pointer acts as the base case because it can't be uninit regardless of its pointee. + + let mut vec: Vec<Recursive<T>> = Vec::with_capacity(1); + //~^ uninit_vec + unsafe { + vec.set_len(1); + } + } + + #[repr(u8)] + enum Enum<T> { + Variant(T), + } + fn union_in_enum<T>() { + // Enums can have a discriminant that can't be uninit, so this should still warn + let mut vec: Vec<Enum<T>> = Vec::with_capacity(1); + //~^ uninit_vec + unsafe { + vec.set_len(1); + } + } } diff --git a/tests/ui/uninit_vec.stderr b/tests/ui/uninit_vec.stderr index e8b77d653f0..e7c81cf792f 100644 --- a/tests/ui/uninit_vec.stderr +++ b/tests/ui/uninit_vec.stderr @@ -125,5 +125,27 @@ LL | vec.set_len(10); | = help: initialize the buffer or wrap the content in `MaybeUninit` -error: aborting due to 12 previous errors +error: calling `set_len()` immediately after reserving a buffer creates uninitialized values + --> tests/ui/uninit_vec.rs:166:9 + | +LL | let mut vec: Vec<Recursive<T>> = Vec::with_capacity(1); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | vec.set_len(1); + | ^^^^^^^^^^^^^^ + | + = help: initialize the buffer or wrap the content in `MaybeUninit` + +error: calling `set_len()` immediately after reserving a buffer creates uninitialized values + --> tests/ui/uninit_vec.rs:179:9 + | +LL | let mut vec: Vec<Enum<T>> = Vec::with_capacity(1); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | vec.set_len(1); + | ^^^^^^^^^^^^^^ + | + = help: initialize the buffer or wrap the content in `MaybeUninit` + +error: aborting due to 14 previous errors |
