Skip to content

Commit e8be230

Browse files
authored
Rollup merge of #142124 - oli-obk:transmute-cast, r=scottmcm
Allow transmute casts in pre-runtime-MIR r? ``@scottmcm`` cc ``@BoxyUwU`` turns out in #138393 I erroneously used transmute casts in https://github.com/rust-lang/rust/blob/fd3da4bebdff63b7529483ff7025986ef16bf463/compiler/rustc_mir_build/src/builder/matches/test.rs#L209 I don't think they have any issues using them before runtime, we just checked for them because we didn't have code exercising those code paths
2 parents 590f630 + c677dc2 commit e8be230

File tree

3 files changed

+20
-31
lines changed

3 files changed

+20
-31
lines changed

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,8 +1531,6 @@ pub enum CastKind {
15311531
///
15321532
/// MIR is well-formed if the input and output types have different sizes,
15331533
/// but running a transmute between differently-sized types is UB.
1534-
///
1535-
/// Allowed only in [`MirPhase::Runtime`]; Earlier it's a [`TerminatorKind::Call`].
15361534
Transmute,
15371535
}
15381536

compiler/rustc_mir_transform/src/validate.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,37 +1308,27 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
13081308
}
13091309
}
13101310
CastKind::Transmute => {
1311-
if let MirPhase::Runtime(..) = self.body.phase {
1312-
// Unlike `mem::transmute`, a MIR `Transmute` is well-formed
1313-
// for any two `Sized` types, just potentially UB to run.
1314-
1315-
if !self
1316-
.tcx
1317-
.normalize_erasing_regions(self.typing_env, op_ty)
1318-
.is_sized(self.tcx, self.typing_env)
1319-
{
1320-
self.fail(
1321-
location,
1322-
format!("Cannot transmute from non-`Sized` type {op_ty}"),
1323-
);
1324-
}
1325-
if !self
1326-
.tcx
1327-
.normalize_erasing_regions(self.typing_env, *target_type)
1328-
.is_sized(self.tcx, self.typing_env)
1329-
{
1330-
self.fail(
1331-
location,
1332-
format!("Cannot transmute to non-`Sized` type {target_type:?}"),
1333-
);
1334-
}
1335-
} else {
1311+
// Unlike `mem::transmute`, a MIR `Transmute` is well-formed
1312+
// for any two `Sized` types, just potentially UB to run.
1313+
1314+
if !self
1315+
.tcx
1316+
.normalize_erasing_regions(self.typing_env, op_ty)
1317+
.is_sized(self.tcx, self.typing_env)
1318+
{
13361319
self.fail(
13371320
location,
1338-
format!(
1339-
"Transmute is not supported in non-runtime phase {:?}.",
1340-
self.body.phase
1341-
),
1321+
format!("Cannot transmute from non-`Sized` type {op_ty}"),
1322+
);
1323+
}
1324+
if !self
1325+
.tcx
1326+
.normalize_erasing_regions(self.typing_env, *target_type)
1327+
.is_sized(self.tcx, self.typing_env)
1328+
{
1329+
self.fail(
1330+
location,
1331+
format!("Cannot transmute to non-`Sized` type {target_type:?}"),
13421332
);
13431333
}
13441334
}

tests/ui/type/pattern_types/matching.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(pattern_types, pattern_type_macro, structural_match)]
22

33
//@ check-pass
4+
//@ compile-flags: -Zvalidate-mir
45

56
use std::marker::StructuralPartialEq;
67
use std::pat::pattern_type;

0 commit comments

Comments
 (0)