Skip to content

Commit acac6ab

Browse files
committed
Parse, store and print type parameter kind constraints.
1 parent 1836f59 commit acac6ab

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

src/comp/middle/resolve.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ fn lookup_in_ty_params(name: &ident, ty_params: &ast::ty_param[]) ->
710710
option::t[def] {
711711
let i = 0u;
712712
for tp: ast::ty_param in ty_params {
713-
if str::eq(tp, name) { ret some(ast::def_ty_arg(i)); }
713+
if str::eq(tp.ident, name) { ret some(ast::def_ty_arg(i)); }
714714
i += 1u;
715715
}
716716
ret none[def];
@@ -1215,22 +1215,30 @@ fn mie_span(mie: &mod_index_entry) -> span {
12151215
}
12161216

12171217
fn check_item(e: &@env, i: &@ast::item, x: &(), v: &vt[()]) {
1218+
fn typaram_names(tps: &ast::ty_param[]) -> ident[] {
1219+
let x: ast::ident[] = ~[];
1220+
for tp: ast::ty_param in tps { x += ~[tp.ident] }
1221+
ret x;
1222+
}
12181223
visit::visit_item(i, x, v);
12191224
alt i.node {
12201225
ast::item_fn(f, ty_params) {
12211226
check_fn(*e, i.span, f);
1222-
ensure_unique(*e, i.span, ty_params, ident_id, "type parameter");
1227+
ensure_unique(*e, i.span, typaram_names(ty_params),
1228+
ident_id, "type parameter");
12231229
}
12241230
ast::item_obj(ob, ty_params, _) {
12251231
fn field_name(field: &ast::obj_field) -> ident { ret field.ident; }
12261232
ensure_unique(*e, i.span, ob.fields, field_name, "object field");
12271233
for m: @ast::method in ob.methods {
12281234
check_fn(*e, m.span, m.node.meth);
12291235
}
1230-
ensure_unique(*e, i.span, ty_params, ident_id, "type parameter");
1236+
ensure_unique(*e, i.span, typaram_names(ty_params),
1237+
ident_id, "type parameter");
12311238
}
12321239
ast::item_tag(_, ty_params) {
1233-
ensure_unique(*e, i.span, ty_params, ident_id, "type parameter");
1240+
ensure_unique(*e, i.span, typaram_names(ty_params),
1241+
ident_id, "type parameter");
12341242
}
12351243
_ { }
12361244
}

src/comp/syntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type def_id = {crate: crate_num, node: node_id};
3131
const local_crate: crate_num = 0;
3232
fn local_def(id: node_id) -> def_id { ret {crate: local_crate, node: id}; }
3333

34-
type ty_param = ident;
34+
type ty_param = {ident: ident, kind: kind};
3535

3636
tag def {
3737
def_fn(def_id, purity);

src/comp/syntax/parse/parser.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,14 @@ fn parse_block_tail(p: &parser, lo: uint) -> ast::blk {
16961696
ret spanned(lo, hi, bloc);
16971697
}
16981698

1699-
fn parse_ty_param(p: &parser) -> ast::ty_param { ret parse_ident(p); }
1699+
fn parse_ty_param(p: &parser) -> ast::ty_param {
1700+
let k = alt p.peek() {
1701+
token::TILDE. { p.bump(); ast::kind_unique }
1702+
token::AT. { p.bump(); ast::kind_shared }
1703+
_ { ast::kind_pinned }
1704+
};
1705+
ret {ident: parse_ident(p), kind: k};
1706+
}
17001707

17011708
fn parse_ty_params(p: &parser) -> ast::ty_param[] {
17021709
let ty_params: ast::ty_param[] = ~[];

src/comp/syntax/print/pprust.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,9 @@ fn print_alias(s: &ps, m: ast::mode) {
11491149
fn print_type_params(s: &ps, params: &ast::ty_param[]) {
11501150
if ivec::len(params) > 0u {
11511151
word(s.s, "[");
1152-
fn printParam(s: &ps, param: &ast::ty_param) { word(s.s, param); }
1152+
fn printParam(s: &ps, param: &ast::ty_param) {
1153+
word(s.s, param.ident);
1154+
}
11531155
commasep(s, inconsistent, params, printParam);
11541156
word(s.s, "]");
11551157
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
fn p_foo[T](pinned: &T) { }
2+
fn s_foo[@T](shared: &T) { }
3+
fn u_foo[~T](unique: &T) { }
4+
5+
resource r(i: int) { }
6+
7+
fn main() {
8+
// FIXME: passing resources doesn't work?
9+
//p_foo(r(10));
10+
//p_foo(@r(10));
11+
// FIXME: unique boxes not yet supported.
12+
// p_foo(~r(10));
13+
p_foo(@10);
14+
// p_foo(~10);
15+
p_foo(10);
16+
17+
//s_foo(@r(10));
18+
//s_foo(~r(10));
19+
s_foo(@10);
20+
//s_foo(~10);
21+
s_foo(10);
22+
23+
//u_foo(~10);
24+
u_foo(10);
25+
}

0 commit comments

Comments
 (0)