Skip to content

Commit fc59f3e

Browse files
committed
Improve SubSupConflict case with one named, one anonymous lifetime parameter #42701
1 parent 7f6417e commit fc59f3e

23 files changed

+76
-115
lines changed

src/librustc/infer/error_reporting/different_lifetimes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
6060
pub fn try_report_anon_anon_conflict(&self, error: &RegionResolutionError<'tcx>) -> bool {
6161
let (span, sub, sup) = match *error {
6262
ConcreteFailure(ref origin, sub, sup) => (origin.span(), sub, sup),
63+
SubSupConflict(_, ref origin, sub, _, sup) => (origin.span(), sub, sup),
6364
_ => return false, // inapplicable
6465
};
6566

src/librustc/infer/error_reporting/named_anon_conflict.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
2121
pub fn try_report_named_anon_conflict(&self, error: &RegionResolutionError<'tcx>) -> bool {
2222
let (span, sub, sup) = match *error {
2323
ConcreteFailure(ref origin, sub, sup) => (origin.span(), sub, sup),
24+
SubSupConflict(_, ref origin, sub, _, sup) => (origin.span(), sub, sup),
2425
_ => return false, // inapplicable
2526
};
2627

src/test/compile-fail/associated-types-project-from-hrtb-in-fn-body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn bar<'a, 'b, I : for<'x> Foo<&'x isize>>(
3030
{
3131
// x and y here have two distinct lifetimes:
3232
let z: I::A = if cond { x } else { y };
33-
//~^ ERROR cannot infer
33+
//~^ ERROR lifetime mismatch
3434
}
3535

3636
pub fn main() {}

src/test/compile-fail/associated-types/cache/project-fn-ret-contravariant.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ fn baz<'a,'b>(x: &'a u32) -> &'static u32 {
5050

5151
#[cfg(krisskross)] // two instantiations, mixing and matching: BAD
5252
fn transmute<'a,'b>(x: &'a u32, y: &'b u32) -> (&'a u32, &'b u32) {
53-
let a = bar(foo, y); //[krisskross]~ ERROR E0495
54-
let b = bar(foo, x); //[krisskross]~ ERROR E0495
55-
(a, b)
53+
let a = bar(foo, y);
54+
let b = bar(foo, x);
55+
(a, b) //[krisskross]~ ERROR 55:5: 55:6: lifetime mismatch [E0623]
56+
//[krisskross]~^ ERROR 55:8: 55:9: lifetime mismatch [E0623]
5657
}
5758

5859
#[rustc_error]

src/test/compile-fail/associated-types/cache/project-fn-ret-invariant.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ fn baz<'a,'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) {
4545
#[cfg(oneuse)] // one instantiation: BAD
4646
fn baz<'a,'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) {
4747
let f = foo; // <-- No consistent type can be inferred for `f` here.
48-
let a = bar(f, x); //[oneuse]~^ ERROR E0495
48+
let a = bar(f, x);
4949
let b = bar(f, y);
50-
(a, b)
50+
(a, b) //[oneuse]~ ERROR E0623
5151
}
5252

5353
#[cfg(transmute)] // one instantiations: BAD
@@ -60,9 +60,10 @@ fn baz<'a,'b>(x: Type<'a>) -> Type<'static> {
6060

6161
#[cfg(krisskross)] // two instantiations, mixing and matching: BAD
6262
fn transmute<'a,'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) {
63-
let a = bar(foo, y); //[krisskross]~ ERROR E0495
64-
let b = bar(foo, x); //[krisskross]~ ERROR E0495
65-
(a, b)
63+
let a = bar(foo, y);
64+
let b = bar(foo, x);
65+
(a, b) //[krisskross]~ ERROR E0623
66+
//[krisskross]~^ ERROR E0623
6667
}
6768

6869
#[rustc_error]

src/test/compile-fail/borrowck/borrowck-reborrow-from-shorter-lived-andmut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct S<'a> {
1717

1818
fn copy_borrowed_ptr<'a,'b>(p: &'a mut S<'b>) -> S<'b> {
1919
S { pointer: &mut *p.pointer }
20-
//~^ ERROR cannot infer
20+
//~^ ERROR lifetime mismatch
2121
}
2222

2323
fn main() {

src/test/compile-fail/issue-17728.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ trait TraversesWorld {
2121
fn attemptTraverse(&self, room: &Room, directionStr: &str) -> Result<&Room, &str> {
2222
let direction = str_to_direction(directionStr);
2323
let maybe_room = room.direction_to_room.get(&direction);
24-
//~^ ERROR cannot infer an appropriate lifetime for autoref due to conflicting requirements
2524
match maybe_room {
2625
Some(entry) => Ok(entry),
26+
//~^ ERROR 25:28: 25:37: lifetime mismatch [E0623]
2727
_ => Err("Direction does not exist in room.")
2828
}
2929
}

src/test/compile-fail/issue-40288-2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ fn prove_static<T: 'static + ?Sized>(_: &'static T) {}
1212

1313
fn lifetime_transmute_slice<'a, T: ?Sized>(x: &'a T, y: &T) -> &'a T {
1414
let mut out = [x];
15-
//~^ ERROR cannot infer an appropriate lifetime due to conflicting requirements
1615
{
1716
let slice: &mut [_] = &mut out;
1817
slice[0] = y;
1918
}
2019
out[0]
20+
//~^ ERROR 19:5: 19:11: explicit lifetime required in the type of `y` [E0621]
2121
}
2222

2323
struct Struct<T, U: ?Sized> {
@@ -27,12 +27,12 @@ struct Struct<T, U: ?Sized> {
2727

2828
fn lifetime_transmute_struct<'a, T: ?Sized>(x: &'a T, y: &T) -> &'a T {
2929
let mut out = Struct { head: x, _tail: [()] };
30-
//~^ ERROR cannot infer an appropriate lifetime due to conflicting requirements
3130
{
3231
let dst: &mut Struct<_, [()]> = &mut out;
3332
dst.head = y;
3433
}
3534
out.head
35+
//~^ ERROR 34:5: 34:13: explicit lifetime required in the type of `y` [E0621]
3636
}
3737

3838
fn main() {

src/test/compile-fail/object-lifetime-default-from-box-error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn store(ss: &mut SomeStruct, b: Box<SomeTrait>) {
3838
fn store1<'b>(ss: &mut SomeStruct, b: Box<SomeTrait+'b>) {
3939
// Here we override the lifetimes explicitly, and so naturally we get an error.
4040

41-
ss.r = b; //~ ERROR cannot infer an appropriate lifetime
41+
ss.r = b; //~ ERROR 41:12: 41:13: explicit lifetime required in the type of `ss` [E0621]
4242
}
4343

4444
fn main() {

src/test/compile-fail/region-lifetime-bounds-on-fns-where-clause.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn b<'a, 'b>(x: &mut &'a isize, y: &mut &'b isize) {
2121
fn c<'a,'b>(x: &mut &'a isize, y: &mut &'b isize) {
2222
// Here we try to call `foo` but do not know that `'a` and `'b` are
2323
// related as required.
24-
a(x, y); //~ ERROR cannot infer
24+
a(x, y); //~ ERROR 24:7: 24:8: lifetime mismatch [E0623]
2525
}
2626

2727
fn d() {

src/test/compile-fail/region-multiple-lifetime-bounds-on-fns-where-clause.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn b<'a, 'b, 'c>(x: &mut &'a isize, y: &mut &'b isize, z: &mut &'c isize) {
2323
fn c<'a,'b, 'c>(x: &mut &'a isize, y: &mut &'b isize, z: &mut &'c isize) {
2424
// Here we try to call `foo` but do not know that `'a` and `'b` are
2525
// related as required.
26-
a(x, y, z); //~ ERROR cannot infer
26+
a(x, y, z); //~ ERROR 26:7: 26:8: lifetime mismatch [E0623]
2727
}
2828

2929
fn d() {

src/test/compile-fail/regions-bounded-method-type-parameters-cross-crate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn call_into_maybe_owned<'x,F:IntoMaybeOwned<'x>>(f: F) {
2727

2828
fn call_bigger_region<'x, 'y>(a: Inv<'x>, b: Inv<'y>) {
2929
// Here the value provided for 'y is 'y, and hence 'y:'x does not hold.
30-
a.bigger_region(b) //~ ERROR cannot infer
30+
a.bigger_region(b) //~ ERROR 30:7: 30:20: lifetime mismatch [E0623]
3131
}
3232

3333
fn main() { }

src/test/compile-fail/regions-bounded-method-type-parameters-trait-bound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn caller1<'a,'b,F:Foo<'a>>(a: Inv<'a>, b: Inv<'b>, f: F) {
2727

2828
fn caller2<'a,'b,F:Foo<'a>>(a: Inv<'a>, b: Inv<'b>, f: F) {
2929
// Here the value provided for 'y is 'b, and hence 'b:'a does not hold.
30-
f.method(b); //~ ERROR cannot infer
30+
f.method(b); //~ ERROR 30:7: 30:13: lifetime mismatch [E0623]
3131
}
3232

3333
fn caller3<'a,'b:'a,F:Foo<'a>>(a: Inv<'a>, b: Inv<'b>, f: F) {

src/test/compile-fail/regions-creating-enums3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum ast<'a> {
1414
}
1515

1616
fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
17-
ast::add(x, y) //~ ERROR cannot infer
17+
ast::add(x, y) //~ ERROR 17:5: 17:19: lifetime mismatch [E0623]
1818
}
1919

2020
fn main() {

src/test/compile-fail/regions-free-region-ordering-callee.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ fn ordering1<'a, 'b>(x: &'a &'b usize) -> &'a usize {
2020

2121
fn ordering2<'a, 'b>(x: &'a &'b usize, y: &'a usize) -> &'b usize {
2222
// However, it is not safe to assume that 'b <= 'a
23-
&*y //~ ERROR cannot infer
23+
&*y //~ ERROR 23:5: 23:8: lifetime mismatch [E0623]
2424
}
2525

2626
fn ordering3<'a, 'b>(x: &'a usize, y: &'b usize) -> &'a &'b usize {
2727
// Do not infer an ordering from the return value.
2828
let z: &'b usize = &*x;
29-
//~^ ERROR cannot infer
29+
//~^ ERROR 28:24: 28:27: lifetime mismatch [E0623]
3030
panic!();
3131
}
3232

src/test/compile-fail/regions-lifetime-bounds-on-fns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn b<'a, 'b>(x: &mut &'a isize, y: &mut &'b isize) {
2121
fn c<'a,'b>(x: &mut &'a isize, y: &mut &'b isize) {
2222
// Here we try to call `foo` but do not know that `'a` and `'b` are
2323
// related as required.
24-
a(x, y); //~ ERROR E0495
24+
a(x, y); //~ ERROR 24:7: 24:8: lifetime mismatch [E0623]
2525
}
2626

2727
fn d() {

src/test/compile-fail/regions-reborrow-from-shorter-mut-ref-mut-ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Issue #8624. Test for reborrowing with 3 levels, not just two.
1212

1313
fn copy_borrowed_ptr<'a, 'b, 'c>(p: &'a mut &'b mut &'c mut isize) -> &'b mut isize {
14-
&mut ***p //~ ERROR cannot infer
14+
&mut ***p //~ ERROR 14:5: 14:14: lifetime mismatch [E0623]
1515
}
1616

1717
fn main() {

src/test/compile-fail/regions-reborrow-from-shorter-mut-ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// for `'a` (which must be a sublifetime of `'b`).
1414

1515
fn copy_borrowed_ptr<'a, 'b>(p: &'a mut &'b mut isize) -> &'b mut isize {
16-
&mut **p //~ ERROR cannot infer
16+
&mut **p //~ ERROR 16:5: 16:13: lifetime mismatch [E0623]
1717
}
1818

1919
fn main() {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2017 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+
struct Foo {
12+
field: i32,
13+
}
14+
15+
fn foo2<'a>(a: &'a Foo, x: &i32) -> &'a i32 {
16+
if true {
17+
let p: &i32 = &a.field;
18+
&*p
19+
} else {
20+
&*x
21+
}
22+
}
23+
24+
fn main() { }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0621]: explicit lifetime required in the type of `x`
2+
--> $DIR/42701_one_named_and_one_anonymous.rs:20:9
3+
|
4+
15 | fn foo2<'a>(a: &'a Foo, x: &i32) -> &'a i32 {
5+
| - consider changing the type of `x` to `&'a i32`
6+
...
7+
20 | &*x
8+
| ^^^ lifetime `'a` required
9+
10+
error: aborting due to previous error
11+
Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
1-
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
2-
--> $DIR/ex2c-push-inference-variable.rs:16:13
3-
|
4-
16 | let z = Ref { data: y.data };
5-
| ^^^
6-
|
7-
note: first, the lifetime cannot outlive the lifetime 'c as defined on the function body at 15:1...
8-
--> $DIR/ex2c-push-inference-variable.rs:15:1
9-
|
10-
15 | / fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
11-
16 | | let z = Ref { data: y.data };
12-
17 | | x.push(z);
13-
18 | | }
14-
| |_^
15-
note: ...so that reference does not outlive borrowed content
16-
--> $DIR/ex2c-push-inference-variable.rs:16:25
17-
|
18-
16 | let z = Ref { data: y.data };
19-
| ^^^^^^
20-
note: but, the lifetime must be valid for the lifetime 'b as defined on the function body at 15:1...
21-
--> $DIR/ex2c-push-inference-variable.rs:15:1
22-
|
23-
15 | / fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
24-
16 | | let z = Ref { data: y.data };
25-
17 | | x.push(z);
26-
18 | | }
27-
| |_^
28-
note: ...so that expression is assignable (expected Ref<'b, _>, found Ref<'_, _>)
1+
error[E0623]: lifetime mismatch
292
--> $DIR/ex2c-push-inference-variable.rs:17:12
303
|
4+
15 | fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
5+
| ------------ ------------ these two types are declared with different lifetimes...
6+
16 | let z = Ref { data: y.data };
317
17 | x.push(z);
32-
| ^
8+
| ^ ...but data from `y` flows into `x` here
339

3410
error: aborting due to previous error
3511

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
1-
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
2-
--> $DIR/ex2d-push-inference-variable-2.rs:17:13
3-
|
4-
17 | let b = Ref { data: y.data };
5-
| ^^^
6-
|
7-
note: first, the lifetime cannot outlive the lifetime 'c as defined on the function body at 15:1...
8-
--> $DIR/ex2d-push-inference-variable-2.rs:15:1
9-
|
10-
15 | / fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
11-
16 | | let a: &mut Vec<Ref<i32>> = x;
12-
17 | | let b = Ref { data: y.data };
13-
18 | | a.push(b);
14-
19 | | }
15-
| |_^
16-
note: ...so that reference does not outlive borrowed content
17-
--> $DIR/ex2d-push-inference-variable-2.rs:17:25
18-
|
19-
17 | let b = Ref { data: y.data };
20-
| ^^^^^^
21-
note: but, the lifetime must be valid for the lifetime 'b as defined on the function body at 15:1...
22-
--> $DIR/ex2d-push-inference-variable-2.rs:15:1
23-
|
24-
15 | / fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
25-
16 | | let a: &mut Vec<Ref<i32>> = x;
26-
17 | | let b = Ref { data: y.data };
27-
18 | | a.push(b);
28-
19 | | }
29-
| |_^
30-
note: ...so that expression is assignable (expected &mut std::vec::Vec<Ref<'_, i32>>, found &mut std::vec::Vec<Ref<'b, i32>>)
1+
error[E0623]: lifetime mismatch
312
--> $DIR/ex2d-push-inference-variable-2.rs:16:33
323
|
4+
15 | fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
5+
| ------------ ------------ these two types are declared with different lifetimes...
336
16 | let a: &mut Vec<Ref<i32>> = x;
34-
| ^
7+
| ^ ...but data from `y` flows into `x` here
358

369
error: aborting due to previous error
3710

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
1-
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
2-
--> $DIR/ex2e-push-inference-variable-3.rs:17:13
3-
|
4-
17 | let b = Ref { data: y.data };
5-
| ^^^
6-
|
7-
note: first, the lifetime cannot outlive the lifetime 'c as defined on the function body at 15:1...
8-
--> $DIR/ex2e-push-inference-variable-3.rs:15:1
9-
|
10-
15 | / fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
11-
16 | | let a: &mut Vec<Ref<i32>> = x;
12-
17 | | let b = Ref { data: y.data };
13-
18 | | Vec::push(a, b);
14-
19 | | }
15-
| |_^
16-
note: ...so that reference does not outlive borrowed content
17-
--> $DIR/ex2e-push-inference-variable-3.rs:17:25
18-
|
19-
17 | let b = Ref { data: y.data };
20-
| ^^^^^^
21-
note: but, the lifetime must be valid for the lifetime 'b as defined on the function body at 15:1...
22-
--> $DIR/ex2e-push-inference-variable-3.rs:15:1
23-
|
24-
15 | / fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
25-
16 | | let a: &mut Vec<Ref<i32>> = x;
26-
17 | | let b = Ref { data: y.data };
27-
18 | | Vec::push(a, b);
28-
19 | | }
29-
| |_^
30-
note: ...so that expression is assignable (expected &mut std::vec::Vec<Ref<'_, i32>>, found &mut std::vec::Vec<Ref<'b, i32>>)
1+
error[E0623]: lifetime mismatch
312
--> $DIR/ex2e-push-inference-variable-3.rs:16:33
323
|
4+
15 | fn foo<'a, 'b, 'c>(x: &'a mut Vec<Ref<'b, i32>>, y: Ref<'c, i32>) {
5+
| ------------ ------------ these two types are declared with different lifetimes...
336
16 | let a: &mut Vec<Ref<i32>> = x;
34-
| ^
7+
| ^ ...but data from `y` flows into `x` here
358

369
error: aborting due to previous error
3710

0 commit comments

Comments
 (0)