Skip to content

Commit 8a6f4c8

Browse files
nikomatsakispietroalbini
authored andcommitted
remove "approx env bounds" if we already know from trait
1 parent 0c3dfe1 commit 8a6f4c8

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

src/librustc/infer/outlives/obligations.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,22 +388,38 @@ where
388388
// rule might not apply (but another rule might). For now, we err
389389
// on the side of adding too few edges into the graph.
390390

391+
// Compute the bounds we can derive from the trait definition.
392+
// These are guaranteed to apply, no matter the inference
393+
// results.
394+
let trait_bounds: Vec<_> = self.verify_bound
395+
.projection_declared_bounds_from_trait(projection_ty)
396+
.collect();
397+
391398
// Compute the bounds we can derive from the environment. This
392399
// is an "approximate" match -- in some cases, these bounds
393400
// may not apply.
394-
let approx_env_bounds = self.verify_bound
401+
let mut approx_env_bounds = self.verify_bound
395402
.projection_approx_declared_bounds_from_env(projection_ty);
396403
debug!(
397404
"projection_must_outlive: approx_env_bounds={:?}",
398405
approx_env_bounds
399406
);
400407

401-
// Compute the bounds we can derive from the trait definition.
402-
// These are guaranteed to apply, no matter the inference
403-
// results.
404-
let trait_bounds: Vec<_> = self.verify_bound
405-
.projection_declared_bounds_from_trait(projection_ty)
406-
.collect();
408+
// Remove outlives bounds that we get from the environment but
409+
// which are also deducable from the trait. This arises (cc
410+
// #55756) in cases where you have e.g. `<T as Foo<'a>>::Item:
411+
// 'a` in the environment but `trait Foo<'b> { type Item: 'b
412+
// }` in the trait definition.
413+
approx_env_bounds.retain(|bound| {
414+
match bound.0.sty {
415+
ty::Projection(projection_ty) => {
416+
self.verify_bound.projection_declared_bounds_from_trait(projection_ty)
417+
.all(|r| r != bound.1)
418+
}
419+
420+
_ => panic!("expected only projection types from env, not {:?}", bound.0),
421+
}
422+
});
407423

408424
// If declared bounds list is empty, the only applicable rule is
409425
// OutlivesProjectionComponent. If there are inference variables,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Regression test for #55756.
2+
//
3+
// In this test, the result of `self.callee` is a projection `<D as
4+
// Database<'?0>>::Guard`. As it may contain a destructor, the dropck
5+
// rules require that this type outlivess the scope of `state`. Unfortunately,
6+
// our region inference is not smart enough to figure out how to
7+
// translate a requirement like
8+
//
9+
// <D as Database<'0>>::guard: 'r
10+
//
11+
// into a requirement that `'0: 'r` -- in particular, it fails to do
12+
// so because it *also* knows that `<D as Database<'a>>::Guard: 'a`
13+
// from the trait definition. Faced with so many choices, the current
14+
// solver opts to do nothing.
15+
//
16+
// Fixed by tweaking the solver to recognize that the constraint from
17+
// the environment duplicates one from the trait.
18+
//
19+
// compile-pass
20+
21+
#![crate_type="lib"]
22+
23+
pub trait Database<'a> {
24+
type Guard: 'a;
25+
}
26+
27+
pub struct Stateful<'a, D: 'a>(&'a D);
28+
29+
impl<'b, D: for <'a> Database<'a>> Stateful<'b, D> {
30+
pub fn callee<'a>(&'a self) -> <D as Database<'a>>::Guard {
31+
unimplemented!()
32+
}
33+
pub fn caller<'a>(&'a self) -> <D as Database<'a>>::Guard {
34+
let state = self.callee();
35+
unimplemented!()
36+
}
37+
}

0 commit comments

Comments
 (0)