Parse, store and print type parameter kind constraints.
This commit is contained in:
parent
1836f59d9a
commit
acac6abc85
@ -710,7 +710,7 @@ fn lookup_in_ty_params(name: &ident, ty_params: &ast::ty_param[]) ->
|
||||
option::t[def] {
|
||||
let i = 0u;
|
||||
for tp: ast::ty_param in ty_params {
|
||||
if str::eq(tp, name) { ret some(ast::def_ty_arg(i)); }
|
||||
if str::eq(tp.ident, name) { ret some(ast::def_ty_arg(i)); }
|
||||
i += 1u;
|
||||
}
|
||||
ret none[def];
|
||||
@ -1215,11 +1215,17 @@ fn mie_span(mie: &mod_index_entry) -> span {
|
||||
}
|
||||
|
||||
fn check_item(e: &@env, i: &@ast::item, x: &(), v: &vt[()]) {
|
||||
fn typaram_names(tps: &ast::ty_param[]) -> ident[] {
|
||||
let x: ast::ident[] = ~[];
|
||||
for tp: ast::ty_param in tps { x += ~[tp.ident] }
|
||||
ret x;
|
||||
}
|
||||
visit::visit_item(i, x, v);
|
||||
alt i.node {
|
||||
ast::item_fn(f, ty_params) {
|
||||
check_fn(*e, i.span, f);
|
||||
ensure_unique(*e, i.span, ty_params, ident_id, "type parameter");
|
||||
ensure_unique(*e, i.span, typaram_names(ty_params),
|
||||
ident_id, "type parameter");
|
||||
}
|
||||
ast::item_obj(ob, ty_params, _) {
|
||||
fn field_name(field: &ast::obj_field) -> ident { ret field.ident; }
|
||||
@ -1227,10 +1233,12 @@ fn check_item(e: &@env, i: &@ast::item, x: &(), v: &vt[()]) {
|
||||
for m: @ast::method in ob.methods {
|
||||
check_fn(*e, m.span, m.node.meth);
|
||||
}
|
||||
ensure_unique(*e, i.span, ty_params, ident_id, "type parameter");
|
||||
ensure_unique(*e, i.span, typaram_names(ty_params),
|
||||
ident_id, "type parameter");
|
||||
}
|
||||
ast::item_tag(_, ty_params) {
|
||||
ensure_unique(*e, i.span, ty_params, ident_id, "type parameter");
|
||||
ensure_unique(*e, i.span, typaram_names(ty_params),
|
||||
ident_id, "type parameter");
|
||||
}
|
||||
_ { }
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ type def_id = {crate: crate_num, node: node_id};
|
||||
const local_crate: crate_num = 0;
|
||||
fn local_def(id: node_id) -> def_id { ret {crate: local_crate, node: id}; }
|
||||
|
||||
type ty_param = ident;
|
||||
type ty_param = {ident: ident, kind: kind};
|
||||
|
||||
tag def {
|
||||
def_fn(def_id, purity);
|
||||
|
@ -1696,7 +1696,14 @@ fn parse_block_tail(p: &parser, lo: uint) -> ast::blk {
|
||||
ret spanned(lo, hi, bloc);
|
||||
}
|
||||
|
||||
fn parse_ty_param(p: &parser) -> ast::ty_param { ret parse_ident(p); }
|
||||
fn parse_ty_param(p: &parser) -> ast::ty_param {
|
||||
let k = alt p.peek() {
|
||||
token::TILDE. { p.bump(); ast::kind_unique }
|
||||
token::AT. { p.bump(); ast::kind_shared }
|
||||
_ { ast::kind_pinned }
|
||||
};
|
||||
ret {ident: parse_ident(p), kind: k};
|
||||
}
|
||||
|
||||
fn parse_ty_params(p: &parser) -> ast::ty_param[] {
|
||||
let ty_params: ast::ty_param[] = ~[];
|
||||
|
@ -1149,7 +1149,9 @@ fn print_alias(s: &ps, m: ast::mode) {
|
||||
fn print_type_params(s: &ps, params: &ast::ty_param[]) {
|
||||
if ivec::len(params) > 0u {
|
||||
word(s.s, "[");
|
||||
fn printParam(s: &ps, param: &ast::ty_param) { word(s.s, param); }
|
||||
fn printParam(s: &ps, param: &ast::ty_param) {
|
||||
word(s.s, param.ident);
|
||||
}
|
||||
commasep(s, inconsistent, params, printParam);
|
||||
word(s.s, "]");
|
||||
}
|
||||
|
25
src/test/run-pass/type-param-constraints.rs
Normal file
25
src/test/run-pass/type-param-constraints.rs
Normal file
@ -0,0 +1,25 @@
|
||||
fn p_foo[T](pinned: &T) { }
|
||||
fn s_foo[@T](shared: &T) { }
|
||||
fn u_foo[~T](unique: &T) { }
|
||||
|
||||
resource r(i: int) { }
|
||||
|
||||
fn main() {
|
||||
// FIXME: passing resources doesn't work?
|
||||
//p_foo(r(10));
|
||||
//p_foo(@r(10));
|
||||
// FIXME: unique boxes not yet supported.
|
||||
// p_foo(~r(10));
|
||||
p_foo(@10);
|
||||
// p_foo(~10);
|
||||
p_foo(10);
|
||||
|
||||
//s_foo(@r(10));
|
||||
//s_foo(~r(10));
|
||||
s_foo(@10);
|
||||
//s_foo(~10);
|
||||
s_foo(10);
|
||||
|
||||
//u_foo(~10);
|
||||
u_foo(10);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user