Skip to content

Commit 8927135

Browse files
Assert that we don't capture escaping bound vars in Fn trait selection
1 parent 5dfb4b0 commit 8927135

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -693,16 +693,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
693693

694694
let gen_sig = substs.as_generator().poly_sig();
695695

696-
// (1) Feels icky to skip the binder here, but OTOH we know
697-
// that the self-type is an generator type and hence is
696+
// NOTE: The self-type is a generator type and hence is
698697
// in fact unparameterized (or at least does not reference any
699-
// regions bound in the obligation). Still probably some
700-
// refactoring could make this nicer.
698+
// regions bound in the obligation).
699+
let self_ty = obligation
700+
.predicate
701+
.self_ty()
702+
.no_bound_vars()
703+
.expect("unboxed closure type should not capture bound vars from the predicate");
701704

702705
let trait_ref = super::util::generator_trait_ref_and_outputs(
703706
self.tcx(),
704707
obligation.predicate.def_id(),
705-
obligation.predicate.skip_binder().self_ty(), // (1)
708+
self_ty,
706709
gen_sig,
707710
)
708711
.map_bound(|(trait_ref, ..)| trait_ref);

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -2271,15 +2271,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
22712271

22722272
debug!(?closure_sig);
22732273

2274-
// (1) Feels icky to skip the binder here, but OTOH we know
2275-
// that the self-type is an unboxed closure type and hence is
2274+
// NOTE: The self-type is an unboxed closure type and hence is
22762275
// in fact unparameterized (or at least does not reference any
2277-
// regions bound in the obligation). Still probably some
2278-
// refactoring could make this nicer.
2276+
// regions bound in the obligation).
2277+
let self_ty = obligation
2278+
.predicate
2279+
.self_ty()
2280+
.no_bound_vars()
2281+
.expect("unboxed closure type should not capture bound vars from the predicate");
2282+
22792283
closure_trait_ref_and_return_type(
22802284
self.tcx(),
22812285
obligation.predicate.def_id(),
2282-
obligation.predicate.skip_binder().self_ty(), // (1)
2286+
self_ty,
22832287
closure_sig,
22842288
util::TupleArgumentsFlag::No,
22852289
)

compiler/rustc_trait_selection/src/traits/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ pub fn closure_trait_ref_and_return_type<'tcx>(
298298
sig: ty::PolyFnSig<'tcx>,
299299
tuple_arguments: TupleArgumentsFlag,
300300
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>)> {
301+
assert!(!self_ty.has_escaping_bound_vars());
301302
let arguments_tuple = match tuple_arguments {
302303
TupleArgumentsFlag::No => sig.skip_binder().inputs()[0],
303304
TupleArgumentsFlag::Yes => tcx.intern_tup(sig.skip_binder().inputs()),
304305
};
305-
debug_assert!(!self_ty.has_escaping_bound_vars());
306306
let trait_ref = tcx.mk_trait_ref(fn_trait_def_id, [self_ty, arguments_tuple]);
307307
sig.map_bound(|sig| (trait_ref, sig.output()))
308308
}
@@ -313,7 +313,7 @@ pub fn generator_trait_ref_and_outputs<'tcx>(
313313
self_ty: Ty<'tcx>,
314314
sig: ty::PolyGenSig<'tcx>,
315315
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>, Ty<'tcx>)> {
316-
debug_assert!(!self_ty.has_escaping_bound_vars());
316+
assert!(!self_ty.has_escaping_bound_vars());
317317
let trait_ref = tcx.mk_trait_ref(fn_trait_def_id, [self_ty, sig.skip_binder().resume_ty]);
318318
sig.map_bound(|sig| (trait_ref, sig.yield_ty, sig.return_ty))
319319
}
@@ -324,7 +324,7 @@ pub fn future_trait_ref_and_outputs<'tcx>(
324324
self_ty: Ty<'tcx>,
325325
sig: ty::PolyGenSig<'tcx>,
326326
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>)> {
327-
debug_assert!(!self_ty.has_escaping_bound_vars());
327+
assert!(!self_ty.has_escaping_bound_vars());
328328
let trait_ref = tcx.mk_trait_ref(fn_trait_def_id, [self_ty]);
329329
sig.map_bound(|sig| (trait_ref, sig.return_ty))
330330
}

0 commit comments

Comments
 (0)