Skip to content

Commit c5dab55

Browse files
committed
move TypingMode to mir::Body
in the future it will need the `DefId` during analysis
1 parent 3973775 commit c5dab55

File tree

13 files changed

+76
-49
lines changed

13 files changed

+76
-49
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
589589
// Typeck only does a "non-const" check since it operates on HIR and cannot distinguish
590590
// which path expressions are getting called on and which path expressions are only used
591591
// as function pointers. This is required for correctness.
592-
let infcx = tcx.infer_ctxt().build(body.phase.typing_mode());
592+
let infcx = tcx.infer_ctxt().build(body.typing_mode(tcx));
593593
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
594594

595595
let predicates = tcx.predicates_of(callee).instantiate(tcx, fn_args);

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
116116
let obligation =
117117
Obligation::new(tcx, ObligationCause::dummy(), param_env, trait_ref);
118118

119-
let infcx = tcx.infer_ctxt().build(body.phase.typing_mode());
119+
let infcx = tcx.infer_ctxt().build(body.typing_mode(tcx));
120120
let mut selcx = SelectionContext::new(&infcx);
121121
let implsrc = selcx.select(&obligation);
122122

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ 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;
87
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
98
use rustc_middle::query::TyCtxtAt;
109
use rustc_middle::ty::layout::{
@@ -117,7 +116,7 @@ impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
117116
/// This test should be symmetric, as it is primarily about layout compatibility.
118117
pub(super) fn mir_assign_valid_types<'tcx>(
119118
tcx: TyCtxt<'tcx>,
120-
mir_phase: MirPhase,
119+
typing_mode: TypingMode<'tcx>,
121120
param_env: ParamEnv<'tcx>,
122121
src: TyAndLayout<'tcx>,
123122
dest: TyAndLayout<'tcx>,
@@ -126,7 +125,7 @@ pub(super) fn mir_assign_valid_types<'tcx>(
126125
// all normal lifetimes are erased, higher-ranked types with their
127126
// late-bound lifetimes are still around and can lead to type
128127
// differences.
129-
if util::relate_types(tcx, mir_phase, param_env, Variance::Covariant, src.ty, dest.ty) {
128+
if util::relate_types(tcx, typing_mode, param_env, Variance::Covariant, src.ty, dest.ty) {
130129
// Make sure the layout is equal, too -- just to be safe. Miri really
131130
// needs layout equality. For performance reason we skip this check when
132131
// the types are equal. Equal types *can* have different layouts when
@@ -146,7 +145,7 @@ pub(super) fn mir_assign_valid_types<'tcx>(
146145
#[cfg_attr(not(debug_assertions), inline(always))]
147146
pub(super) fn from_known_layout<'tcx>(
148147
tcx: TyCtxtAt<'tcx>,
149-
mir_phase: MirPhase,
148+
typing_mode: TypingMode<'tcx>,
150149
param_env: ParamEnv<'tcx>,
151150
known_layout: Option<TyAndLayout<'tcx>>,
152151
compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
@@ -158,7 +157,7 @@ pub(super) fn from_known_layout<'tcx>(
158157
let check_layout = compute()?;
159158
if !mir_assign_valid_types(
160159
tcx.tcx,
161-
mir_phase,
160+
typing_mode,
162161
param_env,
163162
check_layout,
164163
known_layout,

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
773773
)?;
774774
if !mir_assign_valid_types(
775775
*self.tcx,
776-
self.body().phase,
776+
self.body().typing_mode(*self.tcx),
777777
self.param_env,
778778
self.layout_of(normalized_place_ty)?,
779779
op.layout,
@@ -832,10 +832,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
832832
Scalar::Int(int) => Scalar::Int(int),
833833
})
834834
};
835-
let layout =
836-
from_known_layout(self.tcx, self.body().phase, self.param_env, layout, || {
837-
self.layout_of(ty).into()
838-
})?;
835+
let layout = from_known_layout(
836+
self.tcx,
837+
self.body().typing_mode(*self.tcx),
838+
self.param_env,
839+
layout,
840+
|| self.layout_of(ty).into(),
841+
)?;
839842
let imm = match val_val {
840843
mir::ConstValue::Indirect { alloc_id, offset } => {
841844
// This is const data, no mutation allowed.

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ where
540540
)?;
541541
if !mir_assign_valid_types(
542542
*self.tcx,
543-
self.body().phase,
543+
self.body().typing_mode(*self.tcx),
544544
self.param_env,
545545
self.layout_of(normalized_place_ty)?,
546546
place.layout,
@@ -873,7 +873,7 @@ where
873873
// actually "transmute" `&mut T` to `&T` in an assignment without a cast.
874874
let layout_compat = mir_assign_valid_types(
875875
*self.tcx,
876-
self.body().phase,
876+
self.body().typing_mode(*self.tcx),
877877
self.param_env,
878878
src.layout(),
879879
dest.layout(),

compiler/rustc_const_eval/src/interpret/stack.rs

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

599-
let layout =
600-
from_known_layout(self.tcx, self.body().phase, self.param_env, layout, || {
599+
let layout = from_known_layout(
600+
self.tcx,
601+
self.body().typing_mode(*self.tcx),
602+
self.param_env,
603+
layout,
604+
|| {
601605
let local_ty = frame.body.local_decls[local].ty;
602606
let local_ty =
603607
self.instantiate_from_frame_and_normalize_erasing_regions(frame, local_ty)?;
604608
self.layout_of(local_ty).into()
605-
})?;
609+
},
610+
)?;
606611

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

compiler/rustc_const_eval/src/util/compare_types.rs

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

1211
/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
1312
pub fn sub_types<'tcx>(
1413
tcx: TyCtxt<'tcx>,
15-
mir_phase: MirPhase,
14+
typing_mode: TypingMode<'tcx>,
1615
param_env: ParamEnv<'tcx>,
1716
src: Ty<'tcx>,
1817
dest: Ty<'tcx>,
1918
) -> bool {
20-
relate_types(tcx, mir_phase, param_env, Variance::Covariant, src, dest)
19+
relate_types(tcx, typing_mode, param_env, Variance::Covariant, src, dest)
2120
}
2221

2322
/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
@@ -27,7 +26,7 @@ pub fn sub_types<'tcx>(
2726
/// because we want to check for type equality.
2827
pub fn relate_types<'tcx>(
2928
tcx: TyCtxt<'tcx>,
30-
mir_phase: MirPhase,
29+
typing_mode: TypingMode<'tcx>,
3130
param_env: ParamEnv<'tcx>,
3231
variance: Variance,
3332
src: Ty<'tcx>,
@@ -38,7 +37,7 @@ pub fn relate_types<'tcx>(
3837
}
3938

4039
let mut builder = tcx.infer_ctxt().ignoring_regions();
41-
let infcx = builder.build(mir_phase.typing_mode());
40+
let infcx = builder.build(typing_mode);
4241
let ocx = ObligationCtxt::new(&infcx);
4342
let cause = ObligationCause::dummy();
4443
let src = ocx.normalize(&cause, param_env, src);

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::ty::fold::{FallibleTypeFolder, TypeFoldable};
3939
use crate::ty::print::{FmtPrinter, Printer, pretty_print_const, with_no_trimmed_paths};
4040
use crate::ty::visit::TypeVisitableExt;
4141
use crate::ty::{
42-
self, AdtDef, GenericArg, GenericArgsRef, Instance, InstanceKind, List, Ty, TyCtxt,
42+
self, AdtDef, GenericArg, GenericArgsRef, Instance, InstanceKind, List, Ty, TyCtxt, TypingMode,
4343
UserTypeAnnotationIndex,
4444
};
4545

@@ -452,6 +452,15 @@ impl<'tcx> Body<'tcx> {
452452
self.basic_blocks.as_mut()
453453
}
454454

455+
pub fn typing_mode(&self, _tcx: TyCtxt<'tcx>) -> TypingMode<'tcx> {
456+
match self.phase {
457+
// FIXME(#132279): the MIR is quite clearly inside of a body, so we
458+
// should instead reveal opaques defined by that body here.
459+
MirPhase::Built | MirPhase::Analysis(_) => TypingMode::non_body_analysis(),
460+
MirPhase::Runtime(_) => TypingMode::PostAnalysis,
461+
}
462+
}
463+
455464
#[inline]
456465
pub fn local_kind(&self, local: Local) -> LocalKind {
457466
let index = local.as_usize();

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ use smallvec::SmallVec;
2020
use super::{BasicBlock, Const, Local, UserTypeProjection};
2121
use crate::mir::coverage::CoverageKind;
2222
use crate::ty::adjustment::PointerCoercion;
23-
use crate::ty::{
24-
self, GenericArgsRef, List, Region, Ty, TyCtxt, TypingMode, UserTypeAnnotationIndex,
25-
};
23+
use crate::ty::{self, GenericArgsRef, List, Region, Ty, TyCtxt, UserTypeAnnotationIndex};
2624

2725
/// Represents the "flavors" of MIR.
2826
///
@@ -103,20 +101,10 @@ impl MirPhase {
103101
}
104102
}
105103

106-
pub fn typing_mode<'tcx>(&self) -> TypingMode<'tcx> {
107-
match self {
108-
// FIXME(#132279): the MIR is quite clearly inside of a body, so we
109-
// should instead reveal opaques defined by that body here.
110-
MirPhase::Built | MirPhase::Analysis(_) => TypingMode::non_body_analysis(),
111-
MirPhase::Runtime(_) => TypingMode::PostAnalysis,
112-
}
113-
}
114-
115104
pub fn param_env<'tcx>(&self, tcx: TyCtxt<'tcx>, body_def_id: DefId) -> ty::ParamEnv<'tcx> {
116-
match self.typing_mode() {
117-
TypingMode::Coherence => unreachable!(),
118-
TypingMode::Analysis { defining_opaque_types: _ } => tcx.param_env(body_def_id),
119-
TypingMode::PostAnalysis => tcx.param_env_reveal_all_normalized(body_def_id),
105+
match self {
106+
MirPhase::Built | MirPhase::Analysis(_) => tcx.param_env(body_def_id),
107+
MirPhase::Runtime(_) => tcx.param_env_reveal_all_normalized(body_def_id),
120108
}
121109
}
122110
}

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'tcx> Inliner<'tcx> {
246246
let output_type = callee_body.return_ty();
247247
if !util::sub_types(
248248
self.tcx,
249-
caller_body.phase,
249+
caller_body.typing_mode(self.tcx),
250250
self.param_env,
251251
output_type,
252252
destination_ty,
@@ -280,8 +280,13 @@ impl<'tcx> Inliner<'tcx> {
280280
self_arg_ty.into_iter().chain(arg_tuple_tys).zip(callee_body.args_iter())
281281
{
282282
let input_type = callee_body.local_decls[input].ty;
283-
if !util::sub_types(self.tcx, caller_body.phase, self.param_env, input_type, arg_ty)
284-
{
283+
if !util::sub_types(
284+
self.tcx,
285+
caller_body.typing_mode(self.tcx),
286+
self.param_env,
287+
input_type,
288+
arg_ty,
289+
) {
285290
trace!(?arg_ty, ?input_type);
286291
return Err("failed to normalize tuple argument type");
287292
}
@@ -290,8 +295,13 @@ impl<'tcx> Inliner<'tcx> {
290295
for (arg, input) in args.iter().zip(callee_body.args_iter()) {
291296
let input_type = callee_body.local_decls[input].ty;
292297
let arg_ty = arg.node.ty(&caller_body.local_decls, self.tcx);
293-
if !util::sub_types(self.tcx, caller_body.phase, self.param_env, input_type, arg_ty)
294-
{
298+
if !util::sub_types(
299+
self.tcx,
300+
caller_body.typing_mode(self.tcx),
301+
self.param_env,
302+
input_type,
303+
arg_ty,
304+
) {
295305
trace!(?arg_ty, ?input_type);
296306
return Err("failed to normalize argument type");
297307
}

compiler/rustc_mir_transform/src/validate.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,14 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
583583
Variance::Covariant
584584
};
585585

586-
crate::util::relate_types(self.tcx, self.body.phase, self.param_env, variance, src, dest)
586+
crate::util::relate_types(
587+
self.tcx,
588+
self.body.typing_mode(self.tcx),
589+
self.param_env,
590+
variance,
591+
src,
592+
dest,
593+
)
587594
}
588595

589596
/// Check that the given predicate definitely holds in the param-env of this MIR body.
@@ -602,7 +609,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
602609
return true;
603610
}
604611

605-
let infcx = self.tcx.infer_ctxt().build(self.body.phase.typing_mode());
612+
let infcx = self.tcx.infer_ctxt().build(self.body.typing_mode(self.tcx));
606613
let ocx = ObligationCtxt::new(&infcx);
607614
ocx.register_obligation(Obligation::new(
608615
self.tcx,
@@ -796,7 +803,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
796803
ProjectionElem::Subtype(ty) => {
797804
if !util::sub_types(
798805
self.tcx,
799-
self.body.phase,
806+
self.body.typing_mode(self.tcx),
800807
self.param_env,
801808
ty,
802809
place_ref.ty(&self.body.local_decls, self.tcx).ty,

compiler/rustc_type_ir/src/infer_ctxt.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ use crate::{self as ty, Interner};
1212
/// The current typing mode of an inference context. We unfortunately have some
1313
/// slightly different typing rules depending on the current context. See the
1414
/// doc comment for each variant for how and why they are used.
15+
///
16+
/// In most cases you can get the correct typing mode automically via:
17+
/// - `mir::Body::typing_mode`
18+
/// - `rustc_lint::LateContext::typing_mode`
19+
///
20+
/// If neither of these functions are available, feel free to reach out to
21+
/// t-types for help.
1522
#[derive_where(Clone, Copy, Hash, PartialEq, Eq, Debug; I: Interner)]
1623
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
1724
pub enum TypingMode<I: Interner> {

src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ fn is_ty_const_destruct<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>
420420
TraitRef::new(tcx, tcx.require_lang_item(LangItem::Destruct, Some(body.span)), [ty]),
421421
);
422422

423-
let infcx = tcx.infer_ctxt().build(body.phase.typing_mode());
423+
let infcx = tcx.infer_ctxt().build(body.typing_mode(tcx));
424424
let mut selcx = SelectionContext::new(&infcx);
425425
let Some(impl_src) = selcx.select(&obligation).ok().flatten() else {
426426
return false;

0 commit comments

Comments
 (0)