Skip to content

Commit cd138bc

Browse files
committed
test more operations on dangling ZST pointers
1 parent cf44c36 commit cd138bc

13 files changed

+48
-27
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
// This pointer *could* be NULL so we cannot load from it, not even at ZST
3+
let ptr = (&0u8 as *const u8).wrapping_sub(0x800) as *const ();
4+
let _x: () = unsafe { *ptr }; //~ ERROR outside bounds
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
// This pointer *could* be NULL so we cannot load from it, not even at ZST.
3+
// Not using the () type here, as writes of that type do not even have MIR generated.
4+
// Also not assigning directly as that's array initialization, not assignment.
5+
let zst_val = [1u8; 0];
6+
let ptr = (&0u8 as *const u8).wrapping_sub(0x800) as *mut [u8; 0];
7+
unsafe { *ptr = zst_val; } //~ ERROR outside bounds
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let x: () = unsafe { *std::ptr::null() }; //~ ERROR constant evaluation error: invalid use of NULL pointer
3+
panic!("this should never print: {:?}", x);
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
unsafe { *std::ptr::null_mut() = 0i32 }; //~ ERROR constant evaluation error: invalid use of NULL pointer
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
// Not using the () type here, as writes of that type do not even have MIR generated.
3+
// Also not assigning directly as that's array initialization, not assignment.
4+
let zst_val = [1u8; 0];
5+
unsafe { *std::ptr::null_mut() = zst_val }; //~ ERROR constant evaluation error: invalid use of NULL pointer
6+
}

tests/run-pass/cast-rfc0401-vtable-kinds.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
12-
// FIXME: remove the next line when https://github.com/rust-lang/rust/issues/43358 is resolved
13-
// compile-flags: -Zmir-opt-level=0
14-
1511
// Check that you can cast between different pointers to trait objects
1612
// whose vtable have the same kind (both lengths, or both trait pointers).
1713

tests/run-pass/const-vec-of-fns.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// pretty-expanded FIXME #23616
12-
1311
/*!
1412
* Try to double-check that static fns have the right size (with or
1513
* without dummy env ptr, as appropriate) by iterating a size-2 array.

tests/run-pass/issue-20575.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// Test that overloaded calls work with zero arity closures
1212

13-
// pretty-expanded FIXME #23616
14-
1513
fn main() {
1614
let functions: [Box<Fn() -> Option<()>>; 1] = [Box::new(|| None)];
1715

tests/run-pass/pointers.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ fn main() {
5555
assert_eq!(basic_ref_mut_var(), 3);
5656
assert_eq!(tuple_ref_mut(), (10, 22));
5757
assert_eq!(match_ref_mut(), 42);
58-
// FIXME: improve this test... how?
58+
59+
// Compare even dangling pointers with NULL, and with others in the same allocation.
5960
assert!(dangling_pointer() != std::ptr::null());
6061
assert!(match dangling_pointer() as usize { 0 => false, _ => true });
62+
let dangling = dangling_pointer();
63+
assert!(dangling == dangling);
64+
assert!(dangling.wrapping_add(1) != dangling);
6165
}

tests/run-pass/regions-lifetime-nonfree-late-bound.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
// doing region-folding, when really all clients of the region-folding
2323
// case only want to see FREE lifetime variables, not bound ones.
2424

25-
// pretty-expanded FIXME #23616
26-
2725
#![allow(unused_features)]
2826
#![feature(box_syntax)]
2927

tests/run-pass/sendable-class.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// Test that a class with only sendable fields can be sent
1212

13-
// pretty-expanded FIXME #23616
14-
1513
use std::sync::mpsc::channel;
1614

1715
#[allow(dead_code)]

tests/run-pass/zst.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,23 @@ fn use_zst() -> A {
1111
}
1212

1313
fn main() {
14+
// Not using the () type here, as writes of that type do not even have MIR generated.
15+
// Also not assigning directly as that's array initialization, not assignment.
16+
let zst_val = [1u8; 0];
17+
1418
assert_eq!(zst_ret(), A);
1519
assert_eq!(use_zst(), A);
16-
let x = 42 as *mut ();
17-
unsafe { *x = (); }
20+
let x = 42 as *mut [u8; 0];
21+
// reading and writing is okay
22+
unsafe { *x = zst_val; }
23+
unsafe { let _y = *x; }
24+
25+
// We should even be able to use "true" pointers for ZST when the allocation has been
26+
// removed already. The box is for a non-ZST to make sure there actually is an allocation.
27+
let mut x_box = Box::new(((), 1u8));
28+
let x = &mut x_box.0 as *mut _ as *mut [u8; 0];
29+
drop(x_box);
30+
// reading and writing is okay
31+
unsafe { *x = zst_val; }
32+
unsafe { let _y = *x; }
1833
}

tests/run-pass/zst2.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)