Skip to content

Commit 53a4b39

Browse files
committedOct 1, 2018
handle outlives predicates in trait evaluation
Fixes rust-lang#54302.
1 parent de3d640 commit 53a4b39

File tree

6 files changed

+278
-4
lines changed

6 files changed

+278
-4
lines changed
 

‎src/librustc/traits/fulfill.rs

+1
Original file line numberDiff line numberDiff line change
@@ -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

+71-4
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,76 @@ 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.is_late_bound() || r_b.is_late_bound() {
731+
// There is no current way to prove `for<'a> 'a: 'x`
732+
// unless `'a = 'x`, because there are no bounds involving
733+
// lifetimes.
734+
735+
// It is possible to solve `for<'a> 'x: 'a` where `'x`
736+
// is a free region by forcing `'x = 'static`. However,
737+
// fulfillment does not *quite* do this ATM (it calls
738+
// `region_outlives_predicate`, which is OK if `'x` is
739+
// literally ReStatic, but is *not* OK if `'x` is any
740+
// sort of inference variable, even if it *is* equal
741+
// to `'static`).
742+
743+
// If we ever want to handle that sort of obligations,
744+
// we need to make sure we are not confused by
745+
// technically-allowed-by-RFC-447-but-probably-should-not-be
746+
// impls such as
747+
// ```Rust
748+
// impl<'a, 's, T> X<'s> for T where T: Debug + 's, 'a: 's
749+
// ```
750+
751+
// We don't want to allow this sort of reasoning in intercrate
752+
// mode, for backwards-compatibility reasons.
753+
if self.intercrate.is_some() {
754+
Ok(EvaluatedToAmbig)
755+
} else {
756+
Ok(EvaluatedToErr)
757+
}
758+
} else {
759+
// Relating 2 inference variable regions. These will
760+
// always hold if our query is "staticized".
761+
Ok(EvaluatedToOk)
762+
}
697763
}
698764

699765
ty::Predicate::ObjectSafe(trait_def_id) => {
@@ -900,6 +966,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
900966
{
901967
debug!("evaluate_stack({:?}) --> recursive",
902968
stack.fresh_trait_ref);
969+
903970
let cycle = stack.iter().skip(1).take(rec_index + 1);
904971
let cycle = cycle.map(|stack| ty::Predicate::Trait(stack.obligation.predicate));
905972
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`.

0 commit comments

Comments
 (0)
Please sign in to comment.