Skip to content

Commit

Permalink
auto merge of #5802 : nikomatsakis/rust/issue-4183-trait-substs, r=ni…
Browse files Browse the repository at this point in the history
…komatsakis

Cleanup substitutions and treatment of generics around traits in a number of ways

- In a TraitRef, use the self type consistently to refer to the Self type:
  - trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
  - trait ref in `A:Trait` has the self type `A`
  - trait ref associated with a trait decl has self type `Self`
  - trait ref associated with a supertype has self type `Self`
  - trait ref in an object type `@Trait` has no self type

- Rewrite `each_bound_traits_and_supertraits` to perform
  substitutions as it goes, and thus yield a series of trait refs
  that are always in the same 'namespace' as the type parameter
  bound given as input.  Before, we left this to the caller, but
  this doesn't work because the caller lacks adequare information
  to perform the type substitutions correctly.

- For provided methods, substitute the generics involved in the provided
  method correctly.

- Introduce TypeParameterDef, which tracks the bounds declared on a type
  parameter and brings them together with the def_id and (in the future)
  other information (maybe even the parameter's name!).

- Introduce Subst trait, which helps to cleanup a lot of the
  repetitive code involved with doing type substitution.

- Introduce Repr trait, which makes debug printouts far more convenient.

Fixes #4183.  Needed for #5656.

r? @catamorphism
  • Loading branch information
bors committed Apr 10, 2013
2 parents 5e570ce + e8cd29b commit 92e265c
Show file tree
Hide file tree
Showing 32 changed files with 1,253 additions and 620 deletions.
2 changes: 1 addition & 1 deletion src/librustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub enum astencode_tag { // Reserves 0x50 -- 0x6f
tag_table_node_type_subst = 0x58,
tag_table_freevars = 0x59,
tag_table_tcache = 0x5a,
tag_table_param_bounds = 0x5b,
tag_table_param_defs = 0x5b,
tag_table_inferred_modes = 0x5c,
tag_table_mutbl = 0x5d,
tag_table_last_use = 0x5e,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub fn get_field_type(tcx: ty::ctxt, class_id: ast::def_id,
debug!("got field data %?", the_field);
let ty = decoder::item_type(def, the_field, tcx, cdata);
ty::ty_param_bounds_and_ty {
generics: ty::Generics {bounds: @~[],
generics: ty::Generics {type_param_defs: @~[],
region_param: None},
ty: ty
}
Expand Down
40 changes: 21 additions & 19 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use metadata::csearch::{ProvidedTraitMethodInfo, StaticMethodInfo};
use metadata::csearch;
use metadata::cstore;
use metadata::decoder;
use metadata::tydecode::{parse_ty_data, parse_def_id, parse_bounds_data,
use metadata::tydecode::{parse_ty_data, parse_def_id,
parse_type_param_def_data,
parse_bare_fn_ty_data, parse_trait_ref_data};
use middle::{ty, resolve};

Expand Down Expand Up @@ -266,13 +267,14 @@ fn item_trait_ref(doc: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> ty::TraitRef {
doc_trait_ref(tp, tcx, cdata)
}

fn item_ty_param_bounds(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd,
tag: uint)
-> @~[ty::param_bounds] {
fn item_ty_param_defs(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd,
tag: uint)
-> @~[ty::TypeParameterDef] {
let mut bounds = ~[];
for reader::tagged_docs(item, tag) |p| {
let bd = parse_bounds_data(p.data, p.start, cdata.cnum, tcx,
|_, did| translate_def_id(cdata, did));
let bd = parse_type_param_def_data(
p.data, p.start, cdata.cnum, tcx,
|_, did| translate_def_id(cdata, did));
bounds.push(bd);
}
@bounds
Expand Down Expand Up @@ -378,11 +380,11 @@ pub fn get_trait_def(cdata: cmd,
tcx: ty::ctxt) -> ty::TraitDef
{
let item_doc = lookup_item(item_id, cdata.data);
let tp_bounds = item_ty_param_bounds(item_doc, tcx, cdata,
tag_items_data_item_ty_param_bounds);
let tp_defs = item_ty_param_defs(item_doc, tcx, cdata,
tag_items_data_item_ty_param_bounds);
let rp = item_ty_region_param(item_doc);
ty::TraitDef {
generics: ty::Generics {bounds: tp_bounds,
generics: ty::Generics {type_param_defs: tp_defs,
region_param: rp},
trait_ref: @item_trait_ref(item_doc, tcx, cdata)
}
Expand All @@ -394,12 +396,12 @@ pub fn get_type(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
let item = lookup_item(id, cdata.data);
let t = item_type(ast::def_id { crate: cdata.cnum, node: id }, item, tcx,
cdata);
let tp_bounds = if family_has_type_params(item_family(item)) {
item_ty_param_bounds(item, tcx, cdata, tag_items_data_item_ty_param_bounds)
let tp_defs = if family_has_type_params(item_family(item)) {
item_ty_param_defs(item, tcx, cdata, tag_items_data_item_ty_param_bounds)
} else { @~[] };
let rp = item_ty_region_param(item);
ty::ty_param_bounds_and_ty {
generics: ty::Generics {bounds: tp_bounds,
generics: ty::Generics {type_param_defs: tp_defs,
region_param: rp},
ty: t
}
Expand Down Expand Up @@ -753,17 +755,16 @@ pub fn get_method(intr: @ident_interner, cdata: cmd, id: ast::node_id,
let method_doc = lookup_item(id, cdata.data);
let def_id = item_def_id(method_doc, cdata);
let name = item_name(intr, method_doc);
let bounds =
item_ty_param_bounds(method_doc, tcx, cdata,
tag_item_method_tps);
let type_param_defs = item_ty_param_defs(method_doc, tcx, cdata,
tag_item_method_tps);
let transformed_self_ty = doc_transformed_self_ty(method_doc, tcx, cdata);
let fty = doc_method_fty(method_doc, tcx, cdata);
let vis = item_visibility(method_doc);
let self_ty = get_self_ty(method_doc);
ty::method {
ident: name,
generics: ty::Generics {
bounds: bounds,
type_param_defs: type_param_defs,
region_param: None
},
transformed_self_ty: transformed_self_ty,
Expand Down Expand Up @@ -797,8 +798,9 @@ pub fn get_provided_trait_methods(intr: @ident_interner, cdata: cmd,

let did = item_def_id(mth, cdata);

let bounds = item_ty_param_bounds(mth, tcx, cdata,
tag_items_data_item_ty_param_bounds);
let type_param_defs =
item_ty_param_defs(mth, tcx, cdata,
tag_items_data_item_ty_param_bounds);
let name = item_name(intr, mth);
let ty = doc_type(mth, tcx, cdata);

Expand All @@ -815,7 +817,7 @@ pub fn get_provided_trait_methods(intr: @ident_interner, cdata: cmd,
let ty_method = ty::method {
ident: name,
generics: ty::Generics {
bounds: bounds,
type_param_defs: type_param_defs,
region_param: None
},
transformed_self_ty: transformed_self_ty,
Expand Down
28 changes: 15 additions & 13 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ fn encode_family(ebml_w: writer::Encoder, c: char) {

pub fn def_to_str(did: def_id) -> ~str { fmt!("%d:%d", did.crate, did.node) }

fn encode_ty_type_param_bounds(ebml_w: writer::Encoder,
ecx: @EncodeContext,
params: @~[ty::param_bounds],
tag: uint) {
fn encode_ty_type_param_defs(ebml_w: writer::Encoder,
ecx: @EncodeContext,
params: @~[ty::TypeParameterDef],
tag: uint) {
let ty_str_ctxt = @tyencode::ctxt {
diag: ecx.diag,
ds: def_to_str,
Expand All @@ -191,18 +191,18 @@ fn encode_ty_type_param_bounds(ebml_w: writer::Encoder,
abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)};
for params.each |param| {
ebml_w.start_tag(tag);
tyencode::enc_bounds(ebml_w.writer, ty_str_ctxt, *param);
tyencode::enc_type_param_def(ebml_w.writer, ty_str_ctxt, param);
ebml_w.end_tag();
}
}

fn encode_type_param_bounds(ebml_w: writer::Encoder,
ecx: @EncodeContext,
params: &OptVec<TyParam>) {
let ty_param_bounds =
@params.map_to_vec(|param| *ecx.tcx.ty_param_bounds.get(&param.id));
encode_ty_type_param_bounds(ebml_w, ecx, ty_param_bounds,
tag_items_data_item_ty_param_bounds);
let ty_param_defs =
@params.map_to_vec(|param| *ecx.tcx.ty_param_defs.get(&param.id));
encode_ty_type_param_defs(ebml_w, ecx, ty_param_defs,
tag_items_data_item_ty_param_bounds);
}


Expand Down Expand Up @@ -588,8 +588,9 @@ fn encode_method_ty_fields(ecx: @EncodeContext,
{
encode_def_id(ebml_w, method_ty.def_id);
encode_name(ecx, ebml_w, method_ty.ident);
encode_ty_type_param_bounds(ebml_w, ecx, method_ty.generics.bounds,
tag_item_method_tps);
encode_ty_type_param_defs(ebml_w, ecx,
method_ty.generics.type_param_defs,
tag_item_method_tps);
encode_transformed_self_ty(ecx, ebml_w, method_ty.transformed_self_ty);
encode_method_fty(ecx, ebml_w, &method_ty.fty);
encode_visibility(ebml_w, method_ty.vis);
Expand Down Expand Up @@ -952,8 +953,9 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
method_ty.fty.purity));

let tpt = ty::lookup_item_type(tcx, method_def_id);
encode_ty_type_param_bounds(ebml_w, ecx, tpt.generics.bounds,
tag_items_data_item_ty_param_bounds);
encode_ty_type_param_defs(ebml_w, ecx,
tpt.generics.type_param_defs,
tag_items_data_item_ty_param_bounds);
encode_type(ecx, ebml_w, tpt.ty);
}

Expand Down
14 changes: 10 additions & 4 deletions src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,17 @@ pub fn parse_def_id(buf: &[u8]) -> ast::def_id {
ast::def_id { crate: crate_num, node: def_num }
}

pub fn parse_bounds_data(data: @~[u8], start: uint,
crate_num: int, tcx: ty::ctxt, conv: conv_did)
-> @~[ty::param_bound] {
pub fn parse_type_param_def_data(data: @~[u8], start: uint,
crate_num: int, tcx: ty::ctxt,
conv: conv_did) -> ty::TypeParameterDef
{
let st = parse_state_from_data(data, crate_num, start, tcx);
parse_bounds(st, conv)
parse_type_param_def(st, conv)
}

fn parse_type_param_def(st: @mut PState, conv: conv_did) -> ty::TypeParameterDef {
ty::TypeParameterDef {def_id: parse_def(st, NominalType, conv),
bounds: parse_bounds(st, conv)}
}

fn parse_bounds(st: @mut PState, conv: conv_did) -> @~[ty::param_bound] {
Expand Down
8 changes: 7 additions & 1 deletion src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ fn enc_fn_sig(w: @io::Writer, cx: @ctxt, fsig: &ty::FnSig) {
enc_ty(w, cx, fsig.output);
}

pub fn enc_bounds(w: @io::Writer, cx: @ctxt, bs: @~[ty::param_bound]) {
fn enc_bounds(w: @io::Writer, cx: @ctxt, bs: @~[ty::param_bound]) {
for vec::each(*bs) |bound| {
match *bound {
ty::bound_owned => w.write_char('S'),
Expand All @@ -428,6 +428,12 @@ pub fn enc_bounds(w: @io::Writer, cx: @ctxt, bs: @~[ty::param_bound]) {
w.write_char('.');
}

pub fn enc_type_param_def(w: @io::Writer, cx: @ctxt, v: &ty::TypeParameterDef) {
w.write_str((cx.ds)(v.def_id));
w.write_char('|');
enc_bounds(w, cx, v.bounds);
}

//
// Local Variables:
// mode: rust
Expand Down
42 changes: 24 additions & 18 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,9 @@ trait ebml_writer_helpers {
fn emit_ty(&self, ecx: @e::EncodeContext, ty: ty::t);
fn emit_vstore(&self, ecx: @e::EncodeContext, vstore: ty::vstore);
fn emit_tys(&self, ecx: @e::EncodeContext, tys: ~[ty::t]);
fn emit_bounds(&self, ecx: @e::EncodeContext, bs: ty::param_bounds);
fn emit_type_param_def(&self,
ecx: @e::EncodeContext,
type_param_def: &ty::TypeParameterDef);
fn emit_tpbt(&self, ecx: @e::EncodeContext,
tpbt: ty::ty_param_bounds_and_ty);
}
Expand Down Expand Up @@ -771,9 +773,12 @@ impl ebml_writer_helpers for writer::Encoder {
}
}

fn emit_bounds(&self, ecx: @e::EncodeContext, bs: ty::param_bounds) {
fn emit_type_param_def(&self,
ecx: @e::EncodeContext,
type_param_def: &ty::TypeParameterDef) {
do self.emit_opaque {
tyencode::enc_bounds(self.writer, ecx.ty_str_ctxt(), bs)
tyencode::enc_type_param_def(self.writer, ecx.ty_str_ctxt(),
type_param_def)
}
}

Expand All @@ -782,9 +787,11 @@ impl ebml_writer_helpers for writer::Encoder {
do self.emit_struct("ty_param_bounds_and_ty", 2) {
do self.emit_field(~"generics", 0) {
do self.emit_struct("Generics", 2) {
do self.emit_field(~"bounds", 0) {
do self.emit_from_vec(*tpbt.generics.bounds) |bs| {
self.emit_bounds(ecx, *bs);
do self.emit_field(~"type_param_defs", 0) {
do self.emit_from_vec(*tpbt.generics.type_param_defs)
|type_param_def|
{
self.emit_type_param_def(ecx, type_param_def);
}
}
do self.emit_field(~"region_param", 1) {
Expand Down Expand Up @@ -889,11 +896,11 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
}
}

for tcx.ty_param_bounds.find(&id).each |&pbs| {
do ebml_w.tag(c::tag_table_param_bounds) {
for tcx.ty_param_defs.find(&id).each |&type_param_def| {
do ebml_w.tag(c::tag_table_param_defs) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
ebml_w.emit_bounds(ecx, *pbs)
ebml_w.emit_type_param_def(ecx, type_param_def)
}
}
}
Expand Down Expand Up @@ -990,7 +997,7 @@ trait ebml_decoder_decoder_helpers {
fn read_arg(&self, xcx: @ExtendedDecodeContext) -> ty::arg;
fn read_ty(&self, xcx: @ExtendedDecodeContext) -> ty::t;
fn read_tys(&self, xcx: @ExtendedDecodeContext) -> ~[ty::t];
fn read_bounds(&self, xcx: @ExtendedDecodeContext) -> @~[ty::param_bound];
fn read_type_param_def(&self, xcx: @ExtendedDecodeContext) -> ty::TypeParameterDef;
fn read_ty_param_bounds_and_ty(&self, xcx: @ExtendedDecodeContext)
-> ty::ty_param_bounds_and_ty;
fn convert_def_id(&self, xcx: @ExtendedDecodeContext,
Expand Down Expand Up @@ -1038,10 +1045,9 @@ impl ebml_decoder_decoder_helpers for reader::Decoder {
self.read_to_vec(|| self.read_ty(xcx) )
}

fn read_bounds(&self, xcx: @ExtendedDecodeContext)
-> @~[ty::param_bound] {
fn read_type_param_def(&self, xcx: @ExtendedDecodeContext) -> ty::TypeParameterDef {
do self.read_opaque |doc| {
tydecode::parse_bounds_data(
tydecode::parse_type_param_def_data(
doc.data, doc.start, xcx.dcx.cdata.cnum, xcx.dcx.tcx,
|s, a| self.convert_def_id(xcx, s, a))
}
Expand All @@ -1054,8 +1060,8 @@ impl ebml_decoder_decoder_helpers for reader::Decoder {
ty::ty_param_bounds_and_ty {
generics: do self.read_struct("Generics", 2) {
ty::Generics {
bounds: self.read_field(~"bounds", 0, || {
@self.read_to_vec(|| self.read_bounds(xcx) )
type_param_defs: self.read_field("type_param_defs", 0, || {
@self.read_to_vec(|| self.read_type_param_def(xcx))
}),
region_param: self.read_field(~"region_param", 1, || {
Decodable::decode(self)
Expand Down Expand Up @@ -1134,9 +1140,9 @@ fn decode_side_tables(xcx: @ExtendedDecodeContext,
let tpbt = val_dsr.read_ty_param_bounds_and_ty(xcx);
let lid = ast::def_id { crate: ast::local_crate, node: id };
dcx.tcx.tcache.insert(lid, tpbt);
} else if tag == (c::tag_table_param_bounds as uint) {
let bounds = val_dsr.read_bounds(xcx);
dcx.tcx.ty_param_bounds.insert(id, bounds);
} else if tag == (c::tag_table_param_defs as uint) {
let bounds = val_dsr.read_type_param_def(xcx);
dcx.tcx.ty_param_defs.insert(id, bounds);
} else if tag == (c::tag_table_last_use as uint) {
let ids = val_dsr.read_to_vec(|| {
xcx.tr_id(val_dsr.read_int())
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/borrowck/gather_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use middle::pat_util;
use middle::ty::{ty_region};
use middle::ty;
use util::common::indenter;
use util::ppaux::{expr_repr, region_to_str};
use util::ppaux::{Repr, region_to_str};

use core::hashmap::{HashSet, HashMap};
use core::vec;
Expand Down Expand Up @@ -282,7 +282,7 @@ pub impl GatherLoanCtxt {
expr: @ast::expr,
adjustment: &ty::AutoAdjustment) {
debug!("guarantee_adjustments(expr=%s, adjustment=%?)",
expr_repr(self.tcx(), expr), adjustment);
expr.repr(self.tcx()), adjustment);
let _i = indenter();

match *adjustment {
Expand Down
Loading

0 comments on commit 92e265c

Please sign in to comment.