Skip to content

Add an identifier to TypeParameterDefs and use it to pretty print type parameters #7698

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

Closed
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
3 changes: 2 additions & 1 deletion src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ pub fn parse_type_param_def_data(data: &[u8], start: uint,
}

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

Expand Down
2 changes: 2 additions & 0 deletions src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ fn enc_bounds(w: @io::Writer, cx: @ctxt, bs: &ty::ParamBounds) {
}

pub fn enc_type_param_def(w: @io::Writer, cx: @ctxt, v: &ty::TypeParameterDef) {
w.write_str(cx.tcx.sess.str_of(v.ident));
w.write_char(':');
w.write_str((cx.ds)(v.def_id));
w.write_char('|');
enc_bounds(w, cx, v.bounds);
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl Subst for ty::ParamBounds {
impl Subst for ty::TypeParameterDef {
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::TypeParameterDef {
ty::TypeParameterDef {
ident: self.ident,
def_id: self.def_id,
bounds: self.bounds.subst(tcx, substs)
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ impl ToStr for IntVarValue {
}

pub struct TypeParameterDef {
ident: ast::ident,
def_id: ast::def_id,
bounds: @ParamBounds
}
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use syntax::print::pprust::{path_to_str, explicit_self_to_str};
use syntax::visit;
use syntax::opt_vec::OptVec;
use syntax::opt_vec;
use syntax::parse::token::special_idents;

pub fn collect_item_types(ccx: @mut CrateCtxt, crate: &ast::crate) {
fn collect_intrinsic_type(ccx: &CrateCtxt,
Expand Down Expand Up @@ -318,6 +319,7 @@ pub fn ensure_trait_methods(ccx: &CrateCtxt,
let self_trait_def = get_trait_def(ccx, local_def(trait_id));
let self_trait_ref = self_trait_def.trait_ref.subst(tcx, &substs);
new_type_param_defs.push(ty::TypeParameterDef {
ident: special_idents::self_,
def_id: dummy_defid,
bounds: @ty::ParamBounds {
builtin_bounds: ty::EmptyBuiltinBounds(),
Expand Down Expand Up @@ -1151,6 +1153,7 @@ pub fn ty_generics(ccx: &CrateCtxt,
let bounds = @compute_bounds(ccx, rp, generics,
param_ty, &param.bounds);
let def = ty::TypeParameterDef {
ident: param.ident,
def_id: local_def(param.id),
bounds: bounds
};
Expand Down
21 changes: 11 additions & 10 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,17 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
ty_infer(infer_ty) => infer_ty.to_str(),
ty_err => ~"[type error]",
ty_param(param_ty {idx: id, def_id: did}) => {
let mut parm = (('T' as uint) + id) as char;
if (parm as uint) > ('Z' as uint) {
parm = (parm as uint - 26) as char;
}

if cx.sess.verbose() {
fmt!("%c:%?", parm, did)
} else {
fmt!("%c", parm)
}
let param_def = cx.ty_param_defs.find(&did.node);
let ident = match param_def {
Some(def) => {
cx.sess.str_of(def.ident).to_owned()
}
None => {
// This should not happen...
fmt!("BUG[%?]", id)
}
};
if !cx.sess.verbose() { ident } else { fmt!("%s:%?", ident, did) }
}
ty_self(*) => ~"Self",
ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
Expand Down
6 changes: 6 additions & 0 deletions src/test/compile-fail/type-parameter-names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Test that we print out the names of type parameters correctly in
// our error messages.

fn foo<Foo, Bar>(x: Foo) -> Bar { x } //~ ERROR expected `Bar` but found `Foo`

fn main() {}