Skip to content

Commit 68eb102

Browse files
committed
factor out free_region_binding_scope helper
1 parent 323e411 commit 68eb102

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/librustc/infer/error_reporting/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
177177

178178
ty::ReEarlyBound(_) |
179179
ty::ReFree(_) => {
180-
let scope = match *region {
181-
ty::ReEarlyBound(ref br) => {
182-
self.parent_def_id(br.def_id).unwrap()
183-
}
184-
ty::ReFree(ref fr) => fr.scope,
185-
_ => bug!()
186-
};
180+
let scope = region.free_region_binding_scope(self);
187181
let prefix = match *region {
188182
ty::ReEarlyBound(ref br) => {
189183
format!("the lifetime {} as defined on", br.name)

src/librustc/ty/sty.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,35 @@ impl RegionKind {
10501050

10511051
flags
10521052
}
1053+
1054+
/// Given an early-bound or free region, returns the def-id where it was bound.
1055+
/// For example, consider the regions in this snippet of code:
1056+
///
1057+
/// ```
1058+
/// impl<'a> Foo {
1059+
/// ^^ -- early bound, declared on an impl
1060+
///
1061+
/// fn bar<'b, 'c>(x: &self, y: &'b u32, z: &'c u64) where 'static: 'c
1062+
/// ^^ ^^ ^ anonymous, late-bound
1063+
/// | early-bound, appears in where-clauses
1064+
/// late-bound, appears only in fn args
1065+
/// {..}
1066+
/// }
1067+
/// ```
1068+
///
1069+
/// Here, `free_region_binding_scope('a)` would return the def-id
1070+
/// of the impl, and for all the other highlighted regions, it
1071+
/// would return the def-id of the function. In other cases (not shown), this
1072+
/// function might return the def-id of a closure.
1073+
pub fn free_region_binding_scope(&self, tcx: TyCtxt<'_, '_, '_>) -> DefId {
1074+
match self {
1075+
ty::ReEarlyBound(br) => {
1076+
tcx.parent_def_id(br.def_id).unwrap()
1077+
}
1078+
ty::ReFree(fr) => fr.scope,
1079+
_ => bug!("free_region_binding_scope invoked on inappropriate region: {:?}", self),
1080+
}
1081+
}
10531082
}
10541083

10551084
/// Type utilities

0 commit comments

Comments
 (0)