Skip to content

Commit

Permalink
Improve TyCtxt::peel_off_ty_alias
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 29, 2022
1 parent dcca5d5 commit 7d76dc9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
31 changes: 26 additions & 5 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,12 +1729,33 @@ impl<'tcx> TyCtxt<'tcx> {

/// As long as the kind of `ty` is `TyAlias`, then it'll continue to peel it off and return
/// the type below it.
pub fn peel_off_ty_alias(self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
while let ty::TyAlias(def_id, substs) = *ty.kind() {
let binder_ty = self.bound_type_of(def_id);
ty = binder_ty.subst(self, substs);
pub fn peel_off_ty_alias<T: ty::TypeFoldable<'tcx>>(self, ty: T) -> T {
struct TyAliasPeeler<'t> {
tcx: TyCtxt<'t>,
}
ty

impl<'t> ty::TypeFolder<'t> for TyAliasPeeler<'t> {
fn tcx<'a>(&'a self) -> TyCtxt<'t> {
self.tcx
}

fn fold_ty(&mut self, t: Ty<'t>) -> Ty<'t> {
use crate::ty::fold::{TypeFoldable, TypeSuperFoldable};
use crate::ty::visit::TypeVisitable;

match *t.kind() {
ty::TyAlias(def_id, substs) => {
let binder_ty = self.tcx.bound_type_of(def_id);
let ty = binder_ty.subst(self.tcx, substs);
ty.fold_with(self)
}
_ if !t.has_ty_alias() => t,
_ => t.super_fold_with(self),
}
}
}

ty.fold_with(&mut TyAliasPeeler { tcx: self })
}
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/ty/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
fn has_opaque_types(&self) -> bool {
self.has_type_flags(TypeFlags::HAS_TY_OPAQUE)
}
fn has_ty_alias(&self) -> bool {
self.has_type_flags(TypeFlags::HAS_TY_ALIAS)
}
fn references_error(&self) -> bool {
self.has_type_flags(TypeFlags::HAS_ERROR)
}
Expand Down

0 comments on commit 7d76dc9

Please sign in to comment.