Skip to content

Commit c8cecf4

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 76b69a6 + 12037ff commit c8cecf4

File tree

5 files changed

+65
-29
lines changed

5 files changed

+65
-29
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use std::{mem, ptr};
5151
use syntax::ast::{self, DUMMY_NODE_ID, Name, Ident, NodeId};
5252
use syntax::attr;
5353
use syntax::ext::hygiene::Mark;
54-
use syntax::symbol::{Symbol, LocalInternedString, InternedString};
54+
use syntax::symbol::{keywords, Symbol, LocalInternedString, InternedString};
5555
use syntax_pos::{DUMMY_SP, Span};
5656

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

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

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

Lines changed: 15 additions & 27 deletions
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/nll/issue-52742.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
14+
struct Foo<'a, 'b> {
15+
x: &'a u32,
16+
y: &'b u32,
17+
}
18+
19+
struct Bar<'b> {
20+
z: &'b u32
21+
}
22+
23+
impl Foo<'_, '_> {
24+
fn take_bar(&mut self, b: Bar<'_>) {
25+
self.y = b.z
26+
//~^ ERROR unsatisfied lifetime constraints
27+
}
28+
}
29+
30+
fn main() { }

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: unsatisfied lifetime constraints
2+
--> $DIR/issue-52742.rs:25: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)