Skip to content

Commit 766c796

Browse files
committed
introduce EqTypingMode and make TypingMode !Eq
1 parent 4c42051 commit 766c796

18 files changed

Lines changed: 160 additions & 49 deletions

File tree

compiler/rustc_const_eval/src/check_consts/qualifs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_errors::ErrorGuaranteed;
99
use rustc_hir::LangItem;
1010
use rustc_infer::infer::TyCtxtInferExt;
1111
use rustc_middle::mir::*;
12-
use rustc_middle::ty::{self, AdtDef, Ty};
12+
use rustc_middle::ty::{self, AdtDef, EqTypingMode, Ty};
1313
use rustc_middle::{bug, mir};
1414
use rustc_trait_selection::traits::{Obligation, ObligationCause, ObligationCtxt};
1515
use tracing::instrument;
@@ -104,10 +104,10 @@ impl Qualif for HasMutInterior {
104104
// typeck results without causing query cycles, we should use this here instead of defining
105105
// opaque types.
106106
let typing_env = ty::TypingEnv {
107-
typing_mode: ty::TypingMode::analysis_in_body(
107+
typing_mode: EqTypingMode(ty::TypingMode::analysis_in_body(
108108
cx.tcx,
109109
cx.body.source.def_id().expect_local(),
110-
),
110+
)),
111111
param_env: cx.typing_env.param_env,
112112
};
113113
let (infcx, param_env) = cx.tcx.infer_ctxt().build_with_typing_env(typing_env);

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,20 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
371371
// This shouldn't be used for statics, since statics are conceptually places,
372372
// not values -- so what we do here could break pointer identity.
373373
assert!(key.value.promoted.is_some() || !tcx.is_static(key.value.instance.def_id()));
374-
// Const eval always happens in PostAnalysis mode . See the comment in
375-
// `InterpCx::new` for more details.
376-
debug_assert_eq!(key.typing_env.typing_mode, ty::TypingMode::PostAnalysis);
374+
375+
#[cfg(debug_assertions)]
376+
match key.typing_env.typing_mode.0 {
377+
ty::TypingMode::PostAnalysis => {}
378+
ty::TypingMode::Coherence
379+
| ty::TypingMode::Analysis { .. }
380+
| ty::TypingMode::Borrowck { .. }
381+
| ty::TypingMode::PostBorrowckAnalysis { .. } => {
382+
bug!(
383+
"Const eval should always happens in PostAnalysis mode. See the comment in `InterpCx::new` for more details."
384+
)
385+
}
386+
}
387+
377388
if cfg!(debug_assertions) {
378389
// Make sure we format the instance even if we do not print it.
379390
// This serves as a regression test against an ICE on printing.

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,18 @@ pub(crate) fn eval_to_valtree<'tcx>(
236236
typing_env: ty::TypingEnv<'tcx>,
237237
cid: GlobalId<'tcx>,
238238
) -> EvalToValTreeResult<'tcx> {
239-
// Const eval always happens in PostAnalysis mode . See the comment in
240-
// `InterpCx::new` for more details.
241-
debug_assert_eq!(typing_env.typing_mode, ty::TypingMode::PostAnalysis);
239+
#[cfg(debug_assertions)]
240+
match typing_env.typing_mode.0 {
241+
ty::TypingMode::PostAnalysis => {}
242+
ty::TypingMode::Coherence
243+
| ty::TypingMode::Analysis { .. }
244+
| ty::TypingMode::Borrowck { .. }
245+
| ty::TypingMode::PostBorrowckAnalysis { .. } => {
246+
bug!(
247+
"Const eval should always happens in PostAnalysis mode. See the comment in `InterpCx::new` for more details."
248+
)
249+
}
250+
}
242251
let const_alloc = tcx.eval_to_allocation_raw(typing_env.as_query_input(cid))?;
243252

244253
// FIXME Need to provide a span to `eval_to_valtree`

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
243243
// opaque types. This is needed for trivial things like `size_of`, but also for using associated
244244
// types that are not specified in the opaque type. We also use MIR bodies whose opaque types have
245245
// already been revealed, so we'd be able to at least partially observe the hidden types anyways.
246-
debug_assert_matches!(typing_env.typing_mode, ty::TypingMode::PostAnalysis);
246+
debug_assert_matches!(typing_env.typing_mode.0, ty::TypingMode::PostAnalysis);
247+
#[cfg(debug_assertions)]
248+
match typing_env.typing_mode.0 {
249+
ty::TypingMode::PostAnalysis => {}
250+
ty::TypingMode::Coherence
251+
| ty::TypingMode::Analysis { .. }
252+
| ty::TypingMode::Borrowck { .. }
253+
| ty::TypingMode::PostBorrowckAnalysis { .. } => {
254+
use rustc_middle::bug;
255+
256+
bug!("Const eval should always happens in PostAnalysis mode.")
257+
}
258+
}
259+
247260
InterpCx {
248261
machine,
249262
tcx: tcx.at(root_span),

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::ty::{
1313
self, BoundVar, GenericArg, InferConst, List, Ty, TyCtxt, TypeFlags, TypeFoldable, TypeFolder,
1414
TypeSuperFoldable, TypeVisitableExt,
1515
};
16+
use rustc_type_ir::EqTypingMode;
1617
use smallvec::SmallVec;
1718
use tracing::debug;
1819

@@ -72,7 +73,7 @@ impl<'tcx> InferCtxt<'tcx> {
7273
query_state,
7374
)
7475
.unchecked_map(|(param_env, value)| param_env.and(value));
75-
CanonicalQueryInput { canonical, typing_mode: self.typing_mode() }
76+
CanonicalQueryInput { canonical, typing_mode: EqTypingMode(self.typing_mode()) }
7677
}
7778

7879
/// Canonicalizes a query *response* `V`. When we canonicalize a

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rustc_middle::ty::{
3333
TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypingEnv, TypingMode, fold_regions,
3434
};
3535
use rustc_span::{DUMMY_SP, Span, Symbol};
36+
use rustc_type_ir::EqTypingMode;
3637
use snapshot::undo_log::InferCtxtUndoLogs;
3738
use tracing::{debug, instrument};
3839
use type_variable::TypeVariableOrigin;
@@ -564,7 +565,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
564565
where
565566
T: TypeFoldable<TyCtxt<'tcx>>,
566567
{
567-
let infcx = self.build(input.typing_mode);
568+
let infcx = self.build(input.typing_mode.0);
568569
let (value, args) = infcx.instantiate_canonical(span, &input.canonical);
569570
(infcx, value, args)
570571
}
@@ -573,7 +574,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
573574
mut self,
574575
TypingEnv { typing_mode, param_env }: TypingEnv<'tcx>,
575576
) -> (InferCtxt<'tcx>, ty::ParamEnv<'tcx>) {
576-
(self.build(typing_mode), param_env)
577+
(self.build(typing_mode.0), param_env)
577578
}
578579

