Skip to content

Commit 4412df2

Browse files
committed
Add an identifier to TypeParameterDefs and use it to pretty print type parameters
1 parent e388a80 commit 4412df2

File tree

7 files changed

+26
-11
lines changed

7 files changed

+26
-11
lines changed

src/librustc/metadata/tydecode.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,8 @@ pub fn parse_type_param_def_data(data: &[u8], start: uint,
543543
}
544544

545545
fn parse_type_param_def(st: &mut PState, conv: conv_did) -> ty::TypeParameterDef {
546-
ty::TypeParameterDef {def_id: parse_def(st, NominalType, |x,y| conv(x,y)),
546+
ty::TypeParameterDef {ident: parse_ident(st, ':'),
547+
def_id: parse_def(st, NominalType, |x,y| conv(x,y)),
547548
bounds: @parse_bounds(st, |x,y| conv(x,y))}
548549
}
549550

src/librustc/metadata/tyencode.rs

+2
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ fn enc_bounds(w: @io::Writer, cx: @ctxt, bs: &ty::ParamBounds) {
416416
}
417417

418418
pub fn enc_type_param_def(w: @io::Writer, cx: @ctxt, v: &ty::TypeParameterDef) {
419+
w.write_str(cx.tcx.sess.str_of(v.ident));
420+
w.write_char(':');
419421
w.write_str((cx.ds)(v.def_id));
420422
w.write_char('|');
421423
enc_bounds(w, cx, v.bounds);

src/librustc/middle/subst.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ impl Subst for ty::ParamBounds {
130130
impl Subst for ty::TypeParameterDef {
131131
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::TypeParameterDef {
132132
ty::TypeParameterDef {
133+
ident: self.ident,
133134
def_id: self.def_id,
134135
bounds: self.bounds.subst(tcx, substs)
135136
}

src/librustc/middle/ty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ impl ToStr for IntVarValue {
794794
}
795795

796796
pub struct TypeParameterDef {
797+
ident: ast::ident,
797798
def_id: ast::def_id,
798799
bounds: @ParamBounds
799800
}

src/librustc/middle/typeck/collect.rs

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use syntax::print::pprust::{path_to_str, explicit_self_to_str};
5959
use syntax::visit;
6060
use syntax::opt_vec::OptVec;
6161
use syntax::opt_vec;
62+
use syntax::parse::token::special_idents;
6263

6364
pub fn collect_item_types(ccx: @mut CrateCtxt, crate: &ast::crate) {
6465
fn collect_intrinsic_type(ccx: &CrateCtxt,
@@ -318,6 +319,7 @@ pub fn ensure_trait_methods(ccx: &CrateCtxt,
318319
let self_trait_def = get_trait_def(ccx, local_def(trait_id));
319320
let self_trait_ref = self_trait_def.trait_ref.subst(tcx, &substs);
320321
new_type_param_defs.push(ty::TypeParameterDef {
322+
ident: special_idents::self_,
321323
def_id: dummy_defid,
322324
bounds: @ty::ParamBounds {
323325
builtin_bounds: ty::EmptyBuiltinBounds(),
@@ -1151,6 +1153,7 @@ pub fn ty_generics(ccx: &CrateCtxt,
11511153
let bounds = @compute_bounds(ccx, rp, generics,
11521154
param_ty, &param.bounds);
11531155
let def = ty::TypeParameterDef {
1156+
ident: param.ident,
11541157
def_id: local_def(param.id),
11551158
bounds: bounds
11561159
};

src/librustc/util/ppaux.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,17 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
435435
ty_infer(infer_ty) => infer_ty.to_str(),
436436
ty_err => ~"[type error]",
437437
ty_param(param_ty {idx: id, def_id: did}) => {
438-
let mut parm = (('T' as uint) + id) as char;
439-
if (parm as uint) > ('Z' as uint) {
440-
parm = (parm as uint - 26) as char;
441-
}
442-
443-
if cx.sess.verbose() {
444-
fmt!("%c:%?", parm, did)
445-
} else {
446-
fmt!("%c", parm)
447-
}
438+
let param_def = cx.ty_param_defs.find(&did.node);
439+
let ident = match param_def {
440+
Some(def) => {
441+
cx.sess.str_of(def.ident).to_owned()
442+
}
443+
None => {
444+
// This should not happen...
445+
fmt!("BUG[%?]", id)
446+
}
447+
};
448+
if !cx.sess.verbose() { ident } else { fmt!("%s:%?", ident, did) }
448449
}
449450
ty_self(*) => ~"Self",
450451
ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Test that we print out the names of type parameters correctly in
2+
// our error messages.
3+
4+
fn foo<Foo, Bar>(x: Foo) -> Bar { x } //~ ERROR expected `Bar` but found `Foo`
5+
6+
fn main() {}

0 commit comments

Comments
 (0)