Skip to content

Commit 4910fe6

Browse files
committed
Fix incorrect usage of EvaluatedToOk when evaluating TypeOutlives
A global predicate is not guarnatenteed to outlive all regions. If the predicate involves late-bound regions, then it may fail to outlive other regions (e.g. `for<'b> &'b bool: 'static` does not hold) We now only produce `EvaluatedToOk` when a global predicate has no late-bound regions - in that case, the ony region that can be present in the type is 'static
1 parent e6d2de9 commit 4910fe6

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

compiler/rustc_trait_selection/src/traits/select/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
558558
},
559559

560560
ty::PredicateKind::TypeOutlives(pred) => {
561-
if pred.0.is_known_global() {
561+
// A global type with no late-bound regions can only
562+
// contain the "'static" lifetime (any other lifetime
563+
// would either be late-bound or local), so it is guaranteed
564+
// to outlive any other lifetime
565+
if pred.0.is_global(self.infcx.tcx) && !pred.0.has_late_bound_regions() {
562566
Ok(EvaluatedToOk)
563567
} else {
564568
Ok(EvaluatedToOkModuloRegions)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// revisions: with_clause without_clause
2+
// Tests that `EvaluatedToOkModuloRegions` from a projection sub-obligation
3+
// is correctly propagated
4+
5+
#![feature(rustc_attrs)]
6+
7+
trait MyTrait {
8+
type Assoc;
9+
}
10+
11+
struct MyStruct;
12+
13+
impl MyTrait for MyStruct {
14+
// Evaluating this projection will result in `EvaluatedToOkModuloRegions`
15+
// (when `with_clause` is enabled)
16+
type Assoc = <Bar as MyTrait>::Assoc;
17+
}
18+
19+
struct Bar;
20+
21+
// The `where` clause on this impl will cause us to produce `EvaluatedToOkModuloRegions`
22+
// when evaluating a projection involving this impl
23+
#[cfg(with_clause)]
24+
impl MyTrait for Bar where for<'b> &'b (): 'b {
25+
type Assoc = bool;
26+
}
27+
28+
// This impl tests that the `EvaluatedToOkModuoRegions` result that we get
29+
// is really due to the `where` clause on the `with_clause` impl
30+
#[cfg(without_clause)]
31+
impl MyTrait for Bar {
32+
type Assoc = bool;
33+
}
34+
35+
// The implementation of `#[rustc_evaluate_where_clauses]` doesn't perform
36+
// normalization, so we need to place the projection predicate behind a normal
37+
// trait predicate
38+
struct Helper {}
39+
trait HelperTrait {}
40+
impl HelperTrait for Helper where <MyStruct as MyTrait>::Assoc: Sized {}
41+
42+
// Evaluating this 'where' clause will (recursively) end up evaluating
43+
// `for<'b> &'b (): 'b`, which will produce `EvaluatedToOkModuloRegions`
44+
#[rustc_evaluate_where_clauses]
45+
fn test(val: MyStruct) where Helper: HelperTrait {
46+
panic!()
47+
}
48+
49+
fn foo(val: MyStruct) {
50+
test(val);
51+
//[with_clause]~^ ERROR evaluate(Binder(TraitPredicate(<Helper as HelperTrait>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions)
52+
//[without_clause]~^^ ERROR evaluate(Binder(TraitPredicate(<Helper as HelperTrait>, polarity:Positive), [])) = Ok(EvaluatedToOk)
53+
}
54+
55+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: evaluate(Binder(TraitPredicate(<Helper as HelperTrait>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions)
2+
--> $DIR/project-modulo-regions.rs:50:5
3+
|
4+
LL | fn test(val: MyStruct) where Helper: HelperTrait {
5+
| ----------- predicate
6+
...
7+
LL | test(val);
8+
| ^^^^
9+
10+
error: aborting due to previous error
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: evaluate(Binder(TraitPredicate(<Helper as HelperTrait>, polarity:Positive), [])) = Ok(EvaluatedToOk)
2+
--> $DIR/project-modulo-regions.rs:50:5
3+
|
4+
LL | fn test(val: MyStruct) where Helper: HelperTrait {
5+
| ----------- predicate
6+
...
7+
LL | test(val);
8+
| ^^^^
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)