Skip to content

Commit 9fd0972

Browse files
authored
Rollup merge of rust-lang#133798 - lcnr:nested-bodies-opaques, r=compiler-errors
stop replacing bivariant args with `'static` when computing closure requirements It is unnecessary, these get constrained when checking that the opaque type is well-formed. It also results in the opaque type no longer being well formed. If you've got `fn foo<'a>() -> impl Sized + 'a` the opaque is `type Opaque<'a, 'aDummy> where 'a: 'aDummy, 'aDummy: 'a` where `'aDummy` is bivariant. If we call `foo::<'b>()` inside of a closure and its return type ends up in a type test, we start out with the WF `Opaque<'b, 'b>`, and then replace the bivariant `'b` with `'static`. `Opaque<'b, 'static>` is no longer well-formed. Given how these type tests are used, I don't think this caused any practical issues. r? types
2 parents 45088fd + 1c96ddd commit 9fd0972

File tree

6 files changed

+70
-46
lines changed

6 files changed

+70
-46
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

+8-43
Original file line numberDiff line numberDiff line change
@@ -973,25 +973,20 @@ impl<'tcx> RegionInferenceContext<'tcx> {
973973
propagated_outlives_requirements: &mut Vec<ClosureOutlivesRequirement<'tcx>>,
974974
) -> bool {
975975
let tcx = infcx.tcx;
976-
977-
let TypeTest { generic_kind, lower_bound, span: _, verify_bound: _ } = type_test;
976+
let TypeTest { generic_kind, lower_bound, span: blame_span, ref verify_bound } = *type_test;
978977

979978
let generic_ty = generic_kind.to_ty(tcx);
980979
let Some(subject) = self.try_promote_type_test_subject(infcx, generic_ty) else {
981980
return false;
982981
};
983982

984-
debug!("subject = {:?}", subject);
985-
986-
let r_scc = self.constraint_sccs.scc(*lower_bound);
987-
983+
let r_scc = self.constraint_sccs.scc(lower_bound);
988984
debug!(
989985
"lower_bound = {:?} r_scc={:?} universe={:?}",
990986
lower_bound,
991987
r_scc,
992988
self.constraint_sccs.annotation(r_scc).min_universe()
993989
);
994-
995990
// If the type test requires that `T: 'a` where `'a` is a
996991
// placeholder from another universe, that effectively requires
997992
// `T: 'static`, so we have to propagate that requirement.
@@ -1004,7 +999,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1004999
propagated_outlives_requirements.push(ClosureOutlivesRequirement {
10051000
subject,
10061001
outlived_free_region: static_r,
1007-
blame_span: type_test.span,
1002+
blame_span,
10081003
category: ConstraintCategory::Boring,
10091004
});
10101005

@@ -1031,12 +1026,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10311026
// where `ur` is a local bound -- we are sometimes in a
10321027
// position to prove things that our caller cannot. See
10331028
// #53570 for an example.
1034-
if self.eval_verify_bound(infcx, generic_ty, ur, &type_test.verify_bound) {
1029+
if self.eval_verify_bound(infcx, generic_ty, ur, &verify_bound) {
10351030
continue;
10361031
}
10371032

10381033
let non_local_ub = self.universal_region_relations.non_local_upper_bounds(ur);
1039-
debug!("try_promote_type_test: non_local_ub={:?}", non_local_ub);
1034+
debug!(?non_local_ub);
10401035

10411036
// This is slightly too conservative. To show T: '1, given `'2: '1`
10421037
// and `'3: '1` we only need to prove that T: '2 *or* T: '3, but to
@@ -1049,10 +1044,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10491044
let requirement = ClosureOutlivesRequirement {
10501045
subject,
10511046
outlived_free_region: upper_bound,
1052-
blame_span: type_test.span,
1047+
blame_span,
10531048
category: ConstraintCategory::Boring,
10541049
};
1055-
debug!("try_promote_type_test: pushing {:#?}", requirement);
1050+
debug!(?requirement, "adding closure requirement");
10561051
propagated_outlives_requirements.push(requirement);
10571052
}
10581053
}
@@ -1063,44 +1058,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10631058
/// variables in the type `T` with an equal universal region from the
10641059
/// closure signature.
10651060
/// This is not always possible, so this is a fallible process.
1066-
#[instrument(level = "debug", skip(self, infcx))]
1061+
#[instrument(level = "debug", skip(self, infcx), ret)]
10671062
fn try_promote_type_test_subject(
10681063
&self,
10691064
infcx: &InferCtxt<'tcx>,
10701065
ty: Ty<'tcx>,
10711066
) -> Option<ClosureOutlivesSubject<'tcx>> {
10721067
let tcx = infcx.tcx;
1073-
1074-
// Opaque types' args may include useless lifetimes.
1075-
// We will replace them with ReStatic.
1076-
struct OpaqueFolder<'tcx> {
1077-
tcx: TyCtxt<'tcx>,
1078-
}
1079-
impl<'tcx> ty::TypeFolder<TyCtxt<'tcx>> for OpaqueFolder<'tcx> {
1080-
fn cx(&self) -> TyCtxt<'tcx> {
1081-
self.tcx
1082-
}
1083-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
1084-
use ty::TypeSuperFoldable as _;
1085-
let tcx = self.tcx;
1086-
let &ty::Alias(ty::Opaque, ty::AliasTy { args, def_id, .. }) = t.kind() else {
1087-
return t.super_fold_with(self);
1088-
};
1089-
let args = std::iter::zip(args, tcx.variances_of(def_id)).map(|(arg, v)| {
1090-
match (arg.unpack(), v) {
1091-
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => {
1092-
tcx.lifetimes.re_static.into()
1093-
}
1094-
_ => arg.fold_with(self),
1095-
}
1096-
});
1097-
Ty::new_opaque(tcx, def_id, tcx.mk_args_from_iter(args))
1098-
}
1099-
}
1100-
1101-
let ty = ty.fold_with(&mut OpaqueFolder { tcx });
11021068
let mut failed = false;
1103-
11041069
let ty = fold_regions(tcx, ty, |r, _depth| {
11051070
let r_vid = self.to_region_vid(r);
11061071
let r_scc = self.constraint_sccs.scc(r_vid);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This example broke while refactoring the way closure
2+
// requirements are handled. The setup here matches
3+
// `thread::scope`.
4+
5+
//@ check-pass
6+
7+
struct Outlives<'hr, 'scope: 'hr>(*mut (&'scope (), &'hr ()));
8+
impl<'hr, 'scope> Outlives<'hr, 'scope> {
9+
fn outlives_hr<T: 'hr>(self) {}
10+
}
11+
12+
fn takes_closure_implied_bound<'scope>(f: impl for<'hr> FnOnce(Outlives<'hr, 'scope>)) {}
13+
14+
fn requires_external_outlives_hr<T>() {
15+
// implied bounds:
16+
// - `T: 'scope` as `'scope` is local to this function
17+
// - `'scope: 'hr` as it's an implied bound of `Outlives`
18+
//
19+
// need to prove `T: 'hr` :>
20+
takes_closure_implied_bound(|proof| proof.outlives_hr::<T>());
21+
}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This example incorrectly compiled while refactoring the way
2+
// closure requirements are handled.
3+
4+
struct Outlives<'hr: 'scope, 'scope>(*mut (&'scope (), &'hr ()));
5+
impl<'hr, 'scope> Outlives<'hr, 'scope> {
6+
fn outlives_hr<T: 'hr>(self) {}
7+
}
8+
9+
fn takes_closure_implied_bound<'scope>(f: impl for<'hr> FnOnce(Outlives<'hr, 'scope>)) {}
10+
11+
fn requires_external_outlives_hr<T>() {
12+
// implied bounds:
13+
// - `T: 'scope` as `'scope` is local to this function
14+
// - `'hr: 'scope` as it's an implied bound of `Outlives`
15+
//
16+
// need to prove `T: 'hr` :<
17+
takes_closure_implied_bound(|proof| proof.outlives_hr::<T>());
18+
//~^ ERROR the parameter type `T` may not live long enough
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0310]: the parameter type `T` may not live long enough
2+
--> $DIR/thread_scope_incorrect_implied_bound.rs:17:47
3+
|
4+
LL | takes_closure_implied_bound(|proof| proof.outlives_hr::<T>());
5+
| ^^^^^^^^^^^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds
9+
|
10+
help: consider adding an explicit lifetime bound
11+
|
12+
LL | fn requires_external_outlives_hr<T: 'static>() {
13+
| +++++++++
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0310`.

tests/ui/nll/ty-outlives/projection-implied-bounds.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Test that we can deduce when projections like `T::Item` outlive the
22
// function body. Test that this does not imply that `T: 'a` holds.
33

4-
//@ compile-flags:-Zverbose-internals
5-
64
use std::cell::Cell;
75

86
fn twice<F, T>(mut value: T, mut f: F)

tests/ui/nll/ty-outlives/projection-implied-bounds.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0310]: the parameter type `T` may not live long enough
2-
--> $DIR/projection-implied-bounds.rs:30:36
2+
--> $DIR/projection-implied-bounds.rs:28:36
33
|
44
LL | twice(value, |value_ref, item| invoke2(value_ref, item));
55
| ^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)