Skip to content

Commit 41eb6d8

Browse files
Instantiate auto trait before computing higher-ranked constituent types
1 parent 3c95364 commit 41eb6d8

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
406406

407407
let self_ty =
408408
obligation.predicate.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty));
409+
let self_ty = self.infcx.enter_forall_and_leak_universe(self_ty);
409410

410411
let types = self.constituent_types_for_ty(self_ty)?;
411412
let types = self.infcx.enter_forall_and_leak_universe(types);

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,9 +2330,9 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23302330
#[instrument(level = "debug", skip(self), ret)]
23312331
fn constituent_types_for_ty(
23322332
&self,
2333-
t: ty::Binder<'tcx, Ty<'tcx>>,
2333+
t: Ty<'tcx>,
23342334
) -> Result<ty::Binder<'tcx, Vec<Ty<'tcx>>>, SelectionError<'tcx>> {
2335-
Ok(match *t.skip_binder().kind() {
2335+
Ok(match *t.kind() {
23362336
ty::Uint(_)
23372337
| ty::Int(_)
23382338
| ty::Bool
@@ -2349,8 +2349,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23492349
// `assemble_candidates_from_auto_impls`.
23502350
ty::Foreign(..) => ty::Binder::dummy(Vec::new()),
23512351

2352-
// FIXME(unsafe_binders): Squash the double binder for now, I guess.
2353-
ty::UnsafeBinder(_) => return Err(SelectionError::Unimplemented),
2352+
ty::UnsafeBinder(ty) => ty.map_bound(|ty| vec![ty]),
23542353

23552354
// Treat this like `struct str([u8]);`
23562355
ty::Str => ty::Binder::dummy(vec![Ty::new_slice(self.tcx(), self.tcx().types.u8)]),
@@ -2364,40 +2363,47 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23642363
bug!("asked to assemble constituent types of unexpected type: {:?}", t);
23652364
}
23662365

2367-
ty::RawPtr(element_ty, _) | ty::Ref(_, element_ty, _) => t.rebind(vec![element_ty]),
2366+
ty::RawPtr(element_ty, _) | ty::Ref(_, element_ty, _) => {
2367+
ty::Binder::dummy(vec![element_ty])
2368+
}
23682369

2369-
ty::Pat(ty, _) | ty::Array(ty, _) | ty::Slice(ty) => t.rebind(vec![ty]),
2370+
ty::Pat(ty, _) | ty::Array(ty, _) | ty::Slice(ty) => ty::Binder::dummy(vec![ty]),
23702371

23712372
ty::Tuple(tys) => {
23722373
// (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
2373-
t.rebind(tys.iter().collect())
2374+
ty::Binder::dummy(tys.iter().collect())
23742375
}
23752376

23762377
ty::Closure(_, args) => {
23772378
let ty = self.infcx.shallow_resolve(args.as_closure().tupled_upvars_ty());
2378-
t.rebind(vec![ty])
2379+
ty::Binder::dummy(vec![ty])
23792380
}
23802381

23812382
ty::CoroutineClosure(_, args) => {
23822383
let ty = self.infcx.shallow_resolve(args.as_coroutine_closure().tupled_upvars_ty());
2383-
t.rebind(vec![ty])
2384+
ty::Binder::dummy(vec![ty])
23842385
}
23852386

23862387
ty::Coroutine(_, args) => {
23872388
let ty = self.infcx.shallow_resolve(args.as_coroutine().tupled_upvars_ty());
23882389
let witness = args.as_coroutine().witness();
2389-
t.rebind([ty].into_iter().chain(iter::once(witness)).collect())
2390+
ty::Binder::dummy([ty].into_iter().chain(iter::once(witness)).collect())
23902391
}
23912392

2392-
ty::CoroutineWitness(def_id, args) => {
2393-
rebind_coroutine_witness_types(self.infcx.tcx, def_id, args, t.bound_vars())
2394-
}
2393+
ty::CoroutineWitness(def_id, args) => self
2394+
.infcx
2395+
.tcx
2396+
.coroutine_hidden_types(def_id)
2397+
.instantiate(self.infcx.tcx, args)
2398+
.map_bound(|witness| witness.types.to_vec()),
23952399

23962400
// For `PhantomData<T>`, we pass `T`.
2397-
ty::Adt(def, args) if def.is_phantom_data() => t.rebind(args.types().collect()),
2401+
ty::Adt(def, args) if def.is_phantom_data() => {
2402+
ty::Binder::dummy(args.types().collect())
2403+
}
23982404

23992405
ty::Adt(def, args) => {
2400-
t.rebind(def.all_fields().map(|f| f.ty(self.tcx(), args)).collect())
2406+
ty::Binder::dummy(def.all_fields().map(|f| f.ty(self.tcx(), args)).collect())
24012407
}
24022408

24032409
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
@@ -2408,7 +2414,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
24082414
// which enforces a DAG between the functions requiring
24092415
// the auto trait bounds in question.
24102416
match self.tcx().type_of_opaque(def_id) {
2411-
Ok(ty) => t.rebind(vec![ty.instantiate(self.tcx(), args)]),
2417+
Ok(ty) => ty::Binder::dummy(vec![ty.instantiate(self.tcx(), args)]),
24122418
Err(_) => {
24132419
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
24142420
}

0 commit comments

Comments
 (0)