579580
pub fn build(&mut self, typing_mode: TypingMode<'tcx>) -> InferCtxt<'tcx> {
@@ -1376,7 +1377,7 @@ impl<'tcx> InferCtxt<'tcx> {
13761377
| ty::TypingMode::PostBorrowckAnalysis { .. }
13771378
| ty::TypingMode::PostAnalysis) => mode,
13781379
};
1379-
ty::TypingEnv { typing_mode, param_env }
1380+
ty::TypingEnv { typing_mode: EqTypingMode(typing_mode), param_env }
13801381
}
13811382

13821383
/// Similar to [`Self::canonicalize_query`], except that it returns

compiler/rustc_lint/src/context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use rustc_middle::lint::LevelAndSource;
2424
use rustc_middle::middle::privacy::EffectiveVisibilities;
2525
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
2626
use rustc_middle::ty::print::{PrintError, PrintTraitRefExt as _, Printer, with_no_trimmed_paths};
27-
use rustc_middle::ty::{self, GenericArg, RegisteredTools, Ty, TyCtxt, TypingEnv, TypingMode};
27+
use rustc_middle::ty::{
28+
self, EqTypingMode, GenericArg, RegisteredTools, Ty, TyCtxt, TypingEnv, TypingMode,
29+
};
2830
use rustc_session::lint::{
2931
CheckLintNameResult, FutureIncompatibleInfo, Lint, LintExpectationId, LintId, TargetLint,
3032
};
@@ -615,7 +617,7 @@ impl<'tcx> LateContext<'tcx> {
615617
}
616618

617619
pub fn typing_env(&self) -> TypingEnv<'tcx> {
618-
TypingEnv { typing_mode: self.typing_mode(), param_env: self.param_env }
620+
TypingEnv { typing_mode: EqTypingMode(self.typing_mode()), param_env: self.param_env }
619621
}
620622

