diff options
| author | Ralf Jung <post@ralfj.de> | 2025-06-27 12:47:47 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2025-06-27 14:39:35 +0200 |
| commit | ed4f01ed2e033098f6c5e10a4a0cc740dc04f958 (patch) | |
| tree | 15991ee4b0a5dfcd7d04ec09b2a48681ded67293 /tests/ui/statics | |
| parent | e61dd437f33b5a640e67dc3628397689c664c17f (diff) | |
| download | rust-ed4f01ed2e033098f6c5e10a4a0cc740dc04f958.tar.gz rust-ed4f01ed2e033098f6c5e10a4a0cc740dc04f958.zip | |
const-eval: error when initializing a static writes to that static
Diffstat (limited to 'tests/ui/statics')
| -rw-r--r-- | tests/ui/statics/read_before_init.rs | 10 | ||||
| -rw-r--r-- | tests/ui/statics/read_before_init.stderr | 28 |
2 files changed, 27 insertions, 11 deletions
diff --git a/tests/ui/statics/read_before_init.rs b/tests/ui/statics/read_before_init.rs index d779ef6dffa..32cc2554e1a 100644 --- a/tests/ui/statics/read_before_init.rs +++ b/tests/ui/statics/read_before_init.rs @@ -8,13 +8,15 @@ use std::mem::MaybeUninit; -pub static X: (i32, MaybeUninit<i32>) = (1, foo(&X.0)); -//~^ ERROR: encountered static that tried to initialize itself with itself +pub static X: (i32, MaybeUninit<i32>) = (1, foo(&X.0, 1)); +//~^ ERROR: encountered static that tried to access itself during initialization +pub static Y: (i32, MaybeUninit<i32>) = (1, foo(&Y.0, 0)); +//~^ ERROR: encountered static that tried to access itself during initialization -const fn foo(x: &i32) -> MaybeUninit<i32> { +const fn foo(x: &i32, num: usize) -> MaybeUninit<i32> { let mut temp = MaybeUninit::<i32>::uninit(); unsafe { - std::ptr::copy(x, temp.as_mut_ptr(), 1); + std::ptr::copy(x, temp.as_mut_ptr(), num); } temp } diff --git a/tests/ui/statics/read_before_init.stderr b/tests/ui/statics/read_before_init.stderr index aeebcf7d9ce..239568c1205 100644 --- a/tests/ui/statics/read_before_init.stderr +++ b/tests/ui/statics/read_before_init.stderr @@ -1,17 +1,31 @@ -error[E0080]: encountered static that tried to initialize itself with itself +error[E0080]: encountered static that tried to access itself during initialization --> $DIR/read_before_init.rs:11:45 | -LL | pub static X: (i32, MaybeUninit<i32>) = (1, foo(&X.0)); - | ^^^^^^^^^ evaluation of `X` failed inside this call +LL | pub static X: (i32, MaybeUninit<i32>) = (1, foo(&X.0, 1)); + | ^^^^^^^^^^^^ evaluation of `X` failed inside this call | note: inside `foo` - --> $DIR/read_before_init.rs:17:9 + --> $DIR/read_before_init.rs:19:9 | -LL | std::ptr::copy(x, temp.as_mut_ptr(), 1); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | std::ptr::copy(x, temp.as_mut_ptr(), num); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: inside `std::ptr::copy::<i32>` --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL -error: aborting due to 1 previous error +error[E0080]: encountered static that tried to access itself during initialization + --> $DIR/read_before_init.rs:13:45 + | +LL | pub static Y: (i32, MaybeUninit<i32>) = (1, foo(&Y.0, 0)); + | ^^^^^^^^^^^^ evaluation of `Y` failed inside this call + | +note: inside `foo` + --> $DIR/read_before_init.rs:19:9 + | +LL | std::ptr::copy(x, temp.as_mut_ptr(), num); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: inside `std::ptr::copy::<i32>` + --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0080`. |
