Skip to content

Commit

Permalink
auto merge of #14604 : nikomatsakis/rust/issue-5527-namespace-substs,…
Browse files Browse the repository at this point in the history
… r=pcwalton

The current setup is to have a single vector of type parameters in
scope at any one time. We then have to concatenate the parameters from
the impl/trait with those of the method. This makes a lot of things
awkward, most notably associated fns ("static fns"). This branch
restructures the substitutions into three distinct namespaces (type,
self, fn). This makes most of the "type parameter management"
trivial. This also sets us up to support UFCS (though I haven't made
any particular changes in that direction in this patch).

Along the way, this patch fixes a few miscellaneous bits of code cleanup:

1. Patch resolve to detect references to out-of-scope type parameters,
   rather than checking for "out of bound" indices during substitution
   (fixes #14603).

2. Move def out of libsyntax into librustc where it belongs. I should have
   moved DefId too, but didn't.
   
3. Permit homogeneous tuples like `(T, T, T)` to be used as fixed-length
   vectors like `[T, ..3]`. This is awfully handy, though public facing.
   I suppose it requires an RFC.

4. Add some missing tests.

cc #5527

r? @pcwalton or @pnkfelix
  • Loading branch information
bors committed Jun 13, 2014
2 parents 0422934 + 9153d8a commit e7f11f2
Show file tree
Hide file tree
Showing 49 changed files with 2,571 additions and 2,226 deletions.
10 changes: 7 additions & 3 deletions src/librustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ pub static tag_impls_impl: uint = 0x81;
pub static tag_items_data_item_inherent_impl: uint = 0x82;
pub static tag_items_data_item_extension_impl: uint = 0x83;

pub static tag_region_param_def: uint = 0x84;
pub static tag_region_param_def_ident: uint = 0x85;
pub static tag_region_param_def_def_id: uint = 0x86;
// GAP 0x84, 0x85, 0x86

pub static tag_native_libraries: uint = 0x87;
pub static tag_native_libraries_lib: uint = 0x88;
Expand All @@ -217,3 +215,9 @@ pub struct LinkMeta {
pub crateid: CrateId,
pub crate_hash: Svh,
}

pub static tag_region_param_def: uint = 0x90;
pub static tag_region_param_def_ident: uint = 0x91;
pub static tag_region_param_def_def_id: uint = 0x92;
pub static tag_region_param_def_space: uint = 0x93;
pub static tag_region_param_def_index: uint = 0x94;
8 changes: 5 additions & 3 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use metadata::decoder;
use middle::lang_items;
use middle::ty;
use middle::typeck;
use middle::subst::VecPerParamSpace;

use serialize::ebml;
use serialize::ebml::reader;
Expand Down Expand Up @@ -223,8 +224,8 @@ pub fn get_field_type(tcx: &ty::ctxt, class_id: ast::DefId,
});
let ty = decoder::item_type(def, the_field, tcx, &*cdata);
ty::ty_param_bounds_and_ty {
generics: ty::Generics {type_param_defs: Rc::new(Vec::new()),
region_param_defs: Rc::new(Vec::new())},
generics: ty::Generics {types: VecPerParamSpace::empty(),
regions: VecPerParamSpace::empty()},
ty: ty
}
}
Expand All @@ -240,7 +241,8 @@ pub fn get_impl_trait(tcx: &ty::ctxt,

// Given a def_id for an impl, return information about its vtables
pub fn get_impl_vtables(tcx: &ty::ctxt,
def: ast::DefId) -> typeck::impl_res {
def: ast::DefId)
-> typeck::vtable_res {
let cstore = &tcx.sess.cstore;
let cdata = cstore.get_crate_data(def.krate);
decoder::get_impl_vtables(&*cdata, def.node, tcx)
Expand Down
68 changes: 38 additions & 30 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use metadata::tydecode::{parse_ty_data, parse_def_id,
parse_bare_fn_ty_data, parse_trait_ref_data};
use middle::lang_items;
use middle::def;
use middle::subst;
use middle::ty::{ImplContainer, TraitContainer};
use middle::ty;
use middle::typeck;
Expand Down Expand Up @@ -257,34 +258,44 @@ fn item_ty_param_defs(item: ebml::Doc,
tcx: &ty::ctxt,
cdata: Cmd,
tag: uint)
-> Rc<Vec<ty::TypeParameterDef> > {
let mut bounds = Vec::new();
-> subst::VecPerParamSpace<ty::TypeParameterDef> {
let mut bounds = subst::VecPerParamSpace::empty();
reader::tagged_docs(item, tag, |p| {
let bd = parse_type_param_def_data(
p.data, p.start, cdata.cnum, tcx,
|_, did| translate_def_id(cdata, did));
bounds.push(bd);
bounds.push(bd.space, bd);
true
});
Rc::new(bounds)
bounds
}

fn item_region_param_defs(item_doc: ebml::Doc, cdata: Cmd)
-> Rc<Vec<ty::RegionParameterDef> > {
let mut v = Vec::new();
-> subst::VecPerParamSpace<ty::RegionParameterDef>
{
let mut v = subst::VecPerParamSpace::empty();
reader::tagged_docs(item_doc, tag_region_param_def, |rp_doc| {
let ident_str_doc = reader::get_doc(rp_doc,
tag_region_param_def_ident);
let ident = item_name(&*token::get_ident_interner(), ident_str_doc);
let def_id_doc = reader::get_doc(rp_doc,
tag_region_param_def_def_id);
let def_id = reader::with_doc_data(def_id_doc, parse_def_id);
let def_id = translate_def_id(cdata, def_id);
v.push(ty::RegionParameterDef { name: ident.name,
def_id: def_id });
true
});
Rc::new(v)
let ident_str_doc = reader::get_doc(rp_doc,
tag_region_param_def_ident);
let ident = item_name(&*token::get_ident_interner(), ident_str_doc);
let def_id_doc = reader::get_doc(rp_doc,
tag_region_param_def_def_id);
let def_id = reader::with_doc_data(def_id_doc, parse_def_id);
let def_id = translate_def_id(cdata, def_id);

let doc = reader::get_doc(rp_doc, tag_region_param_def_space);
let space = subst::ParamSpace::from_uint(reader::doc_as_u64(doc) as uint);

let doc = reader::get_doc(rp_doc, tag_region_param_def_index);
let index = reader::doc_as_u64(doc) as uint;

v.push(space, ty::RegionParameterDef { name: ident.name,
def_id: def_id,
space: space,
index: index });
true
});
v
}

fn enum_variant_ids(item: ebml::Doc, cdata: Cmd) -> Vec<ast::DefId> {
Expand Down Expand Up @@ -403,8 +414,8 @@ pub fn get_trait_def(cdata: Cmd,
}

ty::TraitDef {
generics: ty::Generics {type_param_defs: tp_defs,
region_param_defs: rp_defs},
generics: ty::Generics {types: tp_defs,
regions: rp_defs},
bounds: bounds,
trait_ref: Rc::new(item_trait_ref(item_doc, tcx, cdata))
}
Expand All @@ -422,8 +433,8 @@ pub fn get_type(cdata: Cmd, id: ast::NodeId, tcx: &ty::ctxt)
let rp_defs = item_region_param_defs(item, cdata);

ty::ty_param_bounds_and_ty {
generics: ty::Generics {type_param_defs: tp_defs,
region_param_defs: rp_defs},
generics: ty::Generics {types: tp_defs,
regions: rp_defs},
ty: t
}
}
Expand All @@ -440,16 +451,13 @@ pub fn get_impl_trait(cdata: Cmd,

pub fn get_impl_vtables(cdata: Cmd,
id: ast::NodeId,
tcx: &ty::ctxt) -> typeck::impl_res
tcx: &ty::ctxt)
-> typeck::vtable_res
{
let item_doc = lookup_item(id, cdata.data());
let vtables_doc = reader::get_doc(item_doc, tag_item_impl_vtables);
let mut decoder = reader::Decoder::new(vtables_doc);

typeck::impl_res {
trait_vtables: decoder.read_vtable_res(tcx, cdata),
self_vtables: decoder.read_vtable_param_res(tcx, cdata)
}
decoder.read_vtable_res(tcx, cdata)
}


Expand Down Expand Up @@ -802,8 +810,8 @@ pub fn get_method(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::NodeId,
ty::Method::new(
name,
ty::Generics {
type_param_defs: type_param_defs,
region_param_defs: rp_defs,
types: type_param_defs,
regions: rp_defs,
},
fty,
explicit_self,
Expand Down
33 changes: 18 additions & 15 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use metadata::common::*;
use metadata::cstore;
use metadata::decoder;
use metadata::tyencode;
use middle::subst::VecPerParamSpace;
use middle::ty::{node_id_to_type, lookup_item_type};
use middle::astencode;
use middle::ty;
Expand Down Expand Up @@ -128,10 +129,9 @@ fn encode_trait_ref(ebml_w: &mut Encoder,

fn encode_impl_vtables(ebml_w: &mut Encoder,
ecx: &EncodeContext,
vtables: &typeck::impl_res) {
vtables: &typeck::vtable_res) {
ebml_w.start_tag(tag_item_impl_vtables);
astencode::encode_vtable_res(ecx, ebml_w, &vtables.trait_vtables);
astencode::encode_vtable_param_res(ecx, ebml_w, &vtables.self_vtables);
astencode::encode_vtable_res(ecx, ebml_w, vtables);
ebml_w.end_tag();
}

Expand All @@ -148,7 +148,7 @@ pub fn def_to_str(did: DefId) -> String {

fn encode_ty_type_param_defs(ebml_w: &mut Encoder,
ecx: &EncodeContext,
params: &[ty::TypeParameterDef],
params: &VecPerParamSpace<ty::TypeParameterDef>,
tag: uint) {
let ty_str_ctxt = &tyencode::ctxt {
diag: ecx.diag,
Expand All @@ -164,7 +164,7 @@ fn encode_ty_type_param_defs(ebml_w: &mut Encoder,
}

fn encode_region_param_defs(ebml_w: &mut Encoder,
params: &[ty::RegionParameterDef]) {
params: &VecPerParamSpace<ty::RegionParameterDef>) {
for param in params.iter() {
ebml_w.start_tag(tag_region_param_def);

Expand All @@ -175,6 +175,12 @@ fn encode_region_param_defs(ebml_w: &mut Encoder,
ebml_w.wr_tagged_str(tag_region_param_def_def_id,
def_to_str(param.def_id).as_slice());

ebml_w.wr_tagged_u64(tag_region_param_def_space,
param.space.to_uint() as u64);

ebml_w.wr_tagged_u64(tag_region_param_def_index,
param.index as u64);

ebml_w.end_tag();
}
}
Expand All @@ -191,9 +197,9 @@ fn encode_item_variances(ebml_w: &mut Encoder,
fn encode_bounds_and_type(ebml_w: &mut Encoder,
ecx: &EncodeContext,
tpt: &ty::ty_param_bounds_and_ty) {
encode_ty_type_param_defs(ebml_w, ecx, tpt.generics.type_param_defs(),
encode_ty_type_param_defs(ebml_w, ecx, &tpt.generics.types,
tag_items_data_item_ty_param_bounds);
encode_region_param_defs(ebml_w, tpt.generics.region_param_defs());
encode_region_param_defs(ebml_w, &tpt.generics.regions);
encode_type(ecx, ebml_w, tpt.ty);
}

Expand Down Expand Up @@ -725,8 +731,7 @@ fn encode_method_ty_fields(ecx: &EncodeContext,
method_ty: &ty::Method) {
encode_def_id(ebml_w, method_ty.def_id);
encode_name(ebml_w, method_ty.ident.name);
encode_ty_type_param_defs(ebml_w, ecx,
method_ty.generics.type_param_defs(),
encode_ty_type_param_defs(ebml_w, ecx, &method_ty.generics.types,
tag_item_method_tps);
encode_method_fty(ecx, ebml_w, &method_ty.fty);
encode_visibility(ebml_w, method_ty.vis);
Expand Down Expand Up @@ -770,10 +775,8 @@ fn encode_info_for_method(ecx: &EncodeContext,
}

for &ast_method in ast_method_opt.iter() {
let num_params = tpt.generics.type_param_defs().len();
if num_params > 0u ||
is_default_impl ||
should_inline(ast_method.attrs.as_slice()) {
let any_types = !tpt.generics.types.is_empty();
if any_types || is_default_impl || should_inline(ast_method.attrs.as_slice()) {
encode_inlined_item(ecx, ebml_w,
IIMethodRef(local_def(parent_id), false,
&*ast_method));
Expand Down Expand Up @@ -1125,9 +1128,9 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_item_variances(ebml_w, ecx, item.id);
let trait_def = ty::lookup_trait_def(tcx, def_id);
encode_ty_type_param_defs(ebml_w, ecx,
trait_def.generics.type_param_defs(),
&trait_def.generics.types,
tag_items_data_item_ty_param_bounds);
encode_region_param_defs(ebml_w, trait_def.generics.region_param_defs());
encode_region_param_defs(ebml_w, &trait_def.generics.regions);
encode_trait_ref(ebml_w, ecx, &*trait_def.trait_ref, tag_item_trait_ref);
encode_name(ebml_w, item.ident.name);
encode_attributes(ebml_w, item.attrs.as_slice());
Expand Down
Loading

0 comments on commit e7f11f2

Please sign in to comment.