diff --git a/src/test/ui/ptr_ops/issue-80309-safe.rs b/src/test/ui/ptr_ops/issue-80309-safe.rs new file mode 100644 index 00000000000..ca3778aab2d --- /dev/null +++ b/src/test/ui/ptr_ops/issue-80309-safe.rs @@ -0,0 +1,17 @@ +// run-pass +// min-llvm-version: 13.0 +// compiler-flags: -O + +// Regression test for issue #80309 + +pub fn zero(x: usize) -> usize { + std::ptr::null::().wrapping_add(x) as usize - x +} +pub fn qux(x: &[i8]) -> i8 { + x[zero(x.as_ptr() as usize)] +} + +fn main() { + let z = vec![42, 43]; + println!("{}", qux(&z)); +} diff --git a/src/test/ui/ptr_ops/issue-80309.rs b/src/test/ui/ptr_ops/issue-80309.rs new file mode 100644 index 00000000000..bbec1012082 --- /dev/null +++ b/src/test/ui/ptr_ops/issue-80309.rs @@ -0,0 +1,14 @@ +// run-pass +// min-llvm-version: 13.0 +// compiler-flags: -O + +// Regression test for issue #80309 + +pub unsafe fn foo(x: *const i8) -> i8 { + *x.wrapping_sub(x as _).wrapping_add(x as _) +} + +fn main() { + let x = 42; + println!("{}", unsafe { foo(&x) }); +}