From 6cb5c0980a14aaad0c676a88160726de52cac456 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 13 Jul 2011 10:50:16 +0200 Subject: [PATCH] box patterns, expect for the trans part --- src/comp/middle/alias.rs | 1 + src/comp/middle/resolve.rs | 3 +++ src/comp/middle/typeck.rs | 14 ++++++++++++++ src/comp/syntax/ast.rs | 2 ++ src/comp/syntax/fold.rs | 1 + src/comp/syntax/parse/parser.rs | 11 ++++++++--- src/comp/syntax/print/pprust.rs | 4 ++++ src/comp/syntax/visit.rs | 1 + src/comp/syntax/walk.rs | 1 + 9 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/comp/middle/alias.rs b/src/comp/middle/alias.rs index 126ad726c35..e5b42c7f907 100644 --- a/src/comp/middle/alias.rs +++ b/src/comp/middle/alias.rs @@ -312,6 +312,7 @@ fn arm_defnums(&ast::arm arm) -> vec[node_id] { case (ast::pat_rec(?fields, _)) { for (ast::field_pat f in fields) { walk_pat(found, f.pat); } } + case (ast::pat_box(?inner)) { walk_pat(found, inner); } case (_) { } } } diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 080d9270f6a..da7d4b1494f 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -311,6 +311,7 @@ fn resolve_names(&@env e, &@ast::crate c) { case (ast::pat_rec(?fields, _)) { for (ast::field_pat f in fields) { walk_pat(e, sc, f.pat); } } + case (ast::pat_box(?inner)) { walk_pat(e, sc, inner); } case (_) { } } } @@ -727,6 +728,7 @@ fn lookup_in_pat(&ident name, &ast::pat pat) -> option::t[def] { if (!option::is_none(found)) { ret found; } } } + case (ast::pat_box(?inner)) { ret lookup_in_pat(name, *inner); } } ret none[def]; } @@ -1268,6 +1270,7 @@ fn check_arm(@env e, &ast::arm a, &() x, &vt[()] v) { case (ast::pat_rec(?fields, _)) { for (ast::field_pat f in fields) { walk_pat(ch, f.pat); } } + case (ast::pat_box(?inner)) { walk_pat(ch, inner); } case (_) { } } } diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 7e0b2f3db55..9a7cf357996 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -1435,6 +1435,20 @@ fn check_pat(&@fn_ctxt fcx, &ast::pat_id_map map, &@ast::pat pat, } write::ty_only_fixup(fcx, pat.id, expected); } + case (ast::pat_box(?inner)) { + alt (structure_of(fcx, pat.span, expected)) { + case (ty::ty_box(?e_inner)) { + check_pat(fcx, map, inner, e_inner.ty); + write::ty_only_fixup(fcx, pat.id, expected); + } + case (_) { + fcx.ccx.tcx.sess.span_fatal + (pat.span, "mismatched types: expected " + + ty_to_str(fcx.ccx.tcx, expected) + + " found box"); + } + } + } } } diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index fb0f474c084..3280c50e3da 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -126,6 +126,7 @@ tag pat_ { pat_lit(@lit); pat_tag(path, (@pat)[]); pat_rec(field_pat[], bool); + pat_box(@pat); } type pat_id_map = std::map::hashmap[str, ast::node_id]; @@ -143,6 +144,7 @@ fn pat_id_map(&@pat pat) -> pat_id_map { pat_rec(?fields, _) { for (field_pat f in fields) { walk(map, f.pat); } } + pat_box(?inner) { walk(map, inner); } _ {} } } diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs index 93c51b9baf8..ca2688d33e9 100644 --- a/src/comp/syntax/fold.rs +++ b/src/comp/syntax/fold.rs @@ -292,6 +292,7 @@ fn noop_fold_pat(&pat_ p, ast_fold fld) -> pat_ { } pat_rec(fs, etc) } + case (pat_box(?inner)) { pat_box(fld.fold_pat(inner)) } }; } diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index ba6813cd6ce..fb9eca8640d 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -1459,6 +1459,12 @@ fn parse_pat(&parser p) -> @ast::pat { } } } + case (token::AT) { + p.bump(); + auto sub = parse_pat(p); + pat = ast::pat_box(sub); + hi = sub.span.hi; + } case (token::LBRACE) { p.bump(); auto fields = ~[]; @@ -2313,9 +2319,8 @@ fn parse_rest_import_name(&parser p, ast::ident first, } alt (p.peek()) { case (token::IDENT(_, _)) { identifiers += ~[parse_ident(p)]; } - case ( - //the lexer can't tell the different kinds of stars apart ) : - token::BINOP(token::STAR)) { + //the lexer can't tell the different kinds of stars apart ) : + case (token::BINOP(token::STAR)) { glob = true; p.bump(); } diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 8875208e2a8..df573020618 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -1136,6 +1136,10 @@ fn print_pat(&ps s, &@ast::pat pat) { } word(s.s, "}"); } + case (ast::pat_box(?inner)) { + word(s.s, "@"); + print_pat(s, inner); + } } s.ann.post(ann_node); } diff --git a/src/comp/syntax/visit.rs b/src/comp/syntax/visit.rs index 5198130c2fb..83bf548efee 100644 --- a/src/comp/syntax/visit.rs +++ b/src/comp/syntax/visit.rs @@ -200,6 +200,7 @@ fn visit_pat[E](&@pat p, &E e, &vt[E] v) { case (pat_rec(?fields, _)) { for (field_pat f in fields) { v.visit_pat(f.pat, e, v); } } + case (pat_box(?inner)) { v.visit_pat(inner, e, v); } case (_) { } } } diff --git a/src/comp/syntax/walk.rs b/src/comp/syntax/walk.rs index 540905893a7..ad2edb980b1 100644 --- a/src/comp/syntax/walk.rs +++ b/src/comp/syntax/walk.rs @@ -192,6 +192,7 @@ fn walk_pat(&ast_visitor v, &@ast::pat p) { case (ast::pat_rec(?fields, _)) { for (ast::field_pat f in fields) { walk_pat(v, f.pat); } } + case (ast::pat_box(?inner)) { walk_pat(v, inner); } case (_) { } } v.visit_pat_post(p);