Skip to content

Commit 5911b2a

Browse files
authored
Unrolled build for rust-lang#129032
Rollup merge of rust-lang#129032 - jswrenn:transmute-method, r=compiler-errors Document & implement the transmutation modeled by `BikeshedIntrinsicFrom` Documents that `BikeshedIntrinsicFrom` models transmute-via-union, which is slightly more expressive than the transmute-via-cast implemented by `transmute_copy`. Additionally, we provide an implementation of transmute-via-union as a method on the `BikeshedIntrinsicFrom` trait with additional documentation on the boundary between trait invariants and caller obligations. Whether or not transmute-via-union is the right kind of transmute to model remains up for discussion [1]. Regardless, it seems wise to document the present behavior. [1] https://rust-lang.zulipchat.com/#narrow/stream/216762-project-safe-transmute/topic/What.20'kind'.20of.20transmute.20to.20model.3F/near/426331967 Tracking Issue: rust-lang#99571 r? `@compiler-errors` cc `@scottmcm,` `@Lokathor`
2 parents 515395a + 2540070 commit 5911b2a

17 files changed

+372
-38
lines changed

compiler/rustc_ty_utils/src/instance.rs

+5
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ fn resolve_associated_item<'tcx>(
363363
tcx.item_name(trait_item_id)
364364
),
365365
}
366+
} else if tcx.is_lang_item(trait_ref.def_id, LangItem::TransmuteTrait) {
367+
let name = tcx.item_name(trait_item_id);
368+
assert_eq!(name, sym::transmute);
369+
let args = tcx.erase_regions(rcvr_args);
370+
Some(ty::Instance::new(trait_item_id, args))
366371
} else {
367372
Instance::try_resolve_item_for_coroutine(tcx, trait_item_id, trait_id, rcvr_args)
368373
}

library/core/src/mem/transmutability.rs

+350-21
Large diffs are not rendered by default.

tests/mir-opt/issue_72181_1.main.built.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() -> () {
1919
StorageLive(_2);
2020
StorageLive(_3);
2121
_3 = ();
22-
_2 = transmute::<(), Void>(move _3) -> bb4;
22+
_2 = std::intrinsics::transmute::<(), Void>(move _3) -> bb4;
2323
}
2424

