Skip to content

Commit 7d76dc9

Browse files
Improve TyCtxt::peel_off_ty_alias
1 parent dcca5d5 commit 7d76dc9

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

compiler/rustc_middle/src/ty/context.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -1729,12 +1729,33 @@ impl<'tcx> TyCtxt<'tcx> {
17291729

17301730
/// As long as the kind of `ty` is `TyAlias`, then it'll continue to peel it off and return
17311731
/// the type below it.
1732-
pub fn peel_off_ty_alias(self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
1733-
while let ty::TyAlias(def_id, substs) = *ty.kind() {
1734-
let binder_ty = self.bound_type_of(def_id);
1735-
ty = binder_ty.subst(self, substs);
1732+
pub fn peel_off_ty_alias<T: ty::TypeFoldable<'tcx>>(self, ty: T) -> T {
1733+
struct TyAliasPeeler<'t> {
1734+
tcx: TyCtxt<'t>,
17361735
}
1737-
ty
1736+
1737+
impl<'t> ty::TypeFolder<'t> for TyAliasPeeler<'t> {
1738+
fn tcx<'a>(&'a self) -> TyCtxt<'t> {
1739+
self.tcx
1740+
}
1741+
1742+
fn fold_ty(&mut self, t: Ty<'t>) -> Ty<'t> {
1743+
use crate::ty::fold::{TypeFoldable, TypeSuperFoldable};
1744+
use crate::ty::visit::TypeVisitable;
1745+
1746+
match *t.kind() {
1747+
ty::TyAlias(def_id, substs) => {
1748+
let binder_ty = self.tcx.bound_type_of(def_id);
1749+
let ty = binder_ty.subst(self.tcx, substs);
1750+
ty.fold_with(self)
1751+
}
1752+
_ if !t.has_ty_alias() => t,
1753+
_ => t.super_fold_with(self),
1754+
}
1755+
}
1756+
}
1757+
1758+
ty.fold_with(&mut TyAliasPeeler { tcx: self })
17381759
}
17391760
}
17401761

compiler/rustc_middle/src/ty/visit.rs

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
9292
fn has_opaque_types(&self) -> bool {
9393
self.has_type_flags(TypeFlags::HAS_TY_OPAQUE)
9494
}
95+
fn has_ty_alias(&self) -> bool {
96+
self.has_type_flags(TypeFlags::HAS_TY_ALIAS)
97+
}
9598
fn references_error(&self) -> bool {
9699
self.has_type_flags(TypeFlags::HAS_ERROR)
97100
}

0 commit comments

Comments
 (0)