Skip to content

Commit f6d43ed

Browse files
committed
Auto merge of #53124 - davidtwco:issue-52742, r=nikomatsakis
region error messages involving impls are confusing Part of #52742. r? @nikomatsakis
2 parents db1acaa + 31657c9 commit f6d43ed

14 files changed

+95
-58
lines changed

src/librustc/infer/error_reporting/nice_region_error/named_anon_conflict.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
129129
ty::BrNamed(..) => true,
130130
_ => false,
131131
},
132-
ty::ReEarlyBound(_) => true,
132+
ty::ReEarlyBound(ebr) => ebr.has_name(),
133133
_ => false,
134134
}
135135
}

src/librustc/ty/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use std::{mem, ptr};
5252
use syntax::ast::{self, DUMMY_NODE_ID, Name, Ident, NodeId};
5353
use syntax::attr;
5454
use syntax::ext::hygiene::Mark;
55-
use syntax::symbol::{Symbol, LocalInternedString, InternedString};
55+
use syntax::symbol::{keywords, Symbol, LocalInternedString, InternedString};
5656
use syntax_pos::{DUMMY_SP, Span};
5757

5858
use rustc_data_structures::accumulate_vec::IntoIter as AccIntoIter;
@@ -825,6 +825,12 @@ impl ty::EarlyBoundRegion {
825825
pub fn to_bound_region(&self) -> ty::BoundRegion {
826826
ty::BoundRegion::BrNamed(self.def_id, self.name)
827827
}
828+
829+
/// Does this early bound region have a name? Early bound regions normally
830+
/// always have names except when using anonymous lifetimes (`'_`).
831+
pub fn has_name(&self) -> bool {
832+
self.name != keywords::UnderscoreLifetime.name().as_interned_str()
833+
}
828834
}
829835

830836
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs

+15-27
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
9595
debug!("give_region_a_name: error_region = {:?}", error_region);
9696
match error_region {
9797
ty::ReEarlyBound(ebr) => {
98-
self.highlight_named_span(tcx, error_region, &ebr.name, diag);
99-
Some(ebr.name)
98+
if ebr.has_name() {
99+
self.highlight_named_span(tcx, error_region, &ebr.name, diag);
100+
Some(ebr.name)
101+
} else {
102+
None
103+
}
100104
},
101105

102106
ty::ReStatic => Some(keywords::StaticLifetime.name().as_interned_str()),
@@ -238,17 +242,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
238242
return Some(region_name);
239243
}
240244

241-
let (_argument_name, argument_span) = self.get_argument_name_and_span_for_region(
242-
mir, argument_index);
243-
244-
let region_name = self.synthesize_region_name(counter);
245-
246-
diag.span_label(
247-
argument_span,
248-
format!("lifetime `{}` appears in this argument", region_name,),
249-
);
250-
251-
Some(region_name)
245+
self.give_name_if_we_cannot_match_hir_ty(
246+
infcx,
247+
mir,
248+
fr,
249+
arg_ty,
250+
counter,
251+
diag,
252+
)
252253
}
253254

254255
fn give_name_if_we_can_match_hir_ty_from_argument(
@@ -366,14 +367,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
366367

367368
search_stack.push((argument_ty, argument_hir_ty));
368369

369-
let mut closest_match: &hir::Ty = argument_hir_ty;
370-
371370
while let Some((ty, hir_ty)) = search_stack.pop() {
372-
// While we search, also track the closet match.
373-
if tcx.any_free_region_meets(&ty, |r| r.to_region_vid() == needle_fr) {
374-
closest_match = hir_ty;
375-
}
376-
377371
match (&ty.sty, &hir_ty.node) {
378372
// Check if the `argument_ty` is `&'X ..` where `'X`
379373
// is the region we are looking for -- if so, and we have a `&T`
@@ -448,13 +442,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
448442
}
449443
}
450444

451-
let region_name = self.synthesize_region_name(counter);
452-
diag.span_label(
453-
closest_match.span,
454-
format!("lifetime `{}` appears in this type", region_name),
455-
);
456-
457-
return Some(region_name);
445+
return None;
458446
}
459447

460448
/// We've found an enum/struct/union type with the substitutions