2525
bb1: {

tests/mir-opt/lower_intrinsics.transmute_inhabited.LowerIntrinsics.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
bb0: {
1010
StorageLive(_2);
1111
_2 = copy _1;
12-
- _0 = transmute::<std::cmp::Ordering, i8>(move _2) -> [return: bb1, unwind unreachable];
12+
- _0 = std::intrinsics::transmute::<std::cmp::Ordering, i8>(move _2) -> [return: bb1, unwind unreachable];
1313
+ _0 = move _2 as i8 (Transmute);
1414
+ goto -> bb1;
1515
}

tests/mir-opt/lower_intrinsics.transmute_inhabited.LowerIntrinsics.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
bb0: {
1010
StorageLive(_2);
1111
_2 = copy _1;
12-
- _0 = transmute::<std::cmp::Ordering, i8>(move _2) -> [return: bb1, unwind unreachable];
12+
- _0 = std::intrinsics::transmute::<std::cmp::Ordering, i8>(move _2) -> [return: bb1, unwind unreachable];
1313
+ _0 = move _2 as i8 (Transmute);
1414
+ goto -> bb1;
1515
}

tests/mir-opt/lower_intrinsics.transmute_ref_dst.LowerIntrinsics.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
bb0: {
1010
StorageLive(_2);
1111
_2 = copy _1;
12-
- _0 = transmute::<&T, *const T>(move _2) -> [return: bb1, unwind unreachable];
12+
- _0 = std::intrinsics::transmute::<&T, *const T>(move _2) -> [return: bb1, unwind unreachable];
1313
+ _0 = move _2 as *const T (Transmute);
1414
+ goto -> bb1;
1515
}

tests/mir-opt/lower_intrinsics.transmute_ref_dst.LowerIntrinsics.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
bb0: {
1010
StorageLive(_2);
1111
_2 = copy _1;
12-
- _0 = transmute::<&T, *const T>(move _2) -> [return: bb1, unwind unreachable];
12+
- _0 = std::intrinsics::transmute::<&T, *const T>(move _2) -> [return: bb1, unwind unreachable];
1313
+ _0 = move _2 as *const T (Transmute);
1414
+ goto -> bb1;
1515
}

tests/mir-opt/lower_intrinsics.transmute_to_box_uninhabited.LowerIntrinsics.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
bb0: {
1313
StorageLive(_1);
14-
- _1 = transmute::<usize, Box<Never>>(const 1_usize) -> [return: bb1, unwind unreachable];
14+
- _1 = std::intrinsics::transmute::<usize, Box<Never>>(const 1_usize) -> [return: bb1, unwind unreachable];
1515
+ _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
1616
+ goto -> bb1;
1717
}

tests/mir-opt/lower_intrinsics.transmute_to_box_uninhabited.LowerIntrinsics.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
bb0: {
1313
StorageLive(_1);
14-
- _1 = transmute::<usize, Box<Never>>(const 1_usize) -> [return: bb1, unwind unreachable];
14+
- _1 = std::intrinsics::transmute::<usize, Box<Never>>(const 1_usize) -> [return: bb1, unwind unreachable];
1515
+ _1 = const 1_usize as std::boxed::Box<Never> (Transmute);
1616
+ goto -> bb1;
1717
}

tests/mir-opt/lower_intrinsics.transmute_to_mut_uninhabited.LowerIntrinsics.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
bb0: {
1212
StorageLive(_1);
13-
- _1 = transmute::<usize, &mut Never>(const 1_usize) -> [return: bb1, unwind unreachable];
13+
- _1 = std::intrinsics::transmute::<usize, &mut Never>(const 1_usize) -> [return: bb1, unwind unreachable];
1414
+ _1 = const 1_usize as &mut Never (Transmute);
1515
+ goto -> bb1;
1616
}

tests/mir-opt/lower_intrinsics.transmute_to_mut_uninhabited.LowerIntrinsics.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
bb0: {
1212
StorageLive(_1);
13-
- _1 = transmute::<usize, &mut Never>(const 1_usize) -> [return: bb1, unwind unreachable];
13+
- _1 = std::intrinsics::transmute::<usize, &mut Never>(const 1_usize) -> [return: bb1, unwind unreachable];
1414
+ _1 = const 1_usize as &mut Never (Transmute);
1515
+ goto -> bb1;
1616
}

tests/mir-opt/lower_intrinsics.transmute_to_ref_uninhabited.LowerIntrinsics.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
bb0: {
1212
StorageLive(_1);
13-
- _1 = transmute::<usize, &Never>(const 1_usize) -> [return: bb1, unwind unreachable];
13+
- _1 = std::intrinsics::transmute::<usize, &Never>(const 1_usize) -> [return: bb1, unwind unreachable];
1414
+ _1 = const 1_usize as &Never (Transmute);
1515
+ goto -> bb1;
1616
}

tests/mir-opt/lower_intrinsics.transmute_to_ref_uninhabited.LowerIntrinsics.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
bb0: {
1212
StorageLive(_1);
13-
- _1 = transmute::<usize, &Never>(const 1_usize) -> [return: bb1, unwind unreachable];
13+
- _1 = std::intrinsics::transmute::<usize, &Never>(const 1_usize) -> [return: bb1, unwind unreachable];
1414
+ _1 = const 1_usize as &Never (Transmute);
1515
+ goto -> bb1;
1616
}

tests/mir-opt/lower_intrinsics.transmute_uninhabited.LowerIntrinsics.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
bb0: {
1010
StorageLive(_2);
1111
_2 = copy _1;
12-
- _0 = transmute::<(), Never>(move _2) -> unwind unreachable;
12+
- _0 = std::intrinsics::transmute::<(), Never>(move _2) -> unwind unreachable;
1313
+ _0 = move _2 as Never (Transmute);
1414
+ unreachable;
1515
}

tests/mir-opt/lower_intrinsics.transmute_uninhabited.LowerIntrinsics.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
bb0: {
1010
StorageLive(_2);
1111
_2 = copy _1;
12-
- _0 = transmute::<(), Never>(move _2) -> unwind unreachable;
12+
- _0 = std::intrinsics::transmute::<(), Never>(move _2) -> unwind unreachable;
1313
+ _0 = move _2 as Never (Transmute);
1414
+ unreachable;
1515
}

tests/ui/closures/coerce-unsafe-to-closure.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0277]: expected a `FnOnce(&str)` closure, found `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
1+
error[E0277]: expected a `FnOnce(&str)` closure, found `unsafe extern "rust-intrinsic" fn(_) -> _ {std::intrinsics::transmute::<_, _>}`
22
--> $DIR/coerce-unsafe-to-closure.rs:2:44
33
|
44
LL | let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);
55
| --- ^^^^^^^^^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
66
| |
77
| required by a bound introduced by this call
88
|
9-
= help: the trait `FnOnce(&str)` is not implemented for fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
9+
= help: the trait `FnOnce(&str)` is not implemented for fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {std::intrinsics::transmute::<_, _>}`
1010
= note: unsafe function cannot be called generically without an unsafe block
1111
note: required by a bound in `Option::<T>::map`
1212
--> $SRC_DIR/core/src/option.rs:LL:COL

tests/ui/intrinsics/reify-intrinsic.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LL | let _: unsafe extern "rust-intrinsic" fn(isize) -> usize = std::mem::tr
77
| expected due to this
88
|
99
= note: expected fn pointer `unsafe extern "rust-intrinsic" fn(isize) -> usize`
10-
found fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
10+
found fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {std::intrinsics::transmute::<_, _>}`
1111

12-
error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}` as `unsafe extern "rust-intrinsic" fn(isize) -> usize` is invalid
12+
error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {std::intrinsics::transmute::<_, _>}` as `unsafe extern "rust-intrinsic" fn(isize) -> usize` is invalid
1313
--> $DIR/reify-intrinsic.rs:11:13
1414
|
1515
LL | let _ = std::mem::transmute as unsafe extern "rust-intrinsic" fn(isize) -> usize;

0 commit comments

Comments
 (0)