Skip to content

Commit f49f6e7

Browse files
committed
Auto merge of #54229 - davidtwco:issue-52534, r=pnkfelix
[nll] borrows that must be valid for a free lifetime should explain why Fixes #52534. r? @nikomatsakis
2 parents be91c35 + b342f00 commit f49f6e7

21 files changed

+1338
-431
lines changed

src/librustc/mir/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,18 @@ impl<'tcx> Place<'tcx> {
18831883
pub fn elem(self, elem: PlaceElem<'tcx>) -> Place<'tcx> {
18841884
Place::Projection(Box::new(PlaceProjection { base: self, elem }))
18851885
}
1886+
1887+
/// Find the innermost `Local` from this `Place`.
1888+
pub fn local(&self) -> Option<Local> {
1889+
match self {
1890+
Place::Local(local) |
1891+
Place::Projection(box Projection {
1892+
base: Place::Local(local),
1893+
elem: ProjectionElem::Deref,
1894+
}) => Some(*local),
1895+
_ => None,
1896+
}
1897+
}
18861898
}
18871899

18881900
impl<'tcx> Debug for Place<'tcx> {

src/librustc/ty/sty.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,23 @@ impl_stable_hash_for!(struct DebruijnIndex { private });
13241324

13251325
/// Region utilities
13261326
impl RegionKind {
1327+
/// Is this region named by the user?
1328+
pub fn has_name(&self) -> bool {
1329+
match *self {
1330+
RegionKind::ReEarlyBound(ebr) => ebr.has_name(),
1331+
RegionKind::ReLateBound(_, br) => br.is_named(),
1332+
RegionKind::ReFree(fr) => fr.bound_region.is_named(),
1333+
RegionKind::ReScope(..) => false,
1334+
RegionKind::ReStatic => true,
1335+
RegionKind::ReVar(..) => false,
1336+
RegionKind::ReSkolemized(_, br) => br.is_named(),
1337+
RegionKind::ReEmpty => false,
1338+
RegionKind::ReErased => false,
1339+
RegionKind::ReClosureBound(..) => false,
1340+
RegionKind::ReCanonical(..) => false,
1341+
}
1342+
}
1343+
13271344
pub fn is_late_bound(&self) -> bool {
13281345
match *self {
13291346
ty::ReLateBound(..) => true,

src/librustc/util/ppaux.rs

+45-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ 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_BOUND_REGION: Cell<Option<(ty::BoundRegion, usize)>> =
44+
Cell::new(None)
3845
}
3946

4047
macro_rules! gen_display_debug_body {
@@ -564,12 +571,34 @@ pub fn parameterized<F: fmt::Write>(f: &mut F,
564571
PrintContext::new().parameterized(f, substs, did, projections)
565572
}
566573

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

571-
pub fn with_highlight_region<R>(r: RegionVid, counter: usize, op: impl FnOnce() -> R) -> R {
572-
HIGHLIGHT_REGION.with(|hr| {
578+
pub fn with_highlight_region_for_regionvid<R>(
579+
r: RegionVid,
580+
counter: usize,
581+
op: impl FnOnce() -> R
582+
) -> R {
583+
HIGHLIGHT_REGION_FOR_REGIONVID.with(|hr| {
584+
assert_eq!(hr.get(), None);
585+
hr.set(Some((r, counter)));
586+
let r = op();
587+
hr.set(None);
588+
r
589+
})
590+
}
591+
592+
fn get_highlight_region_for_bound_region() -> Option<(ty::BoundRegion, usize)> {
593+
HIGHLIGHT_REGION_FOR_BOUND_REGION.with(|hr| hr.get())
594+
}
595+
596+
pub fn with_highlight_region_for_bound_region<R>(
597+
r: ty::BoundRegion,
598+
counter: usize,
599+
op: impl Fn() -> R
600+
) -> R {
601+
HIGHLIGHT_REGION_FOR_BOUND_REGION.with(|hr| {
573602
assert_eq!(hr.get(), None);
574603
hr.set(Some((r, counter)));
575604
let r = op();
@@ -726,6 +755,15 @@ define_print! {
726755
return self.print_debug(f, cx);
727756
}
728757

758+
if let Some((region, counter)) = get_highlight_region_for_bound_region() {
759+
if *self == region {
760+
return match *self {
761+
BrNamed(_, name) => write!(f, "{}", name),
762+
BrAnon(_) | BrFresh(_) | BrEnv => write!(f, "'{}", counter)
763+
};
764+
}
765+
}
766+
729767
match *self {
730768
BrNamed(_, name) => write!(f, "{}", name),
731769
BrAnon(_) | BrFresh(_) | BrEnv => Ok(())
@@ -748,7 +786,7 @@ define_print! {
748786
define_print! {
749787
() ty::RegionKind, (self, f, cx) {
750788
display {
751-
if cx.is_verbose || get_highlight_region().is_some() {
789+
if cx.is_verbose || get_highlight_region_for_regionvid().is_some() {
752790
return self.print_debug(f, cx);
753791
}
754792

@@ -923,7 +961,7 @@ impl fmt::Debug for ty::FloatVid {
923961

924962
impl fmt::Debug for ty::RegionVid {
925963
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
926-
if let Some((region, counter)) = get_highlight_region() {
964+
if let Some((region, counter)) = get_highlight_region_for_regionvid() {
927965
debug!("RegionVid.fmt: region={:?} self={:?} counter={:?}", region, self, counter);
928966
return if *self == region {
929967
write!(f, "'{:?}", counter)

0 commit comments

Comments
 (0)