621623
pub fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>) -> bool {

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ use crate::mir::interpret::{AllocRange, Scalar};
3232
use crate::ty::codec::{TyDecoder, TyEncoder};
3333
use crate::ty::print::{FmtPrinter, Printer, pretty_print_const, with_no_trimmed_paths};
3434
use crate::ty::{
35-
self, GenericArg, GenericArgsRef, Instance, InstanceKind, List, Ty, TyCtxt, TypeVisitableExt,
36-
TypingEnv, UserTypeAnnotationIndex,
35+
self, EqTypingMode, GenericArg, GenericArgsRef, Instance, InstanceKind, List, Ty, TyCtxt,
36+
TypeVisitableExt, TypingEnv, UserTypeAnnotationIndex,
3737
};
3838

3939
mod basic_blocks;
@@ -416,7 +416,7 @@ impl<'tcx> Body<'tcx> {
416416
match self.phase {
417417
// FIXME(#132279): we should reveal the opaques defined in the body during analysis.
418418
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv {
419-
typing_mode: ty::TypingMode::non_body_analysis(),
419+
typing_mode: EqTypingMode(ty::TypingMode::non_body_analysis()),
420420
param_env: tcx.param_env(self.source.def_id()),
421421
},
422422
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ pub use self::region::{
103103
};
104104
pub use self::sty::{
105105
AliasTy, AliasTyKind, Article, Binder, BoundConst, BoundRegion, BoundRegionKind, BoundTy,
106-
BoundTyKind, BoundVariableKind, CanonicalPolyFnSig, CoroutineArgsExt, EarlyBinder, FnSig,
107-
InlineConstArgs, InlineConstArgsParts, ParamConst, ParamTy, PlaceholderConst,
108-
PlaceholderRegion, PlaceholderType, PolyFnSig, TyKind, TypeAndMut, TypingMode, UpvarArgs,
106+
BoundTyKind, BoundVariableKind, CanonicalPolyFnSig, CoroutineArgsExt, EarlyBinder,
107+
EqTypingMode, FnSig, InlineConstArgs, InlineConstArgsParts, ParamConst, ParamTy,
108+
PlaceholderConst, PlaceholderRegion, PlaceholderType, PolyFnSig, TyKind, TypeAndMut,
109+
TypingMode, UpvarArgs,
109110
};
110111
pub use self::trait_def::TraitDef;
111112
pub use self::typeck_results::{
@@ -980,7 +981,7 @@ pub struct ParamEnvAnd<'tcx, T> {
980981
pub struct TypingEnv<'tcx> {
981982
#[type_foldable(identity)]
982983
#[type_visitable(ignore)]
983-
pub typing_mode: TypingMode<'tcx>,
984+
pub typing_mode: EqTypingMode<'tcx>,
984985
pub param_env: ParamEnv<'tcx>,
985986
}
986987

@@ -993,7 +994,10 @@ impl<'tcx> TypingEnv<'tcx> {
993994
/// use `TypingMode::PostAnalysis`, they may still have where-clauses
994995
/// in scope.
995996
pub fn fully_monomorphized() -> TypingEnv<'tcx> {
996-
TypingEnv { typing_mode: TypingMode::PostAnalysis, param_env: ParamEnv::empty() }
997+
TypingEnv {
998+
typing_mode: EqTypingMode(TypingMode::PostAnalysis),
999+
param_env: ParamEnv::empty(),
1000+
}
9971001
}
9981002

9991003
/// Create a typing environment for use during analysis outside of a body.
@@ -1006,7 +1010,10 @@ impl<'tcx> TypingEnv<'tcx> {
10061010
def_id: impl IntoQueryKey<DefId>,
10071011
) -> TypingEnv<'tcx> {
10081012
let def_id = def_id.into_query_key();
1009-
TypingEnv { typing_mode: TypingMode::non_body_analysis(), param_env: tcx.param_env(def_id) }
1013+
TypingEnv {
1014+
typing_mode: EqTypingMode(TypingMode::non_body_analysis()),
1015+
param_env: tcx.param_env(def_id),
1016+
}
10101017
}
10111018

10121019
pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryKey<DefId>) -> TypingEnv<'tcx> {
@@ -1018,8 +1025,12 @@ impl<'tcx> TypingEnv<'tcx> {
10181025
/// opaque types in the `param_env`.
10191026
pub fn with_post_analysis_normalized(self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
10201027
let TypingEnv { typing_mode, param_env } = self;
1021-
if let TypingMode::PostAnalysis = typing_mode {
1022-
return self;
1028+
match typing_mode.0 {
1029+
TypingMode::Coherence
1030+
| TypingMode::Analysis { .. }
1031+
| TypingMode::Borrowck { .. }
1032+
| TypingMode::PostBorrowckAnalysis { .. } => {}
1033+
TypingMode::PostAnalysis => return self,
10231034
}
10241035

10251036
// No need to reveal opaques with the new solver enabled,
@@ -1029,7 +1040,7 @@ impl<'tcx> TypingEnv<'tcx> {
10291040
} else {
10301041
ParamEnv::new(tcx.reveal_opaque_types_in_bounds(param_env.caller_bounds()))
10311042
};
1032-
TypingEnv { typing_mode: TypingMode::PostAnalysis, param_env }
1043+
TypingEnv { typing_mode: EqTypingMode(TypingMode::PostAnalysis), param_env }
10331044
}
10341045

10351046
/// Combine this typing environment with the given `value` to be used by

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub type FnSig<'tcx> = ir::FnSig<TyCtxt<'tcx>>;
4040
pub type Binder<'tcx, T> = ir::Binder<TyCtxt<'tcx>, T>;
4141
pub type EarlyBinder<'tcx, T> = ir::EarlyBinder<TyCtxt<'tcx>, T>;
4242
pub type TypingMode<'tcx> = ir::TypingMode<TyCtxt<'tcx>>;
43+
pub type EqTypingMode<'tcx> = ir::EqTypingMode<TyCtxt<'tcx>>;
4344
pub type Placeholder<'tcx, T> = ir::Placeholder<TyCtxt<'tcx>, T>;
4445
pub type PlaceholderRegion<'tcx> = ir::PlaceholderRegion<TyCtxt<'tcx>>;
4546
pub type PlaceholderType<'tcx> = ir::PlaceholderType<TyCtxt<'tcx>>;

0 commit comments

Comments
 (0)