Skip to content

Commit 47ff6ac

Browse files
Make needs_normalization into a TypeFoldable method
1 parent 36358f6 commit 47ff6ac

File tree

7 files changed

+32
-27
lines changed

7 files changed

+32
-27
lines changed

compiler/rustc_middle/src/ty/normalize_erasing_regions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'tcx> TyCtxt<'tcx> {
5252
let value = self.erase_regions(value);
5353
debug!(?value);
5454

55-
if !value.has_projections() {
55+
if !value.needs_normalization(param_env.reveal()) {
5656
value
5757
} else {
5858
value.fold_with(&mut NormalizeAfterErasingRegionsFolder { tcx: self, param_env })
@@ -84,7 +84,7 @@ impl<'tcx> TyCtxt<'tcx> {
8484
let value = self.erase_regions(value);
8585
debug!(?value);
8686

87-
if !value.has_projections() {
87+
if !value.needs_normalization(param_env.reveal()) {
8888
Ok(value)
8989
} else {
9090
let mut folder = TryNormalizeAfterErasingRegionsFolder::new(self, param_env);

compiler/rustc_middle/src/ty/visit.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
160160
fn still_further_specializable(&self) -> bool {
161161
self.has_type_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE)
162162
}
163+
164+
fn needs_normalization(&self, reveal: ty::Reveal) -> bool {
165+
match reveal {
166+
ty::Reveal::UserFacing => {
167+
self.has_type_flags(TypeFlags::HAS_TY_PROJECTION | TypeFlags::HAS_CT_PROJECTION)
168+
}
169+
ty::Reveal::All => self.has_type_flags(
170+
TypeFlags::HAS_TY_PROJECTION
171+
| TypeFlags::HAS_TY_OPAQUE
172+
| TypeFlags::HAS_CT_PROJECTION,
173+
),
174+
}
175+
}
163176
}
164177

165178
pub trait TypeSuperVisitable<'tcx>: TypeVisitable<'tcx> {
@@ -537,7 +550,7 @@ struct FoundFlags;
537550

538551
// FIXME: Optimize for checking for infer flags
539552
struct HasTypeFlagsVisitor {
540-
flags: ty::TypeFlags,
553+
flags: TypeFlags,
541554
}
542555

543556
impl std::fmt::Debug for HasTypeFlagsVisitor {

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
285285

286286
let infcx = self.selcx.infcx();
287287

288-
if obligation.predicate.has_projections() {
288+
if obligation.predicate.needs_normalization(obligation.param_env.reveal()) {
289289
let mut obligations = Vec::new();
290290
let predicate = crate::traits::project::try_normalize_with_depth_to(
291291
&mut self.selcx,

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,6 @@ where
381381
result
382382
}
383383

384-
pub(crate) fn needs_normalization<'tcx, T: TypeVisitable<'tcx>>(value: &T, reveal: Reveal) -> bool {
385-
match reveal {
386-
Reveal::UserFacing => value
387-
.has_type_flags(ty::TypeFlags::HAS_TY_PROJECTION | ty::TypeFlags::HAS_CT_PROJECTION),
388-
Reveal::All => value.has_type_flags(
389-
ty::TypeFlags::HAS_TY_PROJECTION
390-
| ty::TypeFlags::HAS_TY_OPAQUE
391-
| ty::TypeFlags::HAS_CT_PROJECTION,
392-
),
393-
}
394-
}
395-
396384
struct AssocTypeNormalizer<'a, 'b, 'tcx> {
397385
selcx: &'a mut SelectionContext<'b, 'tcx>,
398386
param_env: ty::ParamEnv<'tcx>,
@@ -453,7 +441,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
453441
value
454442
);
455443

456-
if !needs_normalization(&value, self.param_env.reveal()) {
444+
if !value.needs_normalization(self.param_env.reveal()) {
457445
value
458446
} else {
459447
value.fold_with(self)
@@ -477,7 +465,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
477465
}
478466

479467
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
480-
if !needs_normalization(&ty, self.param_env.reveal()) {
468+
if !ty.needs_normalization(self.param_env.reveal()) {
481469
return ty;
482470
}
483471

@@ -663,7 +651,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
663651

664652
#[inline]
665653
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
666-
if p.allow_normalization() && needs_normalization(&p, self.param_env.reveal()) {
654+
if p.allow_normalization() && p.needs_normalization(self.param_env.reveal()) {
667655
p.super_fold_with(self)
668656
} else {
669657
p
@@ -1124,7 +1112,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>(
11241112

11251113
let projected_term = selcx.infcx().resolve_vars_if_possible(projected_term);
11261114

1127-
let mut result = if projected_term.has_projections() {
1115+
let mut result = if projected_term.needs_normalization(param_env.reveal()) {
11281116
let mut normalizer = AssocTypeNormalizer::new(
11291117
selcx,
11301118
param_env,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::infer::at::At;
66
use crate::infer::canonical::OriginalQueryValues;
77
use crate::infer::{InferCtxt, InferOk};
88
use crate::traits::error_reporting::TypeErrCtxtExt;
9-
use crate::traits::project::{needs_normalization, BoundVarReplacer, PlaceholderReplacer};
9+
use crate::traits::project::{BoundVarReplacer, PlaceholderReplacer};
1010
use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
1111
use rustc_data_structures::sso::SsoHashMap;
1212
use rustc_data_structures::stack::ensure_sufficient_stack;
@@ -54,7 +54,7 @@ impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> {
5454
self.param_env,
5555
self.cause,
5656
);
57-
if !needs_normalization(&value, self.param_env.reveal()) {
57+
if !value.needs_normalization(self.param_env.reveal()) {
5858
return Ok(Normalized { value, obligations: vec![] });
5959
}
6060

@@ -183,7 +183,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
183183

184184
#[instrument(level = "debug", skip(self))]
185185
fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
186-
if !needs_normalization(&ty, self.param_env.reveal()) {
186+
if !ty.needs_normalization(self.param_env.reveal()) {
187187
return Ok(ty);
188188
}
189189

@@ -375,7 +375,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
375375
&mut self,
376376
p: ty::Predicate<'tcx>,
377377
) -> Result<ty::Predicate<'tcx>, Self::Error> {
378-
if p.allow_normalization() && needs_normalization(&p, self.param_env.reveal()) {
378+
if p.allow_normalization() && p.needs_normalization(self.param_env.reveal()) {
379379
p.try_super_fold_with(self)
380380
} else {
381381
Ok(p)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ where
1313
type QueryResponse = T;
1414

1515
fn try_fast_path(_tcx: TyCtxt<'tcx>, key: &ParamEnvAnd<'tcx, Self>) -> Option<T> {
16-
if !key.value.value.has_projections() { Some(key.value.value) } else { None }
16+
if !key.value.value.needs_normalization(key.param_env.reveal()) {
17+
Some(key.value.value)
18+
} else {
19+
None
20+
}
1721
}
1822

1923
fn perform_query(

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ fn layout_of_uncached<'tcx>(
431431

432432
// Arrays and slices.
433433
ty::Array(element, mut count) => {
434-
if count.has_projections() {
434+
if count.needs_normalization(param_env.reveal()) {
435435
count = tcx.normalize_erasing_regions(param_env, count);
436-
if count.has_projections() {
436+
if count.needs_normalization(param_env.reveal()) {
437437
return Err(LayoutError::Unknown(ty));
438438
}
439439
}

0 commit comments

Comments
 (0)