src/test/ui/lifetime-errors/ex2b-push-no-existing-names.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ error: unsatisfied lifetime constraints
88
--> $DIR/ex2b-push-no-existing-names.rs:16:5
99
|
1010
LL | fn foo(x: &mut Vec<Ref<i32>>, y: Ref<i32>) {
11-
| -------- -------- lifetime `'1` appears in this type
12-
| |
13-
| lifetime `'2` appears in this type
11+
| - - has type `Ref<'1, i32>`
12+
| |
13+
| has type `&mut std::vec::Vec<Ref<'2, i32>>`
1414
LL | x.push(y); //~ ERROR lifetime mismatch
1515
| ^^^^^^^^^ argument requires that `'1` must outlive `'2`
1616

src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-2.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ error: unsatisfied lifetime constraints
88
--> $DIR/ex3-both-anon-regions-both-are-structs-2.rs:16:5
99
|
1010
LL | fn foo(mut x: Ref, y: Ref) {
11-
| --- --- lifetime `'1` appears in this type
12-
| |
13-
| lifetime `'2` appears in this type
11+
| ----- - has type `Ref<'_, '1>`
12+
| |
13+
| has type `Ref<'_, '2>`
1414
LL | x.b = y.b; //~ ERROR lifetime mismatch
1515
| ^^^^^^^^^ requires that `'1` must outlive `'2`
1616

src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-3.nll.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ error: unsatisfied lifetime constraints
88
--> $DIR/ex3-both-anon-regions-both-are-structs-3.rs:16:5
99
|
1010
LL | fn foo(mut x: Ref) {
11-
| ---
12-
| |
13-
| lifetime `'1` appears in this type
14-
| lifetime `'2` appears in this type
11+
| -----
12+
| |
13+
| has type `Ref<'_, '1>`
14+
| has type `Ref<'2, '_>`
1515
LL | x.a = x.b; //~ ERROR lifetime mismatch
1616
| ^^^^^^^^^ requires that `'1` must outlive `'2`
1717

src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-4.nll.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ error: unsatisfied lifetime constraints
88
--> $DIR/ex3-both-anon-regions-both-are-structs-4.rs:16:5
99
|
1010
LL | fn foo(mut x: Ref) {
11-
| ---
12-
| |
13-
| lifetime `'1` appears in this type
14-
| lifetime `'2` appears in this type
11+
| -----
12+
| |
13+
| has type `Ref<'_, '1>`
14+
| has type `Ref<'2, '_>`
1515
LL | x.a = x.b; //~ ERROR lifetime mismatch
1616
| ^^^^^^^^^ requires that `'1` must outlive `'2`
1717

src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ error: unsatisfied lifetime constraints
88
--> $DIR/ex3-both-anon-regions-both-are-structs.rs:15:5
99
|
1010
LL | fn foo(mut x: Vec<Ref>, y: Ref) {
11-
| --- --- lifetime `'1` appears in this type
12-
| |
13-
| lifetime `'2` appears in this type
11+
| ----- - has type `Ref<'1>`
12+
| |
13+
| has type `std::vec::Vec<Ref<'2>>`
1414
LL | x.push(y); //~ ERROR lifetime mismatch
1515
| ^^^^^^^^^ argument requires that `'1` must outlive `'2`
1616

src/test/ui/lifetime-errors/ex3-both-anon-regions-one-is-struct-2.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ error: unsatisfied lifetime constraints
88
--> $DIR/ex3-both-anon-regions-one-is-struct-2.rs:14:5
99
|
1010
LL | fn foo(mut x: Ref, y: &u32) {
11-
| --- - let's call the lifetime of this reference `'2`
12-
| |
13-
| lifetime `'1` appears in this type
11+
| ----- - let's call the lifetime of this reference `'2`
12+
| |
13+
| has type `Ref<'_, '1>`
1414
LL | y = x.b; //~ ERROR lifetime mismatch
1515
| ^^^^^^^ requires that `'1` must outlive `'2`
1616

src/test/ui/lifetime-errors/ex3-both-anon-regions-one-is-struct-3.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ error: unsatisfied lifetime constraints
88
--> $DIR/ex3-both-anon-regions-one-is-struct-3.rs:14:5
99
|
1010
LL | fn foo(mut y: Ref, x: &u32) {
11-
| --- - let's call the lifetime of this reference `'1`
12-
| |
13-
| lifetime `'2` appears in this type
11+
| ----- - let's call the lifetime of this reference `'1`
12+
| |
13+
| has type `Ref<'_, '2>`
1414
LL | y.b = x; //~ ERROR lifetime mismatch
1515
| ^^^^^^^ requires that `'1` must outlive `'2`
1616

src/test/ui/lifetime-errors/ex3-both-anon-regions-one-is-struct-4.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ error: unsatisfied lifetime constraints
88
--> $DIR/ex3-both-anon-regions-one-is-struct-4.rs:14:5
99
|
1010
LL | fn foo(mut y: Ref, x: &u32) {
11-
| --- - let's call the lifetime of this reference `'1`
12-
| |
13-
| lifetime `'2` appears in this type
11+
| ----- - let's call the lifetime of this reference `'1`
12+
| |
13+
| has type `Ref<'_, '2>`
1414
LL | y.b = x; //~ ERROR lifetime mismatch
1515
| ^^^^^^^ requires that `'1` must outlive `'2`
1616

src/test/ui/lifetime-errors/ex3-both-anon-regions-one-is-struct.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ error: unsatisfied lifetime constraints
88
--> $DIR/ex3-both-anon-regions-one-is-struct.rs:17:5
99
|
1010
LL | fn foo(mut x: Ref, y: &u32) {
11-
| --- - let's call the lifetime of this reference `'1`
12-
| |
13-
| lifetime `'2` appears in this type
11+
| ----- - let's call the lifetime of this reference `'1`
12+
| |
13+
| has type `Ref<'_, '2>`
1414
LL | x.b = y; //~ ERROR lifetime mismatch
1515
| ^^^^^^^ requires that `'1` must outlive `'2`
1616

src/test/ui/nll/issue-52742.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2012 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+
#![feature(nll)]
12+
#![feature(in_band_lifetimes)]
13+
#![feature(impl_header_lifetime_elision)]
14+
15+
struct Foo<'a, 'b> {
16+
x: &'a u32,
17+
y: &'b u32,
18+
}
19+
20+
struct Bar<'b> {
21+
z: &'b u32
22+
}
23+
24+
impl Foo<'_, '_> {
25+
fn take_bar(&mut self, b: Bar<'_>) {
26+
self.y = b.z
27+
//~^ ERROR unsatisfied lifetime constraints
28+
}
29+
}
30+
31+
fn main() { }

src/test/ui/nll/issue-52742.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: unsatisfied lifetime constraints
2+
--> $DIR/issue-52742.rs:26:9
3+
|
4+
LL | fn take_bar(&mut self, b: Bar<'_>) {
5+
| --------- -- let's call this `'1`
6+
| |
7+
| has type `&mut Foo<'_, '2>`
8+
LL | self.y = b.z
9+
| ^^^^^^^^^^^^ requires that `'1` must outlive `'2`
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)