Skip to content

Commit 7cc4c21

Browse files
Stevengreclaude
andcommitted
test: add pointer/alignment test cases from PRs #812, #877, #1004
Consolidate test coverage from superseded PRs into this branch: - interior-mut3.rs (from #812) - ptr_offset.rs (from #877) - alignment-check.rs, local-raw.rs, ptr-through-wrapper.rs, raw-ptr-cast.rs, ref-ptr-cast-elem.rs, ref-ptr-cast-elem-offset.rs (from #1004) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2c3f474 commit 7cc4c21

8 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Test for issue #638: alignment check in raw pointer dereference.
2+
// The compiler inserts an alignment check that transmutes a pointer to usize.
3+
// Verifies that the transmute cast evaluates concretely.
4+
5+
struct Thing { payload: i16 }
6+
7+
fn main() {
8+
let a = [Thing { payload: 1 }, Thing { payload: 2 }, Thing { payload: 3 }];
9+
let p = &a as *const Thing;
10+
let p1 = unsafe { p.add(1) };
11+
12+
let two = unsafe { (*p1).payload };
13+
assert!(two == 2);
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::cell::UnsafeCell;
2+
3+
fn main() {
4+
let data = UnsafeCell::new(0);
5+
6+
unsafe {
7+
*data.get() += 42;
8+
}
9+
10+
assert!(data.into_inner() == 42)
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let mut x: u32 = 3;
3+
let y: *mut u32 = &mut x;
4+
unsafe { *y = 0; }
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct Wrapper<T>(T);
2+
3+
fn main() {
4+
let i = 32_i32;
5+
6+
unsafe {
7+
let p_i = &i as *const i32;
8+
assert!(32 == *p_i);
9+
10+
let p_w = p_i as *const Wrapper<i32>;
11+
assert!(32 == (*p_w).0);
12+
13+
let p_ww = p_w as *const Wrapper<Wrapper<i32>>;
14+
assert!(32 == (*p_ww).0.0);
15+
16+
let p_ii = p_ww as *const i32;
17+
assert!(32 == *p_ii);
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct Thing {payload: i16}
2+
3+
fn main() {
4+
5+
let a = [Thing{payload: 1}, Thing{payload: 2}, Thing{payload:3}];
6+
let p = &a as *const Thing;
7+
let p1 = unsafe { p.add(1) };
8+
9+
let two = unsafe { (*p1).payload };
10+
assert!(two == 2);
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
let mut data = 11;
3+
let ptr = &data as *const i32;
4+
5+
let ptr_mut = &mut data as *mut i32;
6+
unsafe {
7+
(*ptr_mut) = 44;
8+
assert_eq!(44, *ptr);
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ptr-cast-elem-offset-fail.rs
2+
fn main() {
3+
let arr: [i32; 2] = [42, 84];
4+
let r_arr = &arr;
5+
let p_arr = r_arr as *const i32;
6+
7+
unsafe {
8+
let p_arr_offset = p_arr.offset(1);
9+
assert!(*p_arr_offset == 84);
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
let arr: [i32; 1] = [42];
3+
let r_arr = &arr;
4+
let p_arr = r_arr as *const i32;
5+
6+
unsafe {
7+
assert!(*p_arr == 42);
8+
}
9+
}

0 commit comments

Comments
 (0)