Skip to content

Commit efc508b

Browse files
committed
Auto merge of #51096 - matthewjasper:reverse-normalization-bounds, r=nikomatsakis
Register outlives predicates from queries the right way around. Closes #49354 The region constraints from queries need to be reversed from sub to outlives. Note: wf checking reports these errors before NLL, so I'm not sure if there's any case when these predicates need to be created at all. cc @nikomatsakis
2 parents 1ffb321 + b83daea commit efc508b

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

src/librustc_traits/util.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,19 @@ where
7474
let mut outlives: Vec<_> = constraints
7575
.into_iter()
7676
.map(|(k, _)| match *k {
77+
// Swap regions because we are going from sub (<=) to outlives
78+
// (>=).
7779
Constraint::VarSubVar(v1, v2) => ty::OutlivesPredicate(
78-
tcx.mk_region(ty::ReVar(v1)).into(),
79-
tcx.mk_region(ty::ReVar(v2)),
80+
tcx.mk_region(ty::ReVar(v2)).into(),
81+
tcx.mk_region(ty::ReVar(v1)),
8082
),
8183
Constraint::VarSubReg(v1, r2) => {
82-
ty::OutlivesPredicate(tcx.mk_region(ty::ReVar(v1)).into(), r2)
84+
ty::OutlivesPredicate(r2.into(), tcx.mk_region(ty::ReVar(v1)))
8385
}
8486
Constraint::RegSubVar(r1, v2) => {
85-
ty::OutlivesPredicate(r1.into(), tcx.mk_region(ty::ReVar(v2)))
87+
ty::OutlivesPredicate(tcx.mk_region(ty::ReVar(v2)).into(), r1)
8688
}
87-
Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r1.into(), r2),
89+
Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r2.into(), r1),
8890
})
8991
.map(ty::Binder::dummy) // no bound regions in the code above
9092
.collect();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
// Check that we error when a bound from the impl is not satisfied when
12+
// normalizing an associated type.
13+
14+
#![feature(nll)]
15+
trait Visitor<'d> {
16+
type Value;
17+
}
18+
19+
impl<'a, 'd: 'a> Visitor<'d> for &'a () {
20+
type Value = ();
21+
}
22+
23+
fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
24+
//~^ ERROR
25+
26+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'d` due to conflicting requirements
2+
--> $DIR/normalization-bounds-error.rs:23:1
3+
|
4+
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: first, the lifetime cannot outlive the lifetime 'd as defined on the function body at 23:1...
8+
--> $DIR/normalization-bounds-error.rs:23:1
9+
|
10+
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
note: ...but the lifetime must also be valid for the lifetime 'a as defined on the function body at 23:1...
13+
--> $DIR/normalization-bounds-error.rs:23:1
14+
|
15+
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
= note: ...so that the types are compatible:
18+
expected Visitor<'d>
19+
found Visitor<'_>
20+
21+
error: aborting due to previous error
22+
23+
For more information about this error, try `rustc --explain E0495`.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
// Check that lifetime bounds get checked the right way around with NLL enabled.
12+
13+
//run-pass
14+
15+
#![feature(nll)]
16+
trait Visitor<'d> {
17+
type Value;
18+
}
19+
20+
impl<'a, 'd: 'a> Visitor<'d> for &'a () {
21+
type Value = ();
22+
}
23+
24+
fn visit_seq<'d: 'a, 'a>() -> <&'a () as Visitor<'d>>::Value {}
25+
26+
fn main() {}

0 commit comments

Comments
 (0)