Skip to content

Commit 9eb8d11

Browse files
committed
Improve borrow errors for closures.
Adds improved messages for closures where returned type does not match the inferred return lifetime of the closure.
1 parent 22e49e2 commit 9eb8d11

File tree

13 files changed

+438
-153
lines changed

13 files changed

+438
-153
lines changed

src/librustc/util/ppaux.rs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ use hir;
3434
thread_local! {
3535
/// Mechanism for highlighting of specific regions for display in NLL region inference errors.
3636
/// Contains region to highlight and counter for number to use when highlighting.
37-
static HIGHLIGHT_REGION: Cell<Option<(RegionVid, usize)>> = Cell::new(None)
37+
static HIGHLIGHT_REGION_FOR_REGIONVID: Cell<Option<(RegionVid, usize)>> = Cell::new(None)
38+
}
39+
40+
thread_local! {
41+
/// Mechanism for highlighting of specific regions for display in NLL's 'borrow does not live
42+
/// long enough' errors. Contains a region to highlight and a counter to use.
43+
static HIGHLIGHT_REGION_FOR_REGION: Cell<Option<(ty::BoundRegion, usize)>> = Cell::new(None)
3844
}
3945

4046
macro_rules! gen_display_debug_body {
@@ -564,12 +570,34 @@ pub fn parameterized<F: fmt::Write>(f: &mut F,
564570
PrintContext::new().parameterized(f, substs, did, projections)
565571
}
566572

567-
fn get_highlight_region() -> Option<(RegionVid, usize)> {
568-
HIGHLIGHT_REGION.with(|hr| hr.get())
573+
fn get_highlight_region_for_regionvid() -> Option<(RegionVid, usize)> {
574+
HIGHLIGHT_REGION_FOR_REGIONVID.with(|hr| hr.get())
569575
}
570576

571-
pub fn with_highlight_region<R>(r: RegionVid, counter: usize, op: impl FnOnce() -> R) -> R {
572-
HIGHLIGHT_REGION.with(|hr| {
577+
pub fn with_highlight_region_for_regionvid<R>(
578+
r: RegionVid,
579+
counter: usize,
580+
op: impl FnOnce() -> R
581+
) -> R {
582+
HIGHLIGHT_REGION_FOR_REGIONVID.with(|hr| {
583+
assert_eq!(hr.get(), None);
584+
hr.set(Some((r, counter)));
585+
let r = op();
586+
hr.set(None);
587+
r
588+
})
589+
}
590+
591+
fn get_highlight_region_for_region() -> Option<(ty::BoundRegion, usize)> {
592+
HIGHLIGHT_REGION_FOR_REGION.with(|hr| hr.get())
593+
}
594+
595+
pub fn with_highlight_region_for_region<R>(
596+
r: ty::BoundRegion,
597+
counter: usize,
598+
op: impl Fn() -> R
599+
) -> R {
600+
HIGHLIGHT_REGION_FOR_REGION.with(|hr| {
573601
assert_eq!(hr.get(), None);
574602
hr.set(Some((r, counter)));
575603
let r = op();
@@ -726,6 +754,15 @@ define_print! {
726754
return self.print_debug(f, cx);
727755
}
728756

757+
if let Some((region, counter)) = get_highlight_region_for_region() {
758+
if *self == region {
759+
return match *self {
760+
BrNamed(_, name) => write!(f, "{}", name),
761+
BrAnon(_) | BrFresh(_) | BrEnv => write!(f, "'{}", counter)
762+
};
763+
}
764+
}
765+
729766
match *self {
730767
BrNamed(_, name) => write!(f, "{}", name),
731768
BrAnon(_) | BrFresh(_) | BrEnv => Ok(())
@@ -748,7 +785,7 @@ define_print! {
748785
define_print! {
749786
() ty::RegionKind, (self, f, cx) {
750787
display {
751-
if cx.is_verbose || get_highlight_region().is_some() {
788+
if cx.is_verbose || get_highlight_region_for_regionvid().is_some() {
752789
return self.print_debug(f, cx);
753790
}
754791

@@ -923,7 +960,7 @@ impl fmt::Debug for ty::FloatVid {
923960

924961
impl fmt::Debug for ty::RegionVid {
925962
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
926-
if let Some((region, counter)) = get_highlight_region() {
963+
if let Some((region, counter)) = get_highlight_region_for_regionvid() {
927964
debug!("RegionVid.fmt: region={:?} self={:?} counter={:?}", region, self, counter);
928965
return if *self == region {
929966
write!(f, "'{:?}", counter)

0 commit comments

Comments
 (0)