Skip to content

Commit dc75066

Browse files
committed
normalization folders, yeet ParamEnv::reveal
1 parent 2cde638 commit dc75066

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

compiler/rustc_trait_selection/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ pub fn normalize_param_env_or_error<'tcx>(
411411
debug!("normalize_param_env_or_error: elaborated-predicates={:?}", predicates);
412412

413413
let elaborated_env = ty::ParamEnv::new(tcx.mk_clauses(&predicates), unnormalized_env.reveal());
414-
if !normalize::needs_normalization(&elaborated_env, unnormalized_env.reveal()) {
414+
if !elaborated_env.has_aliases() {
415415
return elaborated_env;
416416
}
417417

compiler/rustc_trait_selection/src/traits/normalize.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
//! Deeply normalize types using the old trait solver.
22
33
use rustc_data_structures::stack::ensure_sufficient_stack;
4-
use rustc_infer::infer::InferOk;
54
use rustc_infer::infer::at::At;
5+
use rustc_infer::infer::{InferCtxt, InferOk};
66
use rustc_infer::traits::{
77
FromSolverError, Normalized, Obligation, PredicateObligations, TraitEngine,
88
};
99
use rustc_macros::extension;
10-
use rustc_middle::traits::{ObligationCause, ObligationCauseCode, Reveal};
10+
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
1111
use rustc_middle::ty::{
1212
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable, TypeVisitableExt,
13+
TypingMode,
1314
};
1415
use tracing::{debug, instrument};
1516

@@ -109,16 +110,19 @@ where
109110
}
110111

111112
pub(super) fn needs_normalization<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
113+
infcx: &InferCtxt<'tcx>,
114+
param_env_for_debug_assertion: ty::ParamEnv<'tcx>,
112115
value: &T,
113-
reveal: Reveal,
114116
) -> bool {
115117
let mut flags = ty::TypeFlags::HAS_ALIAS;
116118

117119
// Opaques are treated as rigid with `Reveal::UserFacing`,
118120
// so we can ignore those.
119-
match reveal {
120-
Reveal::UserFacing => flags.remove(ty::TypeFlags::HAS_TY_OPAQUE),
121-
Reveal::All => {}
121+
match infcx.typing_mode(param_env_for_debug_assertion) {
122+
TypingMode::Coherence | TypingMode::Analysis { defining_opaque_types: _ } => {
123+
flags.remove(ty::TypeFlags::HAS_TY_OPAQUE)
124+
}
125+
TypingMode::PostAnalysis => {}
122126
}
123127

124128
value.has_type_flags(flags)
@@ -154,7 +158,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
154158
"Normalizing {value:?} without wrapping in a `Binder`"
155159
);
156160

157-
if !needs_normalization(&value, self.param_env.reveal()) {
161+
if !needs_normalization(self.selcx.infcx, self.param_env, &value) {
158162
value
159163
} else {
160164
value.fold_with(self)
@@ -178,7 +182,7 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
178182
}
179183

180184
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
181-
if !needs_normalization(&ty, self.param_env.reveal()) {
185+
if !needs_normalization(self.selcx.infcx, self.param_env, &ty) {
182186
return ty;
183187
}
184188

@@ -213,10 +217,11 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
213217
match kind {
214218
ty::Opaque => {
215219
// Only normalize `impl Trait` outside of type inference, usually in codegen.
216-
match self.param_env.reveal() {
217-
Reveal::UserFacing => ty.super_fold_with(self),
218-
219-
Reveal::All => {
220+
match self.selcx.infcx.typing_mode(self.param_env) {
221+
TypingMode::Coherence | TypingMode::Analysis { defining_opaque_types: _ } => {
222+
ty.super_fold_with(self)
223+
}
224+
TypingMode::PostAnalysis => {
220225
let recursion_limit = self.cx().recursion_limit();
221226
if !recursion_limit.value_within_limit(self.depth) {
222227
self.selcx.infcx.err_ctxt().report_overflow_error(
@@ -403,7 +408,7 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
403408
fn fold_const(&mut self, constant: ty::Const<'tcx>) -> ty::Const<'tcx> {
404409
let tcx = self.selcx.tcx();
405410
if tcx.features().generic_const_exprs()
406-
|| !needs_normalization(&constant, self.param_env.reveal())
411+
|| !needs_normalization(self.selcx.infcx, self.param_env, &constant)
407412
{
408413
constant
409414
} else {
@@ -420,7 +425,7 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
420425

421426
#[inline]
422427
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
423-
if p.allow_normalization() && needs_normalization(&p, self.param_env.reveal()) {
428+
if p.allow_normalization() && needs_normalization(self.selcx.infcx, self.param_env, &p) {
424429
p.super_fold_with(self)
425430
} else {
426431
p

compiler/rustc_trait_selection/src/traits/query/normalize.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_macros::extension;
99
pub use rustc_middle::traits::query::NormalizationResult;
1010
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
1111
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt};
12-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor};
12+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor, TypingMode};
1313
use rustc_span::DUMMY_SP;
1414
use tracing::{debug, info, instrument};
1515

@@ -21,7 +21,7 @@ use crate::infer::canonical::OriginalQueryValues;
2121
use crate::infer::{InferCtxt, InferOk};
2222
use crate::traits::normalize::needs_normalization;
2323
use crate::traits::{
24-
BoundVarReplacer, Normalized, ObligationCause, PlaceholderReplacer, Reveal, ScrubbedTraitError,
24+
BoundVarReplacer, Normalized, ObligationCause, PlaceholderReplacer, ScrubbedTraitError,
2525
};
2626

2727
#[extension(pub trait QueryNormalizeExt<'tcx>)]
@@ -89,7 +89,7 @@ impl<'a, 'tcx> At<'a, 'tcx> {
8989
}
9090
}
9191

92-
if !needs_normalization(&value, self.param_env.reveal()) {
92+
if !needs_normalization(self.infcx, self.param_env, &value) {
9393
return Ok(Normalized { value, obligations: PredicateObligations::new() });
9494
}
9595

@@ -191,7 +191,7 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'a, 'tcx> {
191191

192192
#[instrument(level = "debug", skip(self))]
193193
fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
194-
if !needs_normalization(&ty, self.param_env.reveal()) {
194+
if !needs_normalization(self.infcx, self.param_env, &ty) {
195195
return Ok(ty);
196196
}
197197

@@ -215,10 +215,12 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'a, 'tcx> {
215215
let res = match kind {
216216
ty::Opaque => {
217217
// Only normalize `impl Trait` outside of type inference, usually in codegen.
218-
match self.param_env.reveal() {
219-
Reveal::UserFacing => ty.try_super_fold_with(self)?,
218+
match self.infcx.typing_mode(self.param_env) {
219+
TypingMode::Coherence | TypingMode::Analysis { defining_opaque_types: _ } => {
220+
ty.try_super_fold_with(self)?
221+
}
220222

221-
Reveal::All => {
223+
TypingMode::PostAnalysis => {
222224
let args = data.args.try_fold_with(self)?;
223225
let recursion_limit = self.cx().recursion_limit();
224226

@@ -332,7 +334,7 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'a, 'tcx> {
332334
&mut self,
333335
constant: ty::Const<'tcx>,
334336
) -> Result<ty::Const<'tcx>, Self::Error> {
335-
if !needs_normalization(&constant, self.param_env.reveal()) {
337+
if !needs_normalization(self.infcx, self.param_env, &constant) {
336338
return Ok(constant);
337339
}
338340

@@ -351,7 +353,7 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'a, 'tcx> {
351353
&mut self,
352354
p: ty::Predicate<'tcx>,
353355
) -> Result<ty::Predicate<'tcx>, Self::Error> {
354-
if p.allow_normalization() && needs_normalization(&p, self.param_env.reveal()) {
356+
if p.allow_normalization() && needs_normalization(self.infcx, self.param_env, &p) {
355357
p.try_super_fold_with(self)
356358
} else {
357359
Ok(p)

0 commit comments

Comments
 (0)