Skip to content

Commit c67ea54

Browse files
committed
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
handle outlives predicates in trait evaluation This handles higher-ranked outlives predicates in trait evaluation the same way they are handled in projection. Fixes #54302. I think this is a more correct fix than #54401 because it fixes the root case in evaluation instead of making evaluation used in less cases. However, we might want to go to a direction closer to @nikomatsakis's solution with Chalk. r? @nikomatsakis
2 parents d078728 + 1069c0e commit c67ea54

File tree

7 files changed

+328
-5
lines changed

7 files changed

+328
-5
lines changed

src/librustc/traits/fulfill.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl<'a, 'b, 'gcx, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'gcx,
292292
ty::Predicate::Trait(ref data) => {
293293
let trait_obligation = obligation.with(data.clone());
294294

295-
if data.is_global() && !data.has_late_bound_regions() {
295+
if data.is_global() {
296296
// no type variables present, can use evaluation for better caching.
297297
// FIXME: consider caching errors too.
298298
if self.selcx.infcx().predicate_must_hold(&obligation) {
@@ -362,6 +362,7 @@ impl<'a, 'b, 'gcx, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'gcx,
362362
match binder.no_late_bound_regions() {
363363
// If so, this obligation is an error (for now). Eventually we should be
364364
// able to support additional cases here, like `for<'a> &'a str: 'a`.
365+
// NOTE: this is duplicate-implemented between here and fulfillment.
365366
None => {
366367
ProcessResult::Error(CodeSelectionError(Unimplemented))
367368
}

src/librustc/traits/select.rs

+87-4
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,92 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
690690
}
691691
}
692692

693-
ty::Predicate::TypeOutlives(..) | ty::Predicate::RegionOutlives(..) => {
694-
// we do not consider region relationships when
695-
// evaluating trait matches
696-
Ok(EvaluatedToOk)
693+
ty::Predicate::TypeOutlives(ref binder) => {
694+
assert!(!binder.has_escaping_regions());
695+
// Check if the type has higher-ranked regions.
696+
if binder.skip_binder().0.has_escaping_regions() {
697+
// If so, this obligation is an error (for now). Eventually we should be
698+
// able to support additional cases here, like `for<'a> &'a str: 'a`.
699+
700+
// NOTE: this hack is implemented in both trait fulfillment and
701+
// evaluation. If you fix it in one place, make sure you fix it
702+
// in the other.
703+
704+
// We don't want to allow this sort of reasoning in intercrate
705+
// mode, for backwards-compatibility reasons.
706+
if self.intercrate.is_some() {
707+
Ok(EvaluatedToAmbig)
708+
} else {
709+
Ok(EvaluatedToErr)
710+
}
711+
} else {
712+
// If the type has no late bound regions, then if we assign all
713+
// the inference variables in it to be 'static, then the type
714+
// will be 'static itself.
715+
//
716+
// Therefore, `staticize(T): 'a` holds for any `'a`, so this
717+
// obligation is fulfilled. Because evaluation works with
718+
// staticized types (yes I know this is involved with #21974),
719+
// we are 100% OK here.
720+
Ok(EvaluatedToOk)
721+
}
722+
}
723+
724+
ty::Predicate::RegionOutlives(ref binder) => {
725+
let ty::OutlivesPredicate(r_a, r_b) = binder.skip_binder();
726+
727+
if r_a == r_b {
728+
// for<'a> 'a: 'a. OK
729+
Ok(EvaluatedToOk)
730+
} else if **r_a == ty::ReStatic {
731+
// 'static: 'x always holds.
732+
//
733+
// This special case is handled somewhat inconsistently - if we
734+
// have an inference variable that is supposed to be equal to
735+
// `'static`, then we don't allow it to be equated to an LBR,
736+
// but if we have a literal `'static`, then we *do*.
737+
//
738+
// This is actually consistent with how our region inference works.
739+
//
740+
// It would appear that this sort of inconsistency would
741+
// cause "instability" problems with evaluation caching. However,
742+
// evaluation caching is only for trait predicates, and when
743+
// trait predicates create nested obligations, they contain
744+
// inference variables for all the regions in the trait - the
745+
// only way this codepath can be reached from trait predicate
746+
// evaluation is when the user typed an explicit `where 'static: 'a`
747+
// lifetime bound (in which case we want to return EvaluatedToOk).
748+
//
749+
// If we ever want to handle inference variables that might be
750+
// equatable with ReStatic, we need to make sure we are not confused by
751+
// technically-allowed-by-RFC-447-but-probably-should-not-be
752+
// impls such as
753+
// ```Rust
754+
// impl<'a, 's, T> X<'s> for T where T: Debug + 'a, 'a: 's
755+
// ```
756+
Ok(EvaluatedToOk)
757+
} else if r_a.is_late_bound() || r_b.is_late_bound() {
758+
// There is no current way to prove `for<'a> 'a: 'x`
759+
// unless `'a = 'x`, because there are no bounds involving
760+
// lifetimes.
761+
762+
// It might be possible to prove `for<'a> 'x: 'a` by forcing `'x`
763+
// to be `'static`. However, this is not currently done by type
764+
// inference unless `'x` is literally ReStatic. See the comment
765+
// above.
766+
767+
// We don't want to allow this sort of reasoning in intercrate
768+
// mode, for backwards-compatibility reasons.
769+
if self.intercrate.is_some() {
770+
Ok(EvaluatedToAmbig)
771+
} else {
772+
Ok(EvaluatedToErr)
773+
}
774+
} else {
775+
// Relating 2 inference variable regions. These will
776+
// always hold if our query is "staticized".
777+
Ok(EvaluatedToOk)
778+
}
697779
}
698780

699781
ty::Predicate::ObjectSafe(trait_def_id) => {
@@ -900,6 +982,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
900982
{
901983
debug!("evaluate_stack({:?}) --> recursive",
902984
stack.fresh_trait_ref);
985+
903986
let cycle = stack.iter().skip(1).take(rec_index + 1);
904987
let cycle = cycle.map(|stack| ty::Predicate::Trait(stack.obligation.predicate));
905988
if self.coinductive_match(cycle) {

src/test/ui/issue-54302-cases.rs

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
trait Mirror {
12+
type Image;
13+
fn coerce(self) -> Self::Image;
14+
}
15+
16+
impl<T> Mirror for T {
17+
type Image = T;
18+
fn coerce(self) -> Self { self }
19+
}
20+
21+
trait Foo<'x, T> {
22+
fn foo(self) -> &'x T;
23+
}
24+
25+
impl<'s, 'x, T: 'x> Foo<'x, T> for &'s T where &'s T: Foo2<'x, T> {
26+
fn foo(self) -> &'x T { self.foo2() }
27+
}
28+
29+
trait Foo2<'x, T> {
30+
fn foo2(self) -> &'x T;
31+
}
32+
33+
// example 1 - fails leak check
34+
impl<'x> Foo2<'x, u32> for &'x u32
35+
{
36+
fn foo2(self) -> &'x u32 { self }
37+
}
38+
39+
// example 2 - OK with this issue
40+
impl<'x, 'a: 'x> Foo2<'x, i32> for &'a i32
41+
{
42+
fn foo2(self) -> &'x i32 { self }
43+
}
44+
45+
// example 3 - fails due to issue #XYZ + Leak-check
46+
impl<'x, T> Foo2<'x, u64> for T
47+
where T: Mirror<Image=&'x u64>
48+
{
49+
fn foo2(self) -> &'x u64 { self.coerce() }
50+
}
51+
52+
// example 4 - fails due to issue #XYZ
53+
impl<'x, 'a: 'x, T> Foo2<'x, i64> for T
54+
where T: Mirror<Image=&'a i64>
55+
{
56+
fn foo2(self) -> &'x i64 { self.coerce() }
57+
}
58+
59+
60+
trait RefFoo<T> {
61+
fn ref_foo(&self) -> &'static T;
62+
}
63+
64+
impl<T> RefFoo<T> for T where for<'a> &'a T: Foo<'static, T> {
65+
fn ref_foo(&self) -> &'static T {
66+
self.foo()
67+
}
68+
}
69+
70+
71+
fn coerce_lifetime1(a: &u32) -> &'static u32
72+
{
73+
<u32 as RefFoo<u32>>::ref_foo(a)
74+
//~^ ERROR the trait bound `for<'a> &'a u32: Foo2<'_, u32>` is not satisfied
75+
}
76+
77+
fn coerce_lifetime2(a: &i32) -> &'static i32
78+
{
79+
<i32 as RefFoo<i32>>::ref_foo(a)
80+
//~^ ERROR the requirement `for<'a> 'a : ` is not satisfied
81+
}
82+
83+
fn coerce_lifetime3(a: &u64) -> &'static u64
84+
{
85+
<u64 as RefFoo<u64>>::ref_foo(a)
86+
//~^ ERROR type mismatch resolving `for<'a> <&'a u64 as Mirror>::Image == &u64`
87+
}
88+
89+
fn coerce_lifetime4(a: &i64) -> &'static i64
90+
{
91+
<i64 as RefFoo<i64>>::ref_foo(a)
92+
//~^ ERROR type mismatch resolving `for<'a> <&'a i64 as Mirror>::Image == &i64`
93+
}
94+
95+
fn main() {}

src/test/ui/issue-54302-cases.stderr

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
error[E0277]: the trait bound `for<'a> &'a u32: Foo2<'_, u32>` is not satisfied
2+
--> $DIR/issue-54302-cases.rs:73:5
3+
|
4+
LL | <u32 as RefFoo<u32>>::ref_foo(a)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'a> Foo2<'_, u32>` is not implemented for `&'a u32`
6+
|
7+
= help: the following implementations were found:
8+
<&'x u32 as Foo2<'x, u32>>
9+
= note: required because of the requirements on the impl of `for<'a> Foo<'static, u32>` for `&'a u32`
10+
= note: required because of the requirements on the impl of `RefFoo<u32>` for `u32`
11+
note: required by `RefFoo::ref_foo`
12+
--> $DIR/issue-54302-cases.rs:61:5
13+
|
14+
LL | fn ref_foo(&self) -> &'static T;
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
17+
error[E0279]: the requirement `for<'a> 'a : ` is not satisfied (`expected bound lifetime parameter 'a, found concrete lifetime`)
18+
--> $DIR/issue-54302-cases.rs:79:5
19+
|
20+
LL | <i32 as RefFoo<i32>>::ref_foo(a)
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
|
23+
= note: required because of the requirements on the impl of `for<'a> Foo2<'_, i32>` for `&'a i32`
24+
= note: required because of the requirements on the impl of `for<'a> Foo<'static, i32>` for `&'a i32`
25+
= note: required because of the requirements on the impl of `RefFoo<i32>` for `i32`
26+
note: required by `RefFoo::ref_foo`
27+
--> $DIR/issue-54302-cases.rs:61:5
28+
|
29+
LL | fn ref_foo(&self) -> &'static T;
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
32+
error[E0271]: type mismatch resolving `for<'a> <&'a u64 as Mirror>::Image == &u64`
33+
--> $DIR/issue-54302-cases.rs:85:5
34+
|
35+
LL | <u64 as RefFoo<u64>>::ref_foo(a)
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected bound lifetime parameter 'a, found concrete lifetime
37+
|
38+
= note: required because of the requirements on the impl of `for<'a> Foo2<'_, u64>` for `&'a u64`
39+
= note: required because of the requirements on the impl of `for<'a> Foo<'static, u64>` for `&'a u64`
40+
= note: required because of the requirements on the impl of `RefFoo<u64>` for `u64`
41+
note: required by `RefFoo::ref_foo`
42+
--> $DIR/issue-54302-cases.rs:61:5
43+
|
44+
LL | fn ref_foo(&self) -> &'static T;
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
47+
error[E0271]: type mismatch resolving `for<'a> <&'a i64 as Mirror>::Image == &i64`
48+
--> $DIR/issue-54302-cases.rs:91:5
49+
|
50+
LL | <i64 as RefFoo<i64>>::ref_foo(a)
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected bound lifetime parameter 'a, found concrete lifetime
52+
|
53+
= note: required because of the requirements on the impl of `for<'a> Foo2<'_, i64>` for `&'a i64`
54+
= note: required because of the requirements on the impl of `for<'a> Foo<'static, i64>` for `&'a i64`
55+
= note: required because of the requirements on the impl of `RefFoo<i64>` for `i64`
56+
note: required by `RefFoo::ref_foo`
57+
--> $DIR/issue-54302-cases.rs:61:5
58+
|
59+
LL | fn ref_foo(&self) -> &'static T;
60+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
61+
62+
error: aborting due to 4 previous errors
63+
64+
Some errors occurred: E0271, E0277, E0279.
65+
For more information about an error, try `rustc --explain E0271`.

src/test/ui/issue-54302.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
trait Deserialize<'de> {}
12+
13+
trait DeserializeOwned: for<'de> Deserialize<'de> {}
14+
impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
15+
16+
// Based on this impl, `&'static str` only implements Deserialize<'static>.
17+
// It does not implement for<'de> Deserialize<'de>.
18+
impl<'de: 'a, 'a> Deserialize<'de> for &'a str {}
19+
20+
fn main() {
21+
// Then why does it implement DeserializeOwned? This compiles.
22+
fn assert_deserialize_owned<T: DeserializeOwned>() {}
23+
assert_deserialize_owned::<&'static str>();
24+
//~^ ERROR the requirement `for<'de> 'de : ` is not satisfied
25+
26+
// It correctly does not implement for<'de> Deserialize<'de>.
27+
//fn assert_hrtb<T: for<'de> Deserialize<'de>>() {}
28+
//assert_hrtb::<&'static str>();
29+
}

src/test/ui/issue-54302.stderr

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0279]: the requirement `for<'de> 'de : ` is not satisfied (`expected bound lifetime parameter 'de, found concrete lifetime`)
2+
--> $DIR/issue-54302.rs:23:5
3+
|
4+
LL | assert_deserialize_owned::<&'static str>();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: required because of the requirements on the impl of `for<'de> Deserialize<'de>` for `&'static str`
8+
= note: required because of the requirements on the impl of `DeserializeOwned` for `&'static str`
9+
note: required by `main::assert_deserialize_owned`
10+
--> $DIR/issue-54302.rs:22:5
11+
|
12+
LL | fn assert_deserialize_owned<T: DeserializeOwned>() {}
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0279`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// run-pass
12+
13+
trait Foo<'a> {
14+
fn xyz(self);
15+
}
16+
impl<'a, T> Foo<'a> for T where 'static: 'a {
17+
fn xyz(self) {}
18+
}
19+
20+
trait Bar {
21+
fn uvw(self);
22+
}
23+
impl<T> Bar for T where for<'a> T: Foo<'a> {
24+
fn uvw(self) { self.xyz(); }
25+
}
26+
27+
fn foo<T>(t: T) where T: Bar {
28+
t.uvw();
29+
}
30+
31+
fn main() {
32+
foo(0);
33+
}

0 commit comments

Comments
 (0)