Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b6c1b63

Browse files
authoredMar 1, 2025
Rollup merge of #137776 - nnethercote:rustc_transmute-cleanups, r=jswrenn
Some `rustc_transmute` cleanups A number of small things that can be removed. r? ``@jswrenn``
2 parents b7853ef + b0530c9 commit b6c1b63

File tree

14 files changed

+32
-85
lines changed

14 files changed

+32
-85
lines changed
 

‎Cargo.lock

-2
Original file line numberDiff line numberDiff line change
@@ -4501,8 +4501,6 @@ dependencies = [
45014501
"rustc_abi",
45024502
"rustc_data_structures",
45034503
"rustc_hir",
4504-
"rustc_infer",
4505-
"rustc_macros",
45064504
"rustc_middle",
45074505
"rustc_span",
45084506
"tracing",

‎compiler/rustc_next_trait_solver/src/delegate.rs

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
102102

103103
fn is_transmutable(
104104
&self,
105-
param_env: <Self::Interner as Interner>::ParamEnv,
106105
dst: <Self::Interner as Interner>::Ty,
107106
src: <Self::Interner as Interner>::Ty,
108107
assume: <Self::Interner as Interner>::Const,

‎compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1091,12 +1091,11 @@ where
10911091

10921092
pub(super) fn is_transmutable(
10931093
&mut self,
1094-
param_env: I::ParamEnv,
10951094
dst: I::Ty,
10961095
src: I::Ty,
10971096
assume: I::Const,
10981097
) -> Result<Certainty, NoSolution> {
1099-
self.delegate.is_transmutable(param_env, dst, src, assume)
1098+
self.delegate.is_transmutable(dst, src, assume)
11001099
}
11011100
}
11021101

‎compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

-1
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,6 @@ where
617617
)?;
618618

619619
let certainty = ecx.is_transmutable(
620-
goal.param_env,
621620
goal.predicate.trait_ref.args.type_at(0),
622621
goal.predicate.trait_ref.args.type_at(1),
623622
assume,

‎compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2533,9 +2533,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
25332533
return GetSafeTransmuteErrorAndReason::Silent;
25342534
};
25352535

2536-
let Some(assume) =
2537-
rustc_transmute::Assume::from_const(self.infcx.tcx, obligation.param_env, assume)
2538-
else {
2536+
let Some(assume) = rustc_transmute::Assume::from_const(self.infcx.tcx, assume) else {
25392537
self.dcx().span_delayed_bug(
25402538
span,
25412539
"Unable to construct rustc_transmute::Assume where it was previously possible",
@@ -2547,11 +2545,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
25472545
let src = trait_pred.trait_ref.args.type_at(1);
25482546
let err_msg = format!("`{src}` cannot be safely transmuted into `{dst}`");
25492547

2550-
match rustc_transmute::TransmuteTypeEnv::new(self.infcx).is_transmutable(
2551-
obligation.cause,
2552-
src_and_dst,
2553-
assume,
2554-
) {
2548+
match rustc_transmute::TransmuteTypeEnv::new(self.infcx.tcx)
2549+
.is_transmutable(src_and_dst, assume)
2550+
{
25552551
Answer::No(reason) => {
25562552
let safe_transmute_explanation = match reason {
25572553
rustc_transmute::Reason::SrcIsNotYetSupported => {

‎compiler/rustc_trait_selection/src/solve/delegate.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_infer::infer::canonical::{
77
Canonical, CanonicalExt as _, CanonicalQueryInput, CanonicalVarInfo, CanonicalVarValues,
88
};
99
use rustc_infer::infer::{InferCtxt, RegionVariableOrigin, TyCtxtInferExt};
10-
use rustc_infer::traits::ObligationCause;
1110
use rustc_infer::traits::solve::Goal;
1211
use rustc_middle::ty::fold::TypeFoldable;
1312
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt as _};
@@ -222,7 +221,6 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
222221
// register candidates. We probably need to register >1 since we may have an OR of ANDs.
223222
fn is_transmutable(
224223
&self,
225-
param_env: ty::ParamEnv<'tcx>,
226224
dst: Ty<'tcx>,
227225
src: Ty<'tcx>,
228226
assume: ty::Const<'tcx>,
@@ -231,16 +229,14 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
231229
// which will ICE for region vars.
232230
let (dst, src) = self.tcx.erase_regions((dst, src));
233231

234-
let Some(assume) = rustc_transmute::Assume::from_const(self.tcx, param_env, assume) else {
232+
let Some(assume) = rustc_transmute::Assume::from_const(self.tcx, assume) else {
235233
return Err(NoSolution);
236234
};
237235

238236
// FIXME(transmutability): This really should be returning nested goals for `Answer::If*`
239-
match rustc_transmute::TransmuteTypeEnv::new(&self.0).is_transmutable(
240-
ObligationCause::dummy(),
241-
rustc_transmute::Types { src, dst },
242-
assume,
243-
) {
237+
match rustc_transmute::TransmuteTypeEnv::new(self.0.tcx)
238+
.is_transmutable(rustc_transmute::Types { src, dst }, assume)
239+
{
244240
rustc_transmute::Answer::Yes => Ok(Certainty::Yes),
245241
rustc_transmute::Answer::No(_) | rustc_transmute::Answer::If(_) => Err(NoSolution),
246242
}

‎compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -414,22 +414,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
414414
if self.tcx().features().generic_const_exprs() {
415415
assume = crate::traits::evaluate_const(self.infcx, assume, obligation.param_env)
416416
}
417-
let Some(assume) =
418-
rustc_transmute::Assume::from_const(self.infcx.tcx, obligation.param_env, assume)
419-
else {
417+
let Some(assume) = rustc_transmute::Assume::from_const(self.infcx.tcx, assume) else {
420418
return Err(Unimplemented);
421419
};
422420

423421
let dst = predicate.trait_ref.args.type_at(0);
424422
let src = predicate.trait_ref.args.type_at(1);
425423

426424
debug!(?src, ?dst);
427-
let mut transmute_env = rustc_transmute::TransmuteTypeEnv::new(self.infcx);
428-
let maybe_transmutable = transmute_env.is_transmutable(
429-
obligation.cause.clone(),
430-
rustc_transmute::Types { dst, src },
431-
assume,
432-
);
425+
let mut transmute_env = rustc_transmute::TransmuteTypeEnv::new(self.infcx.tcx);
426+
let maybe_transmutable =
427+
transmute_env.is_transmutable(rustc_transmute::Types { dst, src }, assume);
433428

434429
let fully_flattened = match maybe_transmutable {
435430
Answer::No(_) => Err(Unimplemented)?,

‎compiler/rustc_transmute/Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ edition = "2024"
88
rustc_abi = { path = "../rustc_abi", optional = true }
99
rustc_data_structures = { path = "../rustc_data_structures" }
1010
rustc_hir = { path = "../rustc_hir", optional = true }
11-
rustc_infer = { path = "../rustc_infer", optional = true }
12-
rustc_macros = { path = "../rustc_macros", optional = true }
1311
rustc_middle = { path = "../rustc_middle", optional = true }
1412
rustc_span = { path = "../rustc_span", optional = true }
1513
tracing = "0.1"
@@ -19,8 +17,6 @@ tracing = "0.1"
1917
rustc = [
2018
"dep:rustc_abi",
2119
"dep:rustc_hir",
22-
"dep:rustc_infer",
23-
"dep:rustc_macros",
2420
"dep:rustc_middle",
2521
"dep:rustc_span",
2622
]

‎compiler/rustc_transmute/src/layout/dfa.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<R> Transitions<R>
3838
where
3939
R: Ref,
4040
{
41-
#[allow(dead_code)]
41+
#[cfg(test)]
4242
fn insert(&mut self, transition: Transition<R>, state: State) {
4343
match transition {
4444
Transition::Byte(b) => {
@@ -86,15 +86,6 @@ impl<R> Dfa<R>
8686
where
8787
R: Ref,
8888
{
89-
#[allow(dead_code)]
90-
pub(crate) fn unit() -> Self {
91-
let transitions: Map<State, Transitions<R>> = Map::default();
92-
let start = State::new();
93-
let accepting = start;
94-
95-
Self { transitions, start, accepting }
96-
}
97-
9889
#[cfg(test)]
9990
pub(crate) fn bool() -> Self {
10091
let mut transitions: Map<State, Transitions<R>> = Map::default();

‎compiler/rustc_transmute/src/layout/nfa.rs

-5
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,6 @@ where
159159
}
160160
Self { transitions, start, accepting }
161161
}
162-
163-
#[allow(dead_code)]
164-
pub(crate) fn edges_from(&self, start: State) -> Option<&Map<Transition<R>, Set<State>>> {
165-
self.transitions.get(&start)
166-
}
167162
}
168163

169164
impl State {

‎compiler/rustc_transmute/src/layout/tree.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ pub(crate) mod rustc {
237237

238238
ty::Tuple(members) => Self::from_tuple((ty, layout), members, cx),
239239

240-
ty::Array(inner_ty, len) => {
240+
ty::Array(inner_ty, _len) => {
241241
let FieldsShape::Array { stride, count } = &layout.fields else {
242242
return Err(Err::NotYetSupported);
243243
};
@@ -282,7 +282,6 @@ pub(crate) mod rustc {
282282
FieldsShape::Primitive => {
283283
assert_eq!(members.len(), 1);
284284
let inner_ty = members[0];
285-
let inner_layout = layout_of(cx, inner_ty)?;
286285
Self::from_ty(inner_ty, cx)
287286
}
288287
FieldsShape::Arbitrary { offsets, .. } => {
@@ -345,7 +344,7 @@ pub(crate) mod rustc {
345344
// the enum delegates its layout to the variant at `index`.
346345
layout_of_variant(*index, None)
347346
}
348-
Variants::Multiple { tag, tag_encoding, tag_field, .. } => {
347+
Variants::Multiple { tag: _, tag_encoding, tag_field, .. } => {
349348
// `Variants::Multiple` denotes an enum with multiple
350349
// variants. The layout of such an enum is the disjunction
351350
// of the layouts of its tagged variants.
@@ -356,7 +355,7 @@ pub(crate) mod rustc {
356355

357356
let variants = def.discriminants(cx.tcx()).try_fold(
358357
Self::uninhabited(),
359-
|variants, (idx, ref discriminant)| {
358+
|variants, (idx, _discriminant)| {
360359
let variant = layout_of_variant(idx, Some(tag_encoding.clone()))?;
361360
Result::<Self, Err>::Ok(variants.or(variant))
362361
},
@@ -414,7 +413,7 @@ pub(crate) mod rustc {
414413

415414
// Append the fields, in memory order, to the layout.
416415
let inverse_memory_index = memory_index.invert_bijective_mapping();
417-
for (memory_idx, &field_idx) in inverse_memory_index.iter_enumerated() {
416+
for &field_idx in inverse_memory_index.iter() {
418417
// Add interfield padding.
419418
let padding_needed = offsets[field_idx] - size;
420419
let padding = Self::padding(padding_needed.bytes_usize());
@@ -468,15 +467,14 @@ pub(crate) mod rustc {
468467

469468
// This constructor does not support non-`FieldsShape::Union`
470469
// layouts. Fields of this shape are all placed at offset 0.
471-
let FieldsShape::Union(fields) = layout.fields() else {
470+
let FieldsShape::Union(_fields) = layout.fields() else {
472471
return Err(Err::NotYetSupported);
473472
};
474473

475474
let fields = &def.non_enum_variant().fields;
476475
let fields = fields.iter_enumerated().try_fold(
477476
Self::uninhabited(),
478-
|fields, (idx, field_def)| {
479-
let field_def = Def::Field(field_def);
477+
|fields, (idx, _field_def)| {
480478
let field_ty = ty_field(cx, (ty, layout), idx);
481479
let field_layout = layout_of(cx, field_ty)?;
482480
let field = Self::from_ty(field_ty, cx)?;

‎compiler/rustc_transmute/src/lib.rs

+9-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
// tidy-alphabetical-start
2-
#![allow(unused_variables)]
3-
#![feature(alloc_layout_extra)]
42
#![feature(never_type)]
53
#![warn(unreachable_pub)]
64
// tidy-alphabetical-end
@@ -81,55 +79,43 @@ pub enum Reason<T> {
8179
#[cfg(feature = "rustc")]
8280
mod rustc {
8381
use rustc_hir::lang_items::LangItem;
84-
use rustc_infer::infer::InferCtxt;
85-
use rustc_macros::TypeVisitable;
86-
use rustc_middle::traits::ObligationCause;
87-
use rustc_middle::ty::{Const, ParamEnv, Ty, TyCtxt};
82+
use rustc_middle::ty::{Const, Ty, TyCtxt};
8883

8984
use super::*;
9085

9186
/// The source and destination types of a transmutation.
92-
#[derive(TypeVisitable, Debug, Clone, Copy)]
87+
#[derive(Debug, Clone, Copy)]
9388
pub struct Types<'tcx> {
9489
/// The source type.
9590
pub src: Ty<'tcx>,
9691
/// The destination type.
9792
pub dst: Ty<'tcx>,
9893
}
9994

100-
pub struct TransmuteTypeEnv<'cx, 'tcx> {
101-
infcx: &'cx InferCtxt<'tcx>,
95+
pub struct TransmuteTypeEnv<'tcx> {
96+
tcx: TyCtxt<'tcx>,
10297
}
10398

104-
impl<'cx, 'tcx> TransmuteTypeEnv<'cx, 'tcx> {
105-
pub fn new(infcx: &'cx InferCtxt<'tcx>) -> Self {
106-
Self { infcx }
99+
impl<'tcx> TransmuteTypeEnv<'tcx> {
100+
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
101+
Self { tcx }
107102
}
108103

109-
#[allow(unused)]
110104
pub fn is_transmutable(
111105
&mut self,
112-
cause: ObligationCause<'tcx>,
113106
types: Types<'tcx>,
114107
assume: crate::Assume,
115108
) -> crate::Answer<crate::layout::rustc::Ref<'tcx>> {
116109
crate::maybe_transmutable::MaybeTransmutableQuery::new(
117-
types.src,
118-
types.dst,
119-
assume,
120-
self.infcx.tcx,
110+
types.src, types.dst, assume, self.tcx,
121111
)
122112
.answer()
123113
}
124114
}
125115

126116
impl Assume {
127117
/// Constructs an `Assume` from a given const-`Assume`.
128-
pub fn from_const<'tcx>(
129-
tcx: TyCtxt<'tcx>,
130-
param_env: ParamEnv<'tcx>,
131-
ct: Const<'tcx>,
132-
) -> Option<Self> {
118+
pub fn from_const<'tcx>(tcx: TyCtxt<'tcx>, ct: Const<'tcx>) -> Option<Self> {
133119
use rustc_middle::ty::ScalarInt;
134120
use rustc_span::sym;
135121

‎compiler/rustc_transmute/src/maybe_transmutable/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ where
7979
pub(crate) fn answer(self) -> Answer<<C as QueryContext>::Ref> {
8080
let Self { src, dst, assume, context } = self;
8181

82-
// Unconditionally all `Def` nodes from `src`, without pruning away the
82+
// Unconditionally remove all `Def` nodes from `src`, without pruning away the
8383
// branches they appear in. This is valid to do for value-to-value
8484
// transmutations, but not for `&mut T` to `&mut U`; we will need to be
8585
// more sophisticated to handle transmutations between mutable
8686
// references.
87-
let src = src.prune(&|def| false);
87+
let src = src.prune(&|_def| false);
8888

8989
if src.is_inhabited() && !dst.is_inhabited() {
9090
return Answer::No(Reason::DstUninhabited);
@@ -96,7 +96,7 @@ where
9696
let dst = if assume.safety {
9797
// ...if safety is assumed, don't check if they carry safety
9898
// invariants; retain all paths.
99-
dst.prune(&|def| false)
99+
dst.prune(&|_def| false)
100100
} else {
101101
// ...otherwise, prune away all paths with safety invariants from
102102
// the `Dst` layout.

‎compiler/rustc_transmute/src/maybe_transmutable/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ mod bool {
9292

9393
#[test]
9494
fn should_permit_validity_expansion_and_reject_contraction() {
95-
let un = layout::Tree::<Def, !>::uninhabited();
9695
let b0 = layout::Tree::<Def, !>::from_bits(0);
9796
let b1 = layout::Tree::<Def, !>::from_bits(1);
9897
let b2 = layout::Tree::<Def, !>::from_bits(2);

0 commit comments

Comments
 (0)
Please sign in to comment.