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 785e21d

Browse files
committedFeb 28, 2025
Remove allow(unused_variables) for rustc_transmute.
This was hiding some genuine sins, including unused arguments in numerous functions/methods (incl. trait methods), and some unnecessary computation.
1 parent dfb2222 commit 785e21d

File tree

10 files changed

+24
-53
lines changed

10 files changed

+24
-53
lines changed
 

‎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
@@ -1037,12 +1037,11 @@ where
10371037

10381038
pub(super) fn is_transmutable(
10391039
&mut self,
1040-
param_env: I::ParamEnv,
10411040
dst: I::Ty,
10421041
src: I::Ty,
10431042
assume: I::Const,
10441043
) -> Result<Certainty, NoSolution> {
1045-
self.delegate.is_transmutable(param_env, dst, src, assume)
1044+
self.delegate.is_transmutable(dst, src, assume)
10461045
}
10471046
}
10481047

‎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
@@ -2530,9 +2530,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
25302530
return GetSafeTransmuteErrorAndReason::Silent;
25312531
};
25322532

2533-
let Some(assume) =
2534-
rustc_transmute::Assume::from_const(self.infcx.tcx, obligation.param_env, assume)
2535-
else {
2533+
let Some(assume) = rustc_transmute::Assume::from_const(self.infcx.tcx, assume) else {
25362534
self.dcx().span_delayed_bug(
25372535
span,
25382536
"Unable to construct rustc_transmute::Assume where it was previously possible",
@@ -2544,11 +2542,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
25442542
let src = trait_pred.trait_ref.args.type_at(1);
25452543
let err_msg = format!("`{src}` cannot be safely transmuted into `{dst}`");
25462544

2547-
match rustc_transmute::TransmuteTypeEnv::new(self.infcx.tcx).is_transmutable(
2548-
obligation.cause,
2549-
src_and_dst,
2550-
assume,
2551-
) {
2545+
match rustc_transmute::TransmuteTypeEnv::new(self.infcx.tcx)
2546+
.is_transmutable(src_and_dst, assume)
2547+
{
25522548
Answer::No(reason) => {
25532549
let safe_transmute_explanation = match reason {
25542550
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.tcx).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

+3-8
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,7 @@ 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

@@ -425,11 +423,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
425423

426424
debug!(?src, ?dst);
427425
let mut transmute_env = rustc_transmute::TransmuteTypeEnv::new(self.infcx.tcx);
428-
let maybe_transmutable = transmute_env.is_transmutable(
429-
obligation.cause.clone(),
430-
rustc_transmute::Types { dst, src },
431-
assume,
432-
);
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/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

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// tidy-alphabetical-start
2-
#![allow(unused_variables)]
32
#![feature(never_type)]
43
#![warn(unreachable_pub)]
54
// tidy-alphabetical-end
@@ -80,8 +79,7 @@ pub enum Reason<T> {
8079
#[cfg(feature = "rustc")]
8180
mod rustc {
8281
use rustc_hir::lang_items::LangItem;
83-
use rustc_middle::traits::ObligationCause;
84-
use rustc_middle::ty::{Const, ParamEnv, Ty, TyCtxt};
82+
use rustc_middle::ty::{Const, Ty, TyCtxt};
8583

8684
use super::*;
8785

@@ -105,27 +103,19 @@ mod rustc {
105103

106104
pub fn is_transmutable(
107105
&mut self,
108-
cause: ObligationCause<'tcx>,
109106
types: Types<'tcx>,
110107
assume: crate::Assume,
111108
) -> crate::Answer<crate::layout::rustc::Ref<'tcx>> {
112109
crate::maybe_transmutable::MaybeTransmutableQuery::new(
113-
types.src,
114-
types.dst,
115-
assume,
116-
self.tcx,
110+
types.src, types.dst, assume, self.tcx,
117111
)
118112
.answer()
119113
}
120114
}
121115

122116
impl Assume {
123117
/// Constructs an `Assume` from a given const-`Assume`.
124-
pub fn from_const<'tcx>(
125-
tcx: TyCtxt<'tcx>,
126-
param_env: ParamEnv<'tcx>,
127-
ct: Const<'tcx>,
128-
) -> Option<Self> {
118+
pub fn from_const<'tcx>(tcx: TyCtxt<'tcx>, ct: Const<'tcx>) -> Option<Self> {
129119
use rustc_middle::ty::ScalarInt;
130120
use rustc_span::sym;
131121

‎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)
Failed to load comments.