Skip to content

Commit 3973775

Browse files
committed
util::compare_types, no more ParamEnv::reveal
1 parent 563c473 commit 3973775

File tree

8 files changed

+52
-36
lines changed

8 files changed

+52
-36
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_hir::def_id::DefId;
44
use rustc_infer::infer::TyCtxtInferExt;
55
use rustc_infer::infer::at::ToTrace;
66
use rustc_infer::traits::ObligationCause;
7+
use rustc_middle::mir::MirPhase;
78
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
89
use rustc_middle::query::TyCtxtAt;
910
use rustc_middle::ty::layout::{
@@ -116,6 +117,7 @@ impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
116117
/// This test should be symmetric, as it is primarily about layout compatibility.
117118
pub(super) fn mir_assign_valid_types<'tcx>(
118119
tcx: TyCtxt<'tcx>,
120+
mir_phase: MirPhase,
119121
param_env: ParamEnv<'tcx>,
120122
src: TyAndLayout<'tcx>,
121123
dest: TyAndLayout<'tcx>,
@@ -124,7 +126,7 @@ pub(super) fn mir_assign_valid_types<'tcx>(
124126
// all normal lifetimes are erased, higher-ranked types with their
125127
// late-bound lifetimes are still around and can lead to type
126128
// differences.
127-
if util::relate_types(tcx, param_env, Variance::Covariant, src.ty, dest.ty) {
129+
if util::relate_types(tcx, mir_phase, param_env, Variance::Covariant, src.ty, dest.ty) {
128130
// Make sure the layout is equal, too -- just to be safe. Miri really
129131
// needs layout equality. For performance reason we skip this check when
130132
// the types are equal. Equal types *can* have different layouts when
@@ -144,6 +146,7 @@ pub(super) fn mir_assign_valid_types<'tcx>(
144146
#[cfg_attr(not(debug_assertions), inline(always))]
145147
pub(super) fn from_known_layout<'tcx>(
146148
tcx: TyCtxtAt<'tcx>,
149+
mir_phase: MirPhase,
147150
param_env: ParamEnv<'tcx>,
148151
known_layout: Option<TyAndLayout<'tcx>>,
149152
compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
@@ -153,7 +156,13 @@ pub(super) fn from_known_layout<'tcx>(
153156
Some(known_layout) => {
154157
if cfg!(debug_assertions) {
155158
let check_layout = compute()?;
156-
if !mir_assign_valid_types(tcx.tcx, param_env, check_layout, known_layout) {
159+
if !mir_assign_valid_types(
160+
tcx.tcx,
161+
mir_phase,
162+
param_env,
163+
check_layout,
164+
known_layout,
165+
) {
157166
span_bug!(
158167
tcx.span,
159168
"expected type differs from actual type.\nexpected: {}\nactual: {}",

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
773773
)?;
774774
if !mir_assign_valid_types(
775775
*self.tcx,
776+
self.body().phase,
776777
self.param_env,
777778
self.layout_of(normalized_place_ty)?,
778779
op.layout,
@@ -832,7 +833,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
832833
})
833834
};
834835
let layout =
835-
from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty).into())?;
836+
from_known_layout(self.tcx, self.body().phase, self.param_env, layout, || {
837+
self.layout_of(ty).into()
838+
})?;
836839
let imm = match val_val {
837840
mir::ConstValue::Indirect { alloc_id, offset } => {
838841
// This is const data, no mutation allowed.

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ where
540540
)?;
541541
if !mir_assign_valid_types(
542542
*self.tcx,
543+
self.body().phase,
543544
self.param_env,
544545
self.layout_of(normalized_place_ty)?,
545546
place.layout,
@@ -870,8 +871,13 @@ where
870871
) -> InterpResult<'tcx> {
871872
// We do NOT compare the types for equality, because well-typed code can
872873
// actually "transmute" `&mut T` to `&T` in an assignment without a cast.
873-
let layout_compat =
874-
mir_assign_valid_types(*self.tcx, self.param_env, src.layout(), dest.layout());
874+
let layout_compat = mir_assign_valid_types(
875+
*self.tcx,
876+
self.body().phase,
877+
self.param_env,
878+
src.layout(),
879+
dest.layout(),
880+
);
875881
if !allow_transmute && !layout_compat {
876882
span_bug!(
877883
self.cur_span(),

compiler/rustc_const_eval/src/interpret/stack.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
596596
return interp_ok(layout);
597597
}
598598

599-
let layout = from_known_layout(self.tcx, self.param_env, layout, || {
600-
let local_ty = frame.body.local_decls[local].ty;
601-
let local_ty =
602-
self.instantiate_from_frame_and_normalize_erasing_regions(frame, local_ty)?;
603-
self.layout_of(local_ty).into()
604-
})?;
599+
let layout =
600+
from_known_layout(self.tcx, self.body().phase, self.param_env, layout, || {
601+
let local_ty = frame.body.local_decls[local].ty;
602+
let local_ty =
603+
self.instantiate_from_frame_and_normalize_erasing_regions(frame, local_ty)?;
604+
self.layout_of(local_ty).into()
605+
})?;
605606

606607
// Layouts of locals are requested a lot, so we cache them.
607608
state.layout.set(Some(layout));

compiler/rustc_const_eval/src/util/compare_types.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,20 @@
44
//! other areas of the compiler as well.
55
66
use rustc_infer::infer::TyCtxtInferExt;
7+
use rustc_middle::mir::MirPhase;
78
use rustc_middle::traits::ObligationCause;
8-
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt, TypingMode, Variance};
9+
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt, Variance};
910
use rustc_trait_selection::traits::ObligationCtxt;
1011

11-
/// Returns whether the two types are equal up to subtyping.
12-
///
13-
/// This is used in case we don't know the expected subtyping direction
14-
/// and still want to check whether anything is broken.
15-
pub fn is_equal_up_to_subtyping<'tcx>(
12+
/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
13+
pub fn sub_types<'tcx>(
1614
tcx: TyCtxt<'tcx>,
15+
mir_phase: MirPhase,
1716
param_env: ParamEnv<'tcx>,
1817
src: Ty<'tcx>,
1918
dest: Ty<'tcx>,
2019
) -> bool {
21-
// Fast path.
22-
if src == dest {
23-
return true;
24-
}
25-
26-
// Check for subtyping in either direction.
27-
relate_types(tcx, param_env, Variance::Covariant, src, dest)
28-
|| relate_types(tcx, param_env, Variance::Covariant, dest, src)
20+
relate_types(tcx, mir_phase, param_env, Variance::Covariant, src, dest)
2921
}
3022

3123
/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
@@ -35,6 +27,7 @@ pub fn is_equal_up_to_subtyping<'tcx>(
3527
/// because we want to check for type equality.
3628
pub fn relate_types<'tcx>(
3729
tcx: TyCtxt<'tcx>,
30+
mir_phase: MirPhase,
3831
param_env: ParamEnv<'tcx>,
3932
variance: Variance,
4033
src: Ty<'tcx>,
@@ -45,8 +38,7 @@ pub fn relate_types<'tcx>(
4538
}
4639

4740
let mut builder = tcx.infer_ctxt().ignoring_regions();
48-
// FIXME(#132279): This should eventually use the already defined hidden types.
49-
let infcx = builder.build(TypingMode::from_param_env(param_env));
41+
let infcx = builder.build(mir_phase.typing_mode());
5042
let ocx = ObligationCtxt::new(&infcx);
5143
let cause = ObligationCause::dummy();
5244
let src = ocx.normalize(&cause, param_env, src);

compiler/rustc_const_eval/src/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod type_name;
88

99
pub use self::alignment::{is_disaligned, is_within_packed};
1010
pub use self::check_validity_requirement::check_validity_requirement;
11-
pub use self::compare_types::{is_equal_up_to_subtyping, relate_types};
11+
pub use self::compare_types::{relate_types, sub_types};
1212
pub use self::type_name::type_name;
1313

1414
/// Classify whether an operator is "left-homogeneous", i.e., the LHS has the

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,13 @@ impl<'tcx> Inliner<'tcx> {
244244
// Normally, this shouldn't be required, but trait normalization failure can create a
245245
// validation ICE.
246246
let output_type = callee_body.return_ty();
247-
if !util::relate_types(self.tcx, self.param_env, ty::Covariant, output_type, destination_ty)
248-
{
247+
if !util::sub_types(
248+
self.tcx,
249+
caller_body.phase,
250+
self.param_env,
251+
output_type,
252+
destination_ty,
253+
) {
249254
trace!(?output_type, ?destination_ty);
250255
return Err("failed to normalize return type");
251256
}
@@ -275,7 +280,7 @@ impl<'tcx> Inliner<'tcx> {
275280
self_arg_ty.into_iter().chain(arg_tuple_tys).zip(callee_body.args_iter())
276281
{
277282
let input_type = callee_body.local_decls[input].ty;
278-
if !util::relate_types(self.tcx, self.param_env, ty::Covariant, input_type, arg_ty)
283+
if !util::sub_types(self.tcx, caller_body.phase, self.param_env, input_type, arg_ty)
279284
{
280285
trace!(?arg_ty, ?input_type);
281286
return Err("failed to normalize tuple argument type");
@@ -285,7 +290,7 @@ impl<'tcx> Inliner<'tcx> {
285290
for (arg, input) in args.iter().zip(callee_body.args_iter()) {
286291
let input_type = callee_body.local_decls[input].ty;
287292
let arg_ty = arg.node.ty(&caller_body.local_decls, self.tcx);
288-
if !util::relate_types(self.tcx, self.param_env, ty::Covariant, input_type, arg_ty)
293+
if !util::sub_types(self.tcx, caller_body.phase, self.param_env, input_type, arg_ty)
289294
{
290295
trace!(?arg_ty, ?input_type);
291296
return Err("failed to normalize argument type");

compiler/rustc_mir_transform/src/validate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_target::spec::abi::Abi;
2020
use rustc_trait_selection::traits::ObligationCtxt;
2121
use rustc_type_ir::Upcast;
2222

23-
use crate::util::{is_within_packed, relate_types};
23+
use crate::util::{self, is_within_packed};
2424

2525
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2626
enum EdgeKind {
@@ -583,7 +583,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
583583
Variance::Covariant
584584
};
585585

586-
crate::util::relate_types(self.tcx, self.param_env, variance, src, dest)
586+
crate::util::relate_types(self.tcx, self.body.phase, self.param_env, variance, src, dest)
587587
}
588588

589589
/// Check that the given predicate definitely holds in the param-env of this MIR body.
@@ -794,10 +794,10 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
794794
}
795795
}
796796
ProjectionElem::Subtype(ty) => {
797-
if !relate_types(
797+
if !util::sub_types(
798798
self.tcx,
799+
self.body.phase,
799800
self.param_env,
800-
Variance::Covariant,
801801
ty,
802802
place_ref.ty(&self.body.local_decls, self.tcx).ty,
803803
) {

0 commit comments

Comments
 (0)