Skip to content

Commit

Permalink
rustc_typeck: force users of RegionScope to get anon_region's one by …
Browse files Browse the repository at this point in the history
…one.
  • Loading branch information
eddyb committed Jan 28, 2017
1 parent 154c202 commit 4f559f8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 74 deletions.
14 changes: 7 additions & 7 deletions src/librustc_typeck/astconv.rs
Expand Up @@ -306,9 +306,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
ast_region_to_region(self.tcx(), lifetime)
}

None => self.tcx().mk_region(match rscope.anon_regions(default_span, 1) {
Ok(rs) => rs[0],
Err(params) => {
None => {
self.tcx().mk_region(rscope.anon_region(default_span).unwrap_or_else(|params| {
let ampersand_span = Span { hi: default_span.lo, ..default_span};

let mut err = struct_span_err!(self.tcx().sess, ampersand_span, E0106,
Expand All @@ -320,8 +319,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
}
err.emit();
ty::ReStatic
}
})
}))
}
};

debug!("opt_ast_region_to_region(opt_lifetime={:?}) yields {:?}",
Expand Down Expand Up @@ -412,8 +411,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let regions = if expected_num_region_params == supplied_num_region_params {
lifetimes.iter().map(|l| *ast_region_to_region(tcx, l)).collect()
} else {
let anon_regions =
rscope.anon_regions(span, expected_num_region_params);
let anon_regions = (0..expected_num_region_params).map(|_| {
rscope.anon_region(span)
}).collect::<Result<Vec<_>, _>>();

if supplied_num_region_params != 0 || anon_regions.is_err() {
report_lifetime_number_error(tcx, span,
Expand Down
8 changes: 3 additions & 5 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -1466,11 +1466,9 @@ impl<'a, 'gcx, 'tcx> RegionScope for FnCtxt<'a, 'gcx, 'tcx> {
*self.next_region_var(infer::MiscVariable(span))
}

fn anon_regions(&self, span: Span, count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
Ok((0..count).map(|_| {
*self.next_region_var(infer::MiscVariable(span))
}).collect())
fn anon_region(&self, span: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>> {
Ok(*self.next_region_var(infer::MiscVariable(span)))
}
}

Expand Down
91 changes: 29 additions & 62 deletions src/librustc_typeck/rscope.rs
Expand Up @@ -33,18 +33,16 @@ pub type ElidedLifetime = Result<ty::Region, Option<Vec<ElisionFailureInfo>>>;
/// Defines strategies for handling regions that are omitted. For
/// example, if one writes the type `&Foo`, then the lifetime of
/// this reference has been omitted. When converting this
/// type, the generic functions in astconv will invoke `anon_regions`
/// type, the generic functions in astconv will invoke `anon_region`
/// on the provided region-scope to decide how to translate this
/// omitted region.
///
/// It is not always legal to omit regions, therefore `anon_regions`
/// It is not always legal to omit regions, therefore `anon_region`
/// can return `Err(())` to indicate that this is not a scope in which
/// regions can legally be omitted.
pub trait RegionScope {
fn anon_regions(&self,
span: Span,
count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>;
fn anon_region(&self, span: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>;

/// If an object omits any explicit lifetime bound, and none can
/// be derived from the object traits, what should we use? If
Expand Down Expand Up @@ -117,11 +115,9 @@ impl<R: RegionScope> RegionScope for MaybeWithAnonTypes<R> {
self.base_scope.object_lifetime_default(span)
}

fn anon_regions(&self,
span: Span,
count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
self.base_scope.anon_regions(span, count)
fn anon_region(&self, span: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>> {
self.base_scope.anon_region(span)
}

fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
Expand All @@ -139,10 +135,8 @@ impl<R: RegionScope> RegionScope for MaybeWithAnonTypes<R> {
pub struct ExplicitRscope;

impl RegionScope for ExplicitRscope {
fn anon_regions(&self,
_span: Span,
_count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
fn anon_region(&self, _span: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>> {
Err(None)
}

Expand All @@ -165,12 +159,9 @@ impl UnelidableRscope {
}

impl RegionScope for UnelidableRscope {
fn anon_regions(&self,
_span: Span,
_count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
let UnelidableRscope(ref v) = *self;
Err(v.clone())
fn anon_region(&self, _span: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>> {
Err(self.0.clone())
}

fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
Expand Down Expand Up @@ -208,12 +199,10 @@ impl RegionScope for ElidableRscope {
ty::ReStatic
}

fn anon_regions(&self,
_span: Span,
count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
fn anon_region(&self, _span: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>
{
Ok(vec![self.default; count])
Ok(self.default)
}
}

Expand All @@ -232,10 +221,8 @@ impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> StaticRscope<'a, 'gcx, 'tcx> {
}

impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> RegionScope for StaticRscope<'a, 'gcx, 'tcx> {
fn anon_regions(&self,
span: Span,
count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
fn anon_region(&self, span: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>> {
if !self.tcx.sess.features.borrow().static_in_const {
self.tcx
.sess
Expand All @@ -244,7 +231,7 @@ impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> RegionScope for StaticRscope<'a, 'gcx, 'tcx>
`static_in_const` feature, see #35897")
.emit();
}
Ok(vec![ty::ReStatic; count])
Ok(ty::ReStatic)
}

fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
Expand All @@ -268,12 +255,6 @@ impl BindingRscope {
anon_bindings: Cell::new(0),
}
}

fn next_region(&self) -> ty::Region {
let idx = self.anon_bindings.get();
self.anon_bindings.set(idx + 1);
ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrAnon(idx))
}
}

impl RegionScope for BindingRscope {
Expand All @@ -288,12 +269,12 @@ impl RegionScope for BindingRscope {
ty::ReStatic
}

fn anon_regions(&self,
_: Span,
count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
fn anon_region(&self, _: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>
{
Ok((0..count).map(|_| self.next_region()).collect())
let idx = self.anon_bindings.get();
self.anon_bindings.set(idx + 1);
Ok(ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrAnon(idx)))
}
}

Expand Down Expand Up @@ -334,12 +315,10 @@ impl<'r> RegionScope for ObjectLifetimeDefaultRscope<'r> {
self.base_scope.base_object_lifetime_default(span)
}

fn anon_regions(&self,
span: Span,
count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
fn anon_region(&self, span: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>
{
self.base_scope.anon_regions(span, count)
self.base_scope.anon_region(span)
}

fn anon_type_scope(&self) -> Option<AnonTypeScope> {
Expand Down Expand Up @@ -369,22 +348,10 @@ impl<'r> RegionScope for ShiftedRscope<'r> {
ty::fold::shift_region(self.base_scope.base_object_lifetime_default(span), 1)
}

fn anon_regions(&self,
span: Span,
count: usize)
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>>
fn anon_region(&self, span: Span)
-> Result<ty::Region, Option<Vec<ElisionFailureInfo>>>
{
match self.base_scope.anon_regions(span, count) {
Ok(mut v) => {
for r in &mut v {
*r = ty::fold::shift_region(*r, 1);
}
Ok(v)
}
Err(errs) => {
Err(errs)
}
}
self.base_scope.anon_region(span).map(|r| ty::fold::shift_region(r, 1))
}

fn anon_type_scope(&self) -> Option<AnonTypeScope> {
Expand Down

0 comments on commit 4f559f8

Please sign in to comment.