diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2014-12-04 20:06:41 -0500 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2014-12-10 19:45:19 -0500 |
| commit | ca98fefd04b2a0ccd784f96538c824c49210a418 (patch) | |
| tree | e5230aebe1d9cdd1d53d3f070050869c444c7e08 /src | |
| parent | c38e73fef53e8520e5170c40713e32ab965a8abe (diff) | |
| download | rust-ca98fefd04b2a0ccd784f96538c824c49210a418.tar.gz rust-ca98fefd04b2a0ccd784f96538c824c49210a418.zip | |
Fix two bugs in HRTB: 1. Categorize early-vs-late bindings on impls when constructing generics, so that we don't add unnecessary region parameters. 2. Correct the DeBruijn indices when substituting the self type into the method signature.
Previously, the DeBruijn index for the self type was not being adjusted to account for the fn binder. This mean that when late-bound regions were instantiated, you sometimes wind up with two distinct lifetimes. Fixes #19537.
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_typeck/astconv.rs | 17 | ||||
| -rw-r--r-- | src/librustc_typeck/collect.rs | 20 | ||||
| -rw-r--r-- | src/test/compile-fail/hrtb-debruijn-in-receiver.rs | 28 |
3 files changed, 58 insertions, 7 deletions
diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 7f1aad8ca77..790d882f836 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -54,6 +54,7 @@ use middle::resolve_lifetime as rl; use middle::subst::{FnSpace, TypeSpace, AssocSpace, SelfSpace, Subst, Substs}; use middle::subst::{VecPerParamSpace}; use middle::ty::{mod, Ty}; +use middle::ty_fold; use rscope::{mod, UnelidableRscope, RegionScope, SpecificRscope, ShiftedRscope, BindingRscope}; use TypeAndSubsts; @@ -1062,7 +1063,8 @@ fn ty_of_method_or_bare_fn<'a, 'tcx, AC: AstConv<'tcx>>( opt_self_info: Option<SelfInfo<'a, 'tcx>>, decl: &ast::FnDecl) -> (ty::BareFnTy<'tcx>, - Option<ty::ExplicitSelfCategory>) { + Option<ty::ExplicitSelfCategory>) +{ debug!("ty_of_method_or_bare_fn"); // New region names that appear inside of the arguments of the function @@ -1078,6 +1080,11 @@ fn ty_of_method_or_bare_fn<'a, 'tcx, AC: AstConv<'tcx>>( let (self_ty, mut implied_output_region) = match opt_self_info { None => (None, None), Some(self_info) => { + // Shift regions in the self type by 1 to account for the binding + // level introduced by the function itself. + let untransformed_self_ty = + ty_fold::shift_regions(this.tcx(), 1, &self_info.untransformed_self_ty); + // Figure out and record the explicit self category. let explicit_self_category = determine_explicit_self_category(this, &rb, &self_info); @@ -1087,21 +1094,19 @@ fn ty_of_method_or_bare_fn<'a, 'tcx, AC: AstConv<'tcx>>( (None, None) } ty::ByValueExplicitSelfCategory => { - (Some(self_info.untransformed_self_ty), None) + (Some(untransformed_self_ty), None) } ty::ByReferenceExplicitSelfCategory(region, mutability) => { (Some(ty::mk_rptr(this.tcx(), region, ty::mt { - ty: self_info.untransformed_self_ty, + ty: untransformed_self_ty, mutbl: mutability })), Some(region)) } ty::ByBoxExplicitSelfCategory => { - (Some(ty::mk_uniq(this.tcx(), - self_info.untransformed_self_ty)), - None) + (Some(ty::mk_uniq(this.tcx(), untransformed_self_ty)), None) } } } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 74ac9c480de..4ad6dc292a7 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1055,7 +1055,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) { ref selfty, ref impl_items) => { // Create generics from the generics specified in the impl head. - let ty_generics = ty_generics_for_type( + let ty_generics = ty_generics_for_impl( ccx, generics, CreateTypeParametersForAssociatedTypes); @@ -1655,6 +1655,24 @@ fn ty_generics_for_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, generics } +fn ty_generics_for_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, + generics: &ast::Generics, + create_type_parameters_for_associated_types: + CreateTypeParametersForAssociatedTypesFlag) + -> ty::Generics<'tcx> +{ + let early_lifetimes = resolve_lifetime::early_bound_lifetimes(generics); + debug!("ty_generics_for_impl: early_lifetimes={}", + early_lifetimes); + ty_generics(ccx, + subst::TypeSpace, + early_lifetimes.as_slice(), + generics.ty_params.as_slice(), + ty::Generics::empty(), + &generics.where_clause, + create_type_parameters_for_associated_types) +} + fn ty_generics_for_fn_or_method<'tcx,AC>( this: &AC, generics: &ast::Generics, diff --git a/src/test/compile-fail/hrtb-debruijn-in-receiver.rs b/src/test/compile-fail/hrtb-debruijn-in-receiver.rs new file mode 100644 index 00000000000..2dbd16107b0 --- /dev/null +++ b/src/test/compile-fail/hrtb-debruijn-in-receiver.rs @@ -0,0 +1,28 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test the case where the `Self` type has a bound lifetime that must +// be adjusted in the fn signature. Issue #19537. + +use std::collections::HashMap; + +struct Foo<'a> { + map: HashMap<uint, &'a str> +} + +impl<'a> Foo<'a> { + fn new() -> Foo<'a> { panic!() } + fn insert(&'a mut self) { } +} +fn main() { + let mut foo = Foo::new(); + foo.insert(); + foo.insert(); //~ ERROR cannot borrow +} |
