Skip to content

Commit 43e7587

Browse files
committed
avoid propagating outlives obligations on locals if we can
1 parent db169e5 commit 43e7587

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,23 @@ impl<'tcx> RegionInferenceContext<'tcx> {
701701
// `ClosureOutlivesRequirement`.
702702
let r_scc = self.constraint_sccs.scc(*lower_bound);
703703
for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
704+
// Check whether we can already prove that the "subject" outlives `ur`.
705+
// If so, we don't have to propagate this requirement to our caller.
706+
//
707+
// To continue the example from the function, if we are trying to promote
708+
// a requirement that `T: 'X`, and we know that `'X = '1 + '2` (i.e., the union
709+
// `'1` and `'2`), then in this loop `ur` will be `'1` (and `'2`). So here
710+
// we check whether `T: '1` is something we *can* prove. If so, no need
711+
// to propagate that requirement.
712+
//
713+
// This is needed because -- particularly in the case
714+
// where `ur` is a local bound -- we are sometimes in a
715+
// position to prove things that our caller cannot. See
716+
// #53570 for an example.
717+
if self.eval_region_test(mir, ur, &type_test.test) {
718+
continue;
719+
}
720+
704721
debug!("try_promote_type_test: ur={:?}", ur);
705722

706723
let non_local_ub = self.universal_region_relations.non_local_upper_bound(ur);

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

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for #53570. Here, we need to propagate that `T: 'a`
12+
// but in some versions of NLL we were propagating a stronger
13+
// requirement that `T: 'static`. This arose because we actually had
14+
// to propagate both that `T: 'a` but also `T: 'b` where `'b` is the
15+
// higher-ranked lifetime that appears in the type of the closure
16+
// parameter `x` -- since `'b` cannot be expressed in the caller's
17+
// space, that got promoted th `'static`.
18+
//
19+
// compile-pass
20+
21+
#![feature(nll)]
22+
#![feature(rustc_attrs)]
23+
#![allow(dead_code)]
24+
25+
use std::cell::{RefCell, Ref};
26+
27+
trait AnyVec<'a> {
28+
}
29+
30+
trait GenericVec<T> {
31+
fn unwrap<'a, 'b>(vec: &'b AnyVec<'a>) -> &'b [T] where T: 'a;
32+
}
33+
34+
struct Scratchpad<'a> {
35+
buffers: RefCell<Box<AnyVec<'a>>>,
36+
}
37+
38+
impl<'a> Scratchpad<'a> {
39+
fn get<T: GenericVec<T>>(&self) -> Ref<[T]>
40+
where T: 'a
41+
{
42+
Ref::map(self.buffers.borrow(), |x| T::unwrap(x.as_ref()))
43+
}
44+
}
45+
46+
fn main() { }

0 commit comments

Comments
 (0)