diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2012-08-06 18:54:20 -0700 |
|---|---|---|
| committer | Patrick Walton <pcwalton@mimiga.net> | 2012-08-06 18:55:24 -0700 |
| commit | 60f47eabe2ef2730b98713dee2b5fd59513e8c6c (patch) | |
| tree | cf491427dd1bfac38de4fd93d934702db9669fda /src/libsyntax | |
| parent | c0f7ed68e235b61a2e9710864a2283f554bb470d (diff) | |
| download | rust-60f47eabe2ef2730b98713dee2b5fd59513e8c6c.tar.gz rust-60f47eabe2ef2730b98713dee2b5fd59513e8c6c.zip | |
rustc: Parse and stub (broken) typechecking for bounded function types
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ext/auto_serialize.rs | 14 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 37 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 19 |
6 files changed, 53 insertions, 32 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a325b8c634a..9bd6a5ccf7d 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -558,7 +558,7 @@ enum ty_ { ty_ptr(mt), ty_rptr(@region, mt), ty_rec(~[ty_field]), - ty_fn(proto, fn_decl), + ty_fn(proto, @~[ty_param_bound], fn_decl), ty_tup(~[@ty]), ty_path(@path, node_id), ty_fixed_length(@ty, option<uint>), diff --git a/src/libsyntax/ext/auto_serialize.rs b/src/libsyntax/ext/auto_serialize.rs index 2f44204c3a5..346b1cf5b59 100644 --- a/src/libsyntax/ext/auto_serialize.rs +++ b/src/libsyntax/ext/auto_serialize.rs @@ -186,10 +186,12 @@ impl helpers of ext_ctxt_helpers for ext_ctxt { }; @{id: self.next_id(), - node: ast::ty_fn(ast::proto_block, {inputs: args, - output: output, - purity: ast::impure_fn, - cf: ast::return_val}), + node: ast::ty_fn(ast::proto_block, + @~[], + {inputs: args, + output: output, + purity: ast::impure_fn, + cf: ast::return_val}), span: span} } @@ -441,7 +443,7 @@ fn ser_ty(cx: ext_ctxt, tps: ser_tps_map, ~[#ast[stmt]{$(s).emit_rec($(fld_lambda));}] } - ast::ty_fn(_, _) => { + ast::ty_fn(*) => { cx.span_err(ty.span, ~"cannot serialize function types"); ~[] } @@ -681,7 +683,7 @@ fn deser_ty(cx: ext_ctxt, tps: deser_tps_map, #ast{ $(d).read_rec($(fld_lambda)) } } - ast::ty_fn(_, _) => { + ast::ty_fn(*) => { #ast{ fail } } diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 57cebbf044a..7f2c2509f81 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -514,7 +514,10 @@ fn noop_fold_ty(t: ty_, fld: ast_fold) -> ty_ { ty_ptr(mt) => ty_ptr(fold_mt(mt, fld)), ty_rptr(region, mt) => ty_rptr(region, fold_mt(mt, fld)), ty_rec(fields) => ty_rec(vec::map(fields, |f| fold_field(f, fld))), - ty_fn(proto, decl) => ty_fn(proto, fold_fn_decl(decl, fld)), + ty_fn(proto, bounds, decl) => + ty_fn(proto, @vec::map(*bounds, + |x| fold_ty_param_bound(x, fld)), + fold_fn_decl(decl, fld)), ty_tup(tys) => ty_tup(vec::map(tys, |ty| fld.fold_ty(ty))), ty_path(path, id) => ty_path(fld.fold_path(path), fld.new_id(id)), ty_fixed_length(t, vs) => ty_fixed_length(fld.fold_ty(t), vs), diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 41b8b8f27d4..5b889b4ad96 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -50,10 +50,10 @@ import ast::{_mod, add, alt_check, alt_exhaustive, arg, arm, attribute, stmt_semi, subtract, sty_box, sty_by_ref, sty_region, sty_uniq, sty_value, token_tree, trait_method, trait_ref, tt_delim, tt_seq, tt_tok, tt_nonterminal, ty, ty_, ty_bot, ty_box, ty_field, ty_fn, - ty_infer, ty_mac, ty_method, ty_nil, ty_param, ty_path, ty_ptr, - ty_rec, ty_rptr, ty_tup, ty_u32, ty_uniq, ty_vec, - ty_fixed_length, unchecked_blk, uniq, unsafe_blk, unsafe_fn, - variant, view_item, view_item_, view_item_export, + ty_infer, ty_mac, ty_method, ty_nil, ty_param, ty_param_bound, + ty_path, ty_ptr, ty_rec, ty_rptr, ty_tup, ty_u32, ty_uniq, + ty_vec, ty_fixed_length, unchecked_blk, uniq, unsafe_blk, + unsafe_fn, variant, view_item, view_item_, view_item_export, view_item_import, view_item_use, view_path, view_path_glob, view_path_list, view_path_simple, visibility, vstore, vstore_box, vstore_fixed, vstore_slice, vstore_uniq}; @@ -240,14 +240,17 @@ class parser { fn get_id() -> node_id { next_node_id(self.sess) } fn parse_ty_fn(purity: ast::purity) -> ty_ { - let proto = if self.eat_keyword(~"extern") { + let proto, bounds; + if self.eat_keyword(~"extern") { self.expect_keyword(~"fn"); - ast::proto_bare + proto = ast::proto_bare; + bounds = @~[]; } else { self.expect_keyword(~"fn"); - self.parse_fn_ty_proto() + proto = self.parse_fn_ty_proto(); + bounds = self.parse_optional_ty_param_bounds(); }; - ty_fn(proto, self.parse_ty_fn_decl(purity)) + ty_fn(proto, bounds, self.parse_ty_fn_decl(purity)) } fn parse_ty_fn_decl(purity: ast::purity) -> fn_decl { @@ -467,7 +470,7 @@ class parser { self.parse_ty_fn(ast::impure_fn) } else if self.eat_keyword(~"extern") { self.expect_keyword(~"fn"); - ty_fn(proto_bare, self.parse_ty_fn_decl(ast::impure_fn)) + ty_fn(proto_bare, @~[], self.parse_ty_fn_decl(ast::impure_fn)) } else if self.token == token::MOD_SEP || is_ident(self.token) { let path = self.parse_path_with_tps(colons_before_params); ty_path(path, self.get_id()) @@ -2125,11 +2128,10 @@ class parser { return spanned(lo, hi, bloc); } - fn parse_ty_param() -> ty_param { + fn parse_optional_ty_param_bounds() -> @~[ty_param_bound] { let mut bounds = ~[]; - let ident = self.parse_ident(); if self.eat(token::COLON) { - while self.token != token::COMMA && self.token != token::GT { + while is_ident(self.token) { if self.eat_keyword(~"send") { push(bounds, bound_send); } else if self.eat_keyword(~"copy") { @@ -2139,10 +2141,17 @@ class parser { } else if self.eat_keyword(~"owned") { push(bounds, bound_owned); } else { - push(bounds, bound_trait(self.parse_ty(false))); } + push(bounds, bound_trait(self.parse_ty(false))); + } } } - return {ident: ident, id: self.get_id(), bounds: @bounds}; + return @move bounds; + } + + fn parse_ty_param() -> ty_param { + let ident = self.parse_ident(); + let bounds = self.parse_optional_ty_param_bounds(); + return {ident: ident, id: self.get_id(), bounds: bounds}; } fn parse_ty_params() -> ~[ty_param] { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 9d7731e8441..b31e63a9f68 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -399,8 +399,8 @@ fn print_type_ex(s: ps, &&ty: @ast::ty, print_colons: bool) { commasep(s, inconsistent, elts, print_type); pclose(s); } - ast::ty_fn(proto, d) => { - print_ty_fn(s, some(proto), d, none, none); + ast::ty_fn(proto, bounds, d) => { + print_ty_fn(s, some(proto), bounds, d, none, none); } ast::ty_path(path, _) => print_path(s, path, print_colons), ast::ty_fixed_length(t, v) => { @@ -702,7 +702,7 @@ fn print_ty_method(s: ps, m: ast::ty_method) { hardbreak_if_not_bol(s); maybe_print_comment(s, m.span.lo); print_outer_attributes(s, m.attrs); - print_ty_fn(s, none, m.decl, some(m.ident), some(m.tps)); + print_ty_fn(s, none, @~[], m.decl, some(m.ident), some(m.tps)); word(s.s, ~";"); } @@ -1645,10 +1645,12 @@ fn print_arg(s: ps, input: ast::arg) { } fn print_ty_fn(s: ps, opt_proto: option<ast::proto>, + bounds: @~[ast::ty_param_bound], decl: ast::fn_decl, id: option<ast::ident>, tps: option<~[ast::ty_param]>) { ibox(s, indent_unit); word(s.s, opt_proto_to_str(opt_proto)); + print_bounds(s, bounds); match id { some(id) => { word(s.s, ~" "); word(s.s, *id); } _ => () } match tps { some(tps) => print_type_params(s, tps), _ => () } zerobreak(s.s); diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index c988c89f8c2..08572e83f6e 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -195,8 +195,9 @@ fn visit_ty<E>(t: @ty, e: E, v: vt<E>) { ty_tup(ts) => for ts.each |tt| { v.visit_ty(tt, e, v); } - ty_fn(_, decl) => { + ty_fn(_, bounds, decl) => { for decl.inputs.each |a| { v.visit_ty(a.ty, e, v); } + visit_ty_param_bounds(bounds, e, v); v.visit_ty(decl.output, e, v); } ty_path(p, _) => visit_path(p, e, v), @@ -251,14 +252,18 @@ fn visit_foreign_item<E>(ni: @foreign_item, e: E, v: vt<E>) { } } +fn visit_ty_param_bounds<E>(bounds: @~[ty_param_bound], e: E, v: vt<E>) { + for vec::each(*bounds) |bound| { + match bound { + bound_trait(t) => v.visit_ty(t, e, v), + bound_copy | bound_send | bound_const | bound_owned => () + } + } +} + fn visit_ty_params<E>(tps: ~[ty_param], e: E, v: vt<E>) { for tps.each |tp| { - for vec::each(*tp.bounds) |bound| { - match bound { - bound_trait(t) => v.visit_ty(t, e, v), - bound_copy | bound_send | bound_const | bound_owned => () - } - } + visit_ty_param_bounds(tp.bounds, e, v); } } |
