Skip to content

Commit 9e0a2c1

Browse files
Rollup merge of #158034 - kevin-valerio:fix/issue-158033-reborrow-source-unsafety, r=dingxiangfei2009
Fix reborrow source expression visits Fixes #158033
2 parents 338c8c7 + ef672ad commit 9e0a2c1

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
187187
}
188188
ThreadLocalRef(_) => {}
189189
Yield { value } => visitor.visit_expr(&visitor.thir()[value]),
190-
Reborrow { .. } => {}
190+
Reborrow { source, mutability: _, target: _ } => {
191+
visitor.visit_expr(&visitor.thir()[source])
192+
}
191193
}
192194
}
193195

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Regression test for rust-lang/rust#158033.
2+
3+
#![feature(reborrow)]
4+
#![allow(dead_code)]
5+
#![deny(unsafe_code)]
6+
7+
use std::marker::Reborrow;
8+
9+
struct Thing<'a> {
10+
field: &'a mut usize,
11+
}
12+
13+
impl<'a> Reborrow for Thing<'a> {}
14+
15+
fn takes(_: Thing<'_>) {}
16+
17+
fn main() {
18+
let mut x = 0;
19+
let thing = Thing { field: &mut x };
20+
let y = 123usize;
21+
22+
takes({
23+
let p: *const usize = &y;
24+
std::hint::black_box(std::ptr::read(p));
25+
//~^ ERROR call to unsafe function `std::ptr::read` is unsafe
26+
thing
27+
});
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: call to unsafe function `std::ptr::read` is unsafe and requires unsafe function or block
2+
--> $DIR/reborrow-source-unsafety.rs:24:30
3+
|
4+
LL | std::hint::black_box(std::ptr::read(p));
5+
| ^^^^^^^^^^^^^^^^^ call to unsafe function
6+
|
7+
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)