Skip to content

Commit ee8dbbc

Browse files
committed
Auto merge of #1789 - RalfJung:array-to-elem, r=RalfJung
stacked borrows: ensure array-to-elem casts behave correctly When "as"-casting an entire array, that should create a raw ptr usable for all elements in the array, even if we immediately cast to the element type.
2 parents 67c04af + a2b227f commit ee8dbbc

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0309953232d9957aef4c7c5a24fcb30735b2066b
1+
1773f14a24c49356b384e45ebb45643bc9bef2c4

tests/run-pass/stacked-borrows/stacked-borrows.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
shr_and_raw();
1616
disjoint_mutable_subborrows();
1717
raw_ref_to_part();
18+
array_casts();
1819
}
1920

2021
// Make sure that reading from an `&mut` does, like reborrowing to `&`,
@@ -174,3 +175,14 @@ fn raw_ref_to_part() {
174175
assert!(typed.extra == 42);
175176
drop(unsafe { Box::from_raw(whole) });
176177
}
178+
179+
/// When casting an array reference to a raw element ptr, that should cover the whole array.
180+
fn array_casts() {
181+
let mut x: [usize; 2] = [0, 0];
182+
let p = &mut x as *mut usize;
183+
unsafe { *p.add(1) = 1; }
184+
185+
let x: [usize; 2] = [0, 1];
186+
let p = &x as *const usize;
187+
assert_eq!(unsafe { *p.add(1) }, 1);
188+
}

0 commit comments

Comments
 (0)