Skip to content

Commit 9576ee9

Browse files
authored
Rollup merge of #80477 - tmiasko:safe-forget, r=oli-obk
Make forget intrinsic safe
2 parents bdc215a + 5718cc2 commit 9576ee9

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

compiler/rustc_typeck/src/check/intrinsic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub fn intrinsic_operation_unsafety(intrinsic: Symbol) -> hir::Unsafety {
9292
| sym::rustc_peek
9393
| sym::maxnumf64
9494
| sym::type_name
95+
| sym::forget
9596
| sym::variant_count => hir::Unsafety::Normal,
9697
_ => hir::Unsafety::Unsafe,
9798
}

library/core/src/mem/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,14 @@ pub const fn forget<T>(t: T) {
151151
#[inline]
152152
#[unstable(feature = "forget_unsized", issue = "none")]
153153
pub fn forget_unsized<T: ?Sized>(t: T) {
154+
#[cfg(bootstrap)]
154155
// SAFETY: the forget intrinsic could be safe, but there's no point in making it safe since
155156
// we'll be implementing this function soon via `ManuallyDrop`
156-
unsafe { intrinsics::forget(t) }
157+
unsafe {
158+
intrinsics::forget(t)
159+
}
160+
#[cfg(not(bootstrap))]
161+
intrinsics::forget(t)
157162
}
158163

159164
/// Returns the size of a type in bytes.

src/test/mir-opt/lower_intrinsics.forget.LowerIntrinsics.diff

+9-15
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,21 @@
44
fn forget(_1: T) -> () {
55
debug t => _1; // in scope 0 at $DIR/lower_intrinsics.rs:18:18: 18:19
66
let mut _0: (); // return place in scope 0 at $DIR/lower_intrinsics.rs:18:24: 18:24
7-
let _2: (); // in scope 0 at $DIR/lower_intrinsics.rs:19:14: 19:41
8-
let mut _3: T; // in scope 0 at $DIR/lower_intrinsics.rs:19:39: 19:40
9-
scope 1 {
10-
}
7+
let mut _2: T; // in scope 0 at $DIR/lower_intrinsics.rs:19:30: 19:31
118

129
bb0: {
13-
StorageLive(_2); // scope 0 at $DIR/lower_intrinsics.rs:19:5: 19:43
14-
StorageLive(_3); // scope 1 at $DIR/lower_intrinsics.rs:19:39: 19:40
15-
_3 = move _1; // scope 1 at $DIR/lower_intrinsics.rs:19:39: 19:40
16-
- _2 = std::intrinsics::forget::<T>(move _3) -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:19:14: 19:41
10+
StorageLive(_2); // scope 0 at $DIR/lower_intrinsics.rs:19:30: 19:31
11+
_2 = move _1; // scope 0 at $DIR/lower_intrinsics.rs:19:30: 19:31
12+
- _0 = std::intrinsics::forget::<T>(move _2) -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:19:5: 19:32
1713
- // mir::Constant
18-
- // + span: $DIR/lower_intrinsics.rs:19:14: 19:38
19-
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(T) {std::intrinsics::forget::<T>}, val: Value(Scalar(<ZST>)) }
20-
+ _2 = const (); // scope 1 at $DIR/lower_intrinsics.rs:19:14: 19:41
21-
+ goto -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:19:14: 19:41
14+
- // + span: $DIR/lower_intrinsics.rs:19:5: 19:29
15+
- // + literal: Const { ty: extern "rust-intrinsic" fn(T) {std::intrinsics::forget::<T>}, val: Value(Scalar(<ZST>)) }
16+
+ _0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:19:5: 19:32
17+
+ goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:19:5: 19:32
2218
}
2319

2420
bb1: {
25-
StorageDead(_3); // scope 1 at $DIR/lower_intrinsics.rs:19:40: 19:41
26-
StorageDead(_2); // scope 0 at $DIR/lower_intrinsics.rs:19:43: 19:44
27-
_0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:18:24: 20:2
21+
StorageDead(_2); // scope 0 at $DIR/lower_intrinsics.rs:19:31: 19:32
2822
goto -> bb2; // scope 0 at $DIR/lower_intrinsics.rs:20:1: 20:2
2923
}
3024

src/test/mir-opt/lower_intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn size_of<T>() -> usize {
1616

1717
// EMIT_MIR lower_intrinsics.forget.LowerIntrinsics.diff
1818
pub fn forget<T>(t: T) {
19-
unsafe { core::intrinsics::forget(t) };
19+
core::intrinsics::forget(t)
2020
}
2121

2222
// EMIT_MIR lower_intrinsics.unreachable.LowerIntrinsics.diff

0 commit comments

Comments
 (0)