1
Fork 0

Exhaustively match over all alias kinds

This commit is contained in:
Oli Scherer 2023-03-07 10:21:20 +00:00
parent 669e751639
commit d3d537b972
2 changed files with 29 additions and 20 deletions

View file

@ -467,6 +467,11 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
return ty; return ty;
} }
let (kind, data) = match *ty.kind() {
ty::Alias(kind, alias_ty) => (kind, alias_ty),
_ => return ty.super_fold_with(self),
};
// We try to be a little clever here as a performance optimization in // We try to be a little clever here as a performance optimization in
// cases where there are nested projections under binders. // cases where there are nested projections under binders.
// For example: // For example:
@ -490,13 +495,11 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
// replace bound vars if the current type is a `Projection` and we need // replace bound vars if the current type is a `Projection` and we need
// to make sure we don't forget to fold the substs regardless. // to make sure we don't forget to fold the substs regardless.
match *ty.kind() { match kind {
// This is really important. While we *can* handle this, this has // This is really important. While we *can* handle this, this has
// severe performance implications for large opaque types with // severe performance implications for large opaque types with
// late-bound regions. See `issue-88862` benchmark. // late-bound regions. See `issue-88862` benchmark.
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) ty::Opaque if !data.substs.has_escaping_bound_vars() => {
if !substs.has_escaping_bound_vars() =>
{
// Only normalize `impl Trait` outside of type inference, usually in codegen. // Only normalize `impl Trait` outside of type inference, usually in codegen.
match self.param_env.reveal() { match self.param_env.reveal() {
Reveal::UserFacing => ty.super_fold_with(self), Reveal::UserFacing => ty.super_fold_with(self),
@ -512,8 +515,8 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
); );
} }
let substs = substs.fold_with(self); let substs = data.substs.fold_with(self);
let generic_ty = self.interner().type_of(def_id); let generic_ty = self.interner().type_of(data.def_id);
let concrete_ty = generic_ty.subst(self.interner(), substs); let concrete_ty = generic_ty.subst(self.interner(), substs);
self.depth += 1; self.depth += 1;
let folded_ty = self.fold_ty(concrete_ty); let folded_ty = self.fold_ty(concrete_ty);
@ -522,8 +525,9 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
} }
} }
} }
ty::Opaque => ty.super_fold_with(self),
ty::Alias(ty::Projection, data) if !data.has_escaping_bound_vars() => { ty::Projection if !data.has_escaping_bound_vars() => {
// This branch is *mostly* just an optimization: when we don't // This branch is *mostly* just an optimization: when we don't
// have escaping bound vars, we don't need to replace them with // have escaping bound vars, we don't need to replace them with
// placeholders (see branch below). *Also*, we know that we can // placeholders (see branch below). *Also*, we know that we can
@ -562,7 +566,7 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
normalized_ty.ty().unwrap() normalized_ty.ty().unwrap()
} }
ty::Alias(ty::Projection, data) => { ty::Projection => {
// If there are escaping bound vars, we temporarily replace the // If there are escaping bound vars, we temporarily replace the
// bound vars with placeholders. Note though, that in the case // bound vars with placeholders. Note though, that in the case
// that we still can't project for whatever reason (e.g. self // that we still can't project for whatever reason (e.g. self
@ -611,8 +615,6 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
); );
normalized_ty normalized_ty
} }
_ => ty.super_fold_with(self),
} }
} }

View file

@ -197,23 +197,30 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
return Ok(*ty); return Ok(*ty);
} }
let (kind, data) = match *ty.kind() {
ty::Alias(kind, data) => (kind, data),
_ => {
let res = ty.try_super_fold_with(self)?;
self.cache.insert(ty, res);
return Ok(res);
}
};
// See note in `rustc_trait_selection::traits::project` about why we // See note in `rustc_trait_selection::traits::project` about why we
// wait to fold the substs. // wait to fold the substs.
// Wrap this in a closure so we don't accidentally return from the outer function // Wrap this in a closure so we don't accidentally return from the outer function
let res = match *ty.kind() { let res = match kind {
// This is really important. While we *can* handle this, this has // This is really important. While we *can* handle this, this has
// severe performance implications for large opaque types with // severe performance implications for large opaque types with
// late-bound regions. See `issue-88862` benchmark. // late-bound regions. See `issue-88862` benchmark.
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) ty::Opaque if !data.substs.has_escaping_bound_vars() => {
if !substs.has_escaping_bound_vars() =>
{
// Only normalize `impl Trait` outside of type inference, usually in codegen. // Only normalize `impl Trait` outside of type inference, usually in codegen.
match self.param_env.reveal() { match self.param_env.reveal() {
Reveal::UserFacing => ty.try_super_fold_with(self)?, Reveal::UserFacing => ty.try_super_fold_with(self)?,
Reveal::All => { Reveal::All => {
let substs = substs.try_fold_with(self)?; let substs = data.substs.try_fold_with(self)?;
let recursion_limit = self.interner().recursion_limit(); let recursion_limit = self.interner().recursion_limit();
if !recursion_limit.value_within_limit(self.anon_depth) { if !recursion_limit.value_within_limit(self.anon_depth) {
// A closure or generator may have itself as in its upvars. // A closure or generator may have itself as in its upvars.
@ -228,7 +235,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
return ty.try_super_fold_with(self); return ty.try_super_fold_with(self);
} }
let generic_ty = self.interner().type_of(def_id); let generic_ty = self.interner().type_of(data.def_id);
let concrete_ty = generic_ty.subst(self.interner(), substs); let concrete_ty = generic_ty.subst(self.interner(), substs);
self.anon_depth += 1; self.anon_depth += 1;
if concrete_ty == ty { if concrete_ty == ty {
@ -248,7 +255,9 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
} }
} }
ty::Alias(ty::Projection, data) if !data.has_escaping_bound_vars() => { ty::Opaque => ty.try_super_fold_with(self)?,
ty::Projection if !data.has_escaping_bound_vars() => {
// This branch is just an optimization: when we don't have escaping bound vars, // This branch is just an optimization: when we don't have escaping bound vars,
// we don't need to replace them with placeholders (see branch below). // we don't need to replace them with placeholders (see branch below).
@ -297,7 +306,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
} }
} }
ty::Alias(ty::Projection, data) => { ty::Projection => {
// See note in `rustc_trait_selection::traits::project` // See note in `rustc_trait_selection::traits::project`
let tcx = self.infcx.tcx; let tcx = self.infcx.tcx;
@ -353,8 +362,6 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
res res
} }
} }
_ => ty.try_super_fold_with(self)?,
}; };
self.cache.insert(ty, res); self.cache.insert(ty, res);