Skip to content

Commit d3d537b

Browse files
committed
Exhaustively match over all alias kinds
1 parent 669e751 commit d3d537b

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
467467
return ty;
468468
}
469469

470+
let (kind, data) = match *ty.kind() {
471+
ty::Alias(kind, alias_ty) => (kind, alias_ty),
472+
_ => return ty.super_fold_with(self),
473+
};
474+
470475
// We try to be a little clever here as a performance optimization in
471476
// cases where there are nested projections under binders.
472477
// For example:
@@ -490,13 +495,11 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
490495
// replace bound vars if the current type is a `Projection` and we need
491496
// to make sure we don't forget to fold the substs regardless.
492497

493-
match *ty.kind() {
498+
match kind {
494499
// This is really important. While we *can* handle this, this has
495500
// severe performance implications for large opaque types with
496501
// late-bound regions. See `issue-88862` benchmark.
497-
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. })
498-
if !substs.has_escaping_bound_vars() =>
499-
{
502+
ty::Opaque if !data.substs.has_escaping_bound_vars() => {
500503
// Only normalize `impl Trait` outside of type inference, usually in codegen.
501504
match self.param_env.reveal() {
502505
Reveal::UserFacing => ty.super_fold_with(self),
@@ -512,8 +515,8 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
512515
);
513516
}
514517

515-
let substs = substs.fold_with(self);
516-
let generic_ty = self.interner().type_of(def_id);
518+
let substs = data.substs.fold_with(self);
519+
let generic_ty = self.interner().type_of(data.def_id);
517520
let concrete_ty = generic_ty.subst(self.interner(), substs);
518521
self.depth += 1;
519522
let folded_ty = self.fold_ty(concrete_ty);
@@ -522,8 +525,9 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
522525
}
523526
}
524527
}
528+
ty::Opaque => ty.super_fold_with(self),
525529

526-
ty::Alias(ty::Projection, data) if !data.has_escaping_bound_vars() => {
530+
ty::Projection if !data.has_escaping_bound_vars() => {
527531
// This branch is *mostly* just an optimization: when we don't
528532
// have escaping bound vars, we don't need to replace them with
529533
// 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
562566
normalized_ty.ty().unwrap()
563567
}
564568

565-
ty::Alias(ty::Projection, data) => {
569+
ty::Projection => {
566570
// If there are escaping bound vars, we temporarily replace the
567571
// bound vars with placeholders. Note though, that in the case
568572
// 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
611615
);
612616
normalized_ty
613617
}
614-
615-
_ => ty.super_fold_with(self),
616618
}
617619
}
618620

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,30 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
197197
return Ok(*ty);
198198
}
199199

200+
let (kind, data) = match *ty.kind() {
201+
ty::Alias(kind, data) => (kind, data),
202+
_ => {
203+
let res = ty.try_super_fold_with(self)?;
204+
self.cache.insert(ty, res);
205+
return Ok(res);
206+
}
207+
};
208+
200209
// See note in `rustc_trait_selection::traits::project` about why we
201210
// wait to fold the substs.
202211

203212
// Wrap this in a closure so we don't accidentally return from the outer function
204-
let res = match *ty.kind() {
213+
let res = match kind {
205214
// This is really important. While we *can* handle this, this has
206215
// severe performance implications for large opaque types with
207216
// late-bound regions. See `issue-88862` benchmark.
208-
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. })
209-
if !substs.has_escaping_bound_vars() =>
210-
{
217+
ty::Opaque if !data.substs.has_escaping_bound_vars() => {
211218
// Only normalize `impl Trait` outside of type inference, usually in codegen.
212219
match self.param_env.reveal() {
213220
Reveal::UserFacing => ty.try_super_fold_with(self)?,
214221

215222
Reveal::All => {
216-
let substs = substs.try_fold_with(self)?;
223+
let substs = data.substs.try_fold_with(self)?;
217224
let recursion_limit = self.interner().recursion_limit();
218225
if !recursion_limit.value_within_limit(self.anon_depth) {
219226
// 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>
228235
return ty.try_super_fold_with(self);
229236
}
230237

231-
let generic_ty = self.interner().type_of(def_id);
238+
let generic_ty = self.interner().type_of(data.def_id);
232239
let concrete_ty = generic_ty.subst(self.interner(), substs);
233240
self.anon_depth += 1;
234241
if concrete_ty == ty {
@@ -248,7 +255,9 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
248255
}
249256
}
250257

251-
ty::Alias(ty::Projection, data) if !data.has_escaping_bound_vars() => {
258+
ty::Opaque => ty.try_super_fold_with(self)?,
259+
260+
ty::Projection if !data.has_escaping_bound_vars() => {
252261
// This branch is just an optimization: when we don't have escaping bound vars,
253262
// we don't need to replace them with placeholders (see branch below).
254263

@@ -297,7 +306,7 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
297306
}
298307
}
299308

300-
ty::Alias(ty::Projection, data) => {
309+
ty::Projection => {
301310
// See note in `rustc_trait_selection::traits::project`
302311

303312
let tcx = self.infcx.tcx;
@@ -353,8 +362,6 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
353362
res
354363
}
355364
}
356-
357-
_ => ty.try_super_fold_with(self)?,
358365
};
359366

360367
self.cache.insert(ty, res);

0 commit comments

Comments
 (0)