Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prohibit unconstrained lifetimes that appear in associated types #24461

Merged
merged 4 commits into from
Apr 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,12 @@ fn parse_region_<F>(st: &mut PState, conv: &mut F) -> ty::Region where
let index = parse_u32(st);
assert_eq!(next(st), '|');
let nm = token::str_to_ident(&parse_str(st, ']'));
ty::ReEarlyBound(node_id, space, index, nm.name)
ty::ReEarlyBound(ty::EarlyBoundRegion {
param_id: node_id,
space: space,
index: index,
name: nm.name
})
}
'f' => {
assert_eq!(next(st), '[');
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ pub fn enc_region(w: &mut Encoder, cx: &ctxt, r: ty::Region) {
enc_bound_region(w, cx, br);
mywrite!(w, "]");
}
ty::ReEarlyBound(node_id, space, index, name) => {
ty::ReEarlyBound(ref data) => {
mywrite!(w, "B[{}|{}|{}|{}]",
node_id,
space.to_uint(),
index,
token::get_name(name));
data.param_id,
data.space.to_uint(),
data.index,
token::get_name(data.name));
}
ty::ReFree(ref fr) => {
mywrite!(w, "f[");
Expand Down
9 changes: 7 additions & 2 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,13 @@ impl tr for ty::Region {
ty::ReLateBound(debruijn, br) => {
ty::ReLateBound(debruijn, br.tr(dcx))
}
ty::ReEarlyBound(id, space, index, ident) => {
ty::ReEarlyBound(dcx.tr_id(id), space, index, ident)
ty::ReEarlyBound(data) => {
ty::ReEarlyBound(ty::EarlyBoundRegion {
param_id: dcx.tr_id(data.param_id),
space: data.space,
index: data.index,
name: data.name,
})
}
ty::ReScope(scope) => {
ty::ReScope(scope.tr(dcx))
Expand Down
9 changes: 3 additions & 6 deletions src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,11 @@ impl RegionMaps {
self.sub_free_region(sub_fr, super_fr)
}

(ty::ReEarlyBound(param_id_a, param_space_a, index_a, _),
ty::ReEarlyBound(param_id_b, param_space_b, index_b, _)) => {
(ty::ReEarlyBound(data_a), ty::ReEarlyBound(data_b)) => {
// This case is used only to make sure that explicitly-
// specified `Self` types match the real self type in
// implementations.
param_id_a == param_id_b &&
param_space_a == param_space_b &&
index_a == index_b
// implementations. Yuck.
data_a == data_b
}

_ => {
Expand Down
15 changes: 8 additions & 7 deletions src/librustc/middle/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,11 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
// regions that appear in a function signature is done using
// the specialized routine `ty::replace_late_regions()`.
match r {
ty::ReEarlyBound(_, space, i, region_name) => {
ty::ReEarlyBound(data) => {
match self.substs.regions {
ErasedRegions => ty::ReStatic,
NonerasedRegions(ref regions) =>
match regions.opt_get(space, i as usize) {
match regions.opt_get(data.space, data.index as usize) {
Some(&r) => {
self.shift_region_through_binders(r)
}
Expand All @@ -635,11 +635,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
self.tcx().sess.span_bug(
span,
&format!("Type parameter out of range \
when substituting in region {} (root type={}) \
(space={:?}, index={})",
region_name.as_str(),
self.root_ty.repr(self.tcx()),
space, i));
when substituting in region {} (root type={}) \
(space={:?}, index={})",
data.name.as_str(),
self.root_ty.repr(self.tcx()),
data.space,
data.index));
}
}
}
Expand Down
23 changes: 16 additions & 7 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,10 +1134,7 @@ pub enum Region {
// Region bound in a type or fn declaration which will be
// substituted 'early' -- that is, at the same time when type
// parameters are substituted.
ReEarlyBound(/* param id */ ast::NodeId,
subst::ParamSpace,
/*index*/ u32,
ast::Name),
ReEarlyBound(EarlyBoundRegion),

// Region bound in a function scope, which will be substituted when the
// function is called.
Expand Down Expand Up @@ -1169,6 +1166,14 @@ pub enum Region {
ReEmpty,
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
pub struct EarlyBoundRegion {
pub param_id: ast::NodeId,
pub space: subst::ParamSpace,
pub index: u32,
pub name: ast::Name,
}

/// Upvars do not get their own node-id. Instead, we use the pair of
/// the original var id (that is, the root variable that is referenced
/// by the upvar) and the id of the closure expression.
Expand Down Expand Up @@ -1761,7 +1766,12 @@ pub struct RegionParameterDef {

impl RegionParameterDef {
pub fn to_early_bound_region(&self) -> ty::Region {
ty::ReEarlyBound(self.def_id.node, self.space, self.index, self.name)
ty::ReEarlyBound(ty::EarlyBoundRegion {
param_id: self.def_id.node,
space: self.space,
index: self.index,
name: self.name,
})
}
pub fn to_bound_region(&self) -> ty::BoundRegion {
ty::BoundRegion::BrNamed(self.def_id, self.name)
Expand Down Expand Up @@ -7071,8 +7081,7 @@ pub fn make_substs_for_receiver_types<'tcx>(tcx: &ty::ctxt<'tcx>,
let meth_regions: Vec<ty::Region> =
method.generics.regions.get_slice(subst::FnSpace)
.iter()
.map(|def| ty::ReEarlyBound(def.def_id.node, def.space,
def.index, def.name))
.map(|def| def.to_early_bound_region())
.collect();
trait_ref.substs.clone().with_method(meth_tps, meth_regions)
}
Expand Down
18 changes: 9 additions & 9 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)

ReEmpty => { ("the empty lifetime".to_string(), None) }

ReEarlyBound(_, _, _, name) => {
(format!("{}", token::get_name(name)), None)
ReEarlyBound(ref data) => {
(format!("{}", token::get_name(data.name)), None)
}

// I believe these cases should not occur (except when debugging,
Expand Down Expand Up @@ -223,8 +223,8 @@ pub fn region_to_string(cx: &ctxt, prefix: &str, space: bool, region: Region) ->
// `explain_region()` or `note_and_explain_region()`.
match region {
ty::ReScope(_) => prefix.to_string(),
ty::ReEarlyBound(_, _, _, name) => {
token::get_name(name).to_string()
ty::ReEarlyBound(ref data) => {
token::get_name(data.name).to_string()
}
ty::ReLateBound(_, br) => bound_region_to_string(cx, prefix, space, br),
ty::ReFree(ref fr) => bound_region_to_string(cx, prefix, space, fr.bound_region),
Expand Down Expand Up @@ -899,12 +899,12 @@ impl<'tcx> Repr<'tcx> for ty::BoundRegion {
impl<'tcx> Repr<'tcx> for ty::Region {
fn repr(&self, tcx: &ctxt) -> String {
match *self {
ty::ReEarlyBound(id, space, index, name) => {
ty::ReEarlyBound(ref data) => {
format!("ReEarlyBound({}, {:?}, {}, {})",
id,
space,
index,
token::get_name(name))
data.param_id,
data.space,
data.index,
token::get_name(data.name))
}

ty::ReLateBound(binder_id, ref bound_region) => {
Expand Down
7 changes: 6 additions & 1 deletion src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
-> ty::Region
{
let name = token::intern(name);
ty::ReEarlyBound(ast::DUMMY_NODE_ID, space, index, name)
ty::ReEarlyBound(ty::EarlyBoundRegion {
param_id: ast::DUMMY_NODE_ID,
space: space,
index: index,
name: name
})
}

pub fn re_late_bound_with_debruijn(&self, id: u32, debruijn: ty::DebruijnIndex) -> ty::Region {
Expand Down
7 changes: 6 additions & 1 deletion src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ pub fn ast_region_to_region(tcx: &ty::ctxt, lifetime: &ast::Lifetime)
}

Some(&rl::DefEarlyBoundRegion(space, index, id)) => {
ty::ReEarlyBound(id, space, index, lifetime.name)
ty::ReEarlyBound(ty::EarlyBoundRegion {
param_id: id,
space: space,
index: index,
name: lifetime.name
})
}

Some(&rl::DefFreeRegion(scope, id)) => {
Expand Down
13 changes: 7 additions & 6 deletions src/librustc_typeck/check/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use astconv::AstConv;
use check::{FnCtxt, Inherited, blank_fn_ctxt, vtable, regionck};
use constrained_type_params::identify_constrained_type_params;
use constrained_type_params::{identify_constrained_type_params, Parameter};
use CrateCtxt;
use middle::region;
use middle::subst::{self, TypeSpace, FnSpace, ParamSpace, SelfSpace};
Expand Down Expand Up @@ -287,10 +287,11 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {

let mut constrained_parameters: HashSet<_> =
variances.types
.iter_enumerated()
.filter(|&(_, _, &variance)| variance != ty::Bivariant)
.map(|(space, index, _)| self.param_ty(ast_generics, space, index))
.collect();
.iter_enumerated()
.filter(|&(_, _, &variance)| variance != ty::Bivariant)
.map(|(space, index, _)| self.param_ty(ast_generics, space, index))
.map(|p| Parameter::Type(p))
.collect();

identify_constrained_type_params(self.tcx(),
ty_predicates.predicates.as_slice(),
Expand All @@ -299,7 +300,7 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {

for (space, index, _) in variances.types.iter_enumerated() {
let param_ty = self.param_ty(ast_generics, space, index);
if constrained_parameters.contains(&param_ty) {
if constrained_parameters.contains(&Parameter::Type(param_ty)) {
continue;
}
let span = self.ty_param_span(ast_generics, item, space, index);
Expand Down
Loading