Skip to content

Commit f515f99

Browse files
committed
Move the peeling function for weak alias types
1 parent 1b3df6f commit f515f99

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

Diff for: compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'tcx> InherentCollect<'tcx> {
144144
let id = id.owner_id.def_id;
145145
let item_span = self.tcx.def_span(id);
146146
let self_ty = self.tcx.type_of(id).instantiate_identity();
147-
let self_ty = peel_off_weak_aliases(self.tcx, self_ty);
147+
let self_ty = self.tcx.peel_off_weak_alias_tys(self_ty);
148148
match *self_ty.kind() {
149149
ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
150150
ty::Foreign(did) => self.check_def_id(id, self_ty, did),
@@ -186,30 +186,3 @@ impl<'tcx> InherentCollect<'tcx> {
186186
}
187187
}
188188
}
189-
190-
/// Peel off all weak alias types in this type until there are none left.
191-
///
192-
/// <div class="warning">
193-
///
194-
/// This assumes that `ty` gets normalized later and that any overflows occurring
195-
/// during said normalization get reported.
196-
///
197-
/// </div>
198-
fn peel_off_weak_aliases<'tcx>(tcx: TyCtxt<'tcx>, mut ty: Ty<'tcx>) -> Ty<'tcx> {
199-
let ty::Alias(ty::Weak, _) = ty.kind() else { return ty };
200-
201-
let limit = tcx.recursion_limit();
202-
let mut depth = 0;
203-
204-
while let ty::Alias(ty::Weak, alias) = ty.kind() {
205-
if !limit.value_within_limit(depth) {
206-
let guar = tcx.dcx().delayed_bug("overflow expanding weak alias type");
207-
return Ty::new_error(tcx, guar);
208-
}
209-
210-
ty = tcx.type_of(alias.def_id).instantiate(tcx, alias.args);
211-
depth += 1;
212-
}
213-
214-
ty
215-
}

Diff for: compiler/rustc_middle/src/ty/util.rs

+33
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,39 @@ impl<'tcx> TyCtxt<'tcx> {
892892
pub fn expand_weak_alias_tys<T: TypeFoldable<TyCtxt<'tcx>>>(self, value: T) -> T {
893893
value.fold_with(&mut WeakAliasTypeExpander { tcx: self, depth: 0 })
894894
}
895+
896+
/// Peel off all [weak alias types] in this type until there are none left.
897+
///
898+
/// This only expands weak alias types in “head” / outermost positions. It can
899+
/// be used over [expand_weak_alias_tys] as an optimization in situations where
900+
/// one only really cares about the *kind* of the final aliased type but not
901+
/// the types the other constituent types alias.
902+
///
903+
/// <div class="warning">
904+
/// This delays a bug on overflow! Therefore you need to be certain that the
905+
/// type gets fully normalized at a later stage.
906+
/// </div>
907+
///
908+
/// [weak]: ty::Weak
909+
/// [expand_weak_alias_tys]: Self::expand_weak_alias_tys
910+
pub fn peel_off_weak_alias_tys(self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
911+
let ty::Alias(ty::Weak, _) = ty.kind() else { return ty };
912+
913+
let limit = self.recursion_limit();
914+
let mut depth = 0;
915+
916+
while let ty::Alias(ty::Weak, alias) = ty.kind() {
917+
if !limit.value_within_limit(depth) {
918+
let guar = self.dcx().delayed_bug("overflow expanding weak alias type");
919+
return Ty::new_error(self, guar);
920+
}
921+
922+
ty = self.type_of(alias.def_id).instantiate(self, alias.args);
923+
depth += 1;
924+
}
925+
926+
ty
927+
}
895928
}
896929

897930
struct OpaqueTypeExpander<'tcx> {

0 commit comments

Comments
 (0)