Skip to content

Commit 2cb7d1c

Browse files
authored
Rollup merge of #98713 - nikomatsakis:issue-98693, r=jackh726
promote placeholder bounds to 'static obligations In NLL, when we are promoting a bound out from a closure, if we have a requirement that `T: 'a` where `'a` is in a higher universe, we were previously ignoring that, which is totally wrong. We should be promoting those constraints to `'static`, since universes are not expressible across closure boundaries. Fixes #98693 ~~(Marking as WIP because I'm still running tests, haven't add the new test, etc)~~ r? ``@jackh726``
2 parents c396bb3 + 7fda862 commit 2cb7d1c

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

+33-5
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
917917
/// The idea then is to lower the `T: 'X` constraint into multiple
918918
/// bounds -- e.g., if `'X` is the union of two free lifetimes,
919919
/// `'1` and `'2`, then we would create `T: '1` and `T: '2`.
920+
#[instrument(level = "debug", skip(self, infcx, propagated_outlives_requirements))]
920921
fn try_promote_type_test(
921922
&self,
922923
infcx: &InferCtxt<'_, 'tcx>,
@@ -934,11 +935,41 @@ impl<'tcx> RegionInferenceContext<'tcx> {
934935
return false;
935936
};
936937

938+
debug!("subject = {:?}", subject);
939+
940+
let r_scc = self.constraint_sccs.scc(*lower_bound);
941+
942+
debug!(
943+
"lower_bound = {:?} r_scc={:?} universe={:?}",
944+
lower_bound, r_scc, self.scc_universes[r_scc]
945+
);
946+
947+
// If the type test requires that `T: 'a` where `'a` is a
948+
// placeholder from another universe, that effectively requires
949+
// `T: 'static`, so we have to propagate that requirement.
950+
//
951+
// It doesn't matter *what* universe because the promoted `T` will
952+
// always be in the root universe.
953+
if let Some(p) = self.scc_values.placeholders_contained_in(r_scc).next() {
954+
debug!("encountered placeholder in higher universe: {:?}, requiring 'static", p);
955+
let static_r = self.universal_regions.fr_static;
956+
propagated_outlives_requirements.push(ClosureOutlivesRequirement {
957+
subject,
958+
outlived_free_region: static_r,
959+
blame_span: locations.span(body),
960+
category: ConstraintCategory::Boring,
961+
});
962+
963+
// we can return here -- the code below might push add'l constraints
964+
// but they would all be weaker than this one.
965+
return true;
966+
}
967+
937968
// For each region outlived by lower_bound find a non-local,
938969
// universal region (it may be the same region) and add it to
939970
// `ClosureOutlivesRequirement`.
940-
let r_scc = self.constraint_sccs.scc(*lower_bound);
941971
for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
972+
debug!("universal_region_outlived_by ur={:?}", ur);
942973
// Check whether we can already prove that the "subject" outlives `ur`.
943974
// If so, we don't have to propagate this requirement to our caller.
944975
//
@@ -963,8 +994,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
963994
continue;
964995
}
965996

966-
debug!("try_promote_type_test: ur={:?}", ur);
967-
968997
let non_local_ub = self.universal_region_relations.non_local_upper_bounds(ur);
969998
debug!("try_promote_type_test: non_local_ub={:?}", non_local_ub);
970999

@@ -1001,15 +1030,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10011030
/// will use it's *external name*, which will be a `RegionKind`
10021031
/// variant that can be used in query responses such as
10031032
/// `ReEarlyBound`.
1033+
#[instrument(level = "debug", skip(self, infcx))]
10041034
fn try_promote_type_test_subject(
10051035
&self,
10061036
infcx: &InferCtxt<'_, 'tcx>,
10071037
ty: Ty<'tcx>,
10081038
) -> Option<ClosureOutlivesSubject<'tcx>> {
10091039
let tcx = infcx.tcx;
10101040

1011-
debug!("try_promote_type_test_subject(ty = {:?})", ty);
1012-
10131041
let ty = tcx.fold_regions(ty, |r, _depth| {
10141042
let region_vid = self.to_region_vid(r);
10151043

src/test/ui/generic-associated-types/issue-91139.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn foo<T>() {
2222
//~| ERROR `T` does not live long enough
2323
//~| ERROR `T` does not live long enough
2424
//~| ERROR `T` does not live long enough
25+
//~| ERROR `T` may not live long enough
2526
//
2627
// FIXME: This error is bogus, but it arises because we try to validate
2728
// that `<() as Foo<T>>::Type<'a>` is valid, which requires proving

src/test/ui/generic-associated-types/issue-91139.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ error: `T` does not live long enough
3434
LL | let _: for<'a> fn(<() as Foo<T>>::Type<'a>, &'a T) = |_, _| ();
3535
| ^^^^^^^^^
3636

37+
error[E0310]: the parameter type `T` may not live long enough
38+
--> $DIR/issue-91139.rs:16:58
39+
|
40+
LL | let _: for<'a> fn(<() as Foo<T>>::Type<'a>, &'a T) = |_, _| ();
41+
| ^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
42+
|
43+
help: consider adding an explicit lifetime bound...
44+
|
45+
LL | fn foo<T: 'static>() {
46+
| +++++++++
47+
3748
error: `T` does not live long enough
3849
--> $DIR/issue-91139.rs:16:58
3950
|
@@ -46,5 +57,6 @@ error: `T` does not live long enough
4657
LL | let _: for<'a> fn(<() as Foo<T>>::Type<'a>, &'a T) = |_, _| ();
4758
| ^^^^^^^^^
4859

49-
error: aborting due to 8 previous errors
60+
error: aborting due to 9 previous errors
5061

62+
For more information about this error, try `rustc --explain E0310`.

src/test/ui/nll/issue-98693.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Regression test for #98693.
2+
//
3+
// The closure encounters an obligation that `T` must outlive `!U1`,
4+
// a placeholder from universe U1. We were ignoring this placeholder
5+
// when promoting the constraint to the enclosing function, and
6+
// thus incorrectly judging the closure to be safe.
7+
8+
fn assert_static<T>()
9+
where
10+
for<'a> T: 'a,
11+
{
12+
}
13+
14+
fn test<T>() {
15+
|| {
16+
//~^ ERROR the parameter type `T` may not live long enough
17+
assert_static::<T>();
18+
};
19+
}
20+
21+
fn main() {}

src/test/ui/nll/issue-98693.stderr

+17
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/issue-98693.rs:15:5
3+
|
4+
LL | / || {
5+
LL | |
6+
LL | | assert_static::<T>();
7+
LL | | };
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 test<T: 'static>() {
13+
| +++++++++
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0310`.

0 commit comments

Comments
 (0)