From 792436826892d61ca16797fe3d26b1f202f6702a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 31 Aug 2011 22:34:41 -0700 Subject: [PATCH] Allow istrs as patterns. Issue #855 --- src/comp/middle/trans_alt.rs | 14 +++++++++----- src/comp/syntax/parse/parser.rs | 16 ++++++++++++++++ src/test/run-pass/alt-str.rs | 34 ++++++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/comp/middle/trans_alt.rs b/src/comp/middle/trans_alt.rs index 76f4ef1ec0b..6dddf42caeb 100644 --- a/src/comp/middle/trans_alt.rs +++ b/src/comp/middle/trans_alt.rs @@ -296,9 +296,9 @@ fn compile_submatch(bcx: @block_ctxt, m: &match, vals: [ValueRef], let data = m[0].data; alt data.guard { some(e) { - let guard_cx = new_scope_block_ctxt(bcx, ~"guard"); - let next_cx = new_sub_block_ctxt(bcx, ~"next"); - let else_cx = new_sub_block_ctxt(bcx, ~"else"); + let guard_cx = new_scope_block_ctxt(bcx, ~"submatch_guard"); + let next_cx = new_sub_block_ctxt(bcx, ~"submatch_next"); + let else_cx = new_sub_block_ctxt(bcx, ~"submatch_else"); Br(bcx, guard_cx.llbb); // Temporarily set bindings. They'll be rewritten to PHI nodes for // the actual arm block. @@ -431,13 +431,17 @@ fn compile_submatch(bcx: @block_ctxt, m: &match, vals: [ValueRef], llvm::LLVMAddCase(sw, r.val, opt_cx.llbb); } compare. { + let compare_cx = new_scope_block_ctxt(bcx, ~"compare_scope"); + Br(bcx, compare_cx.llbb); + bcx = compare_cx; let r = trans_opt(bcx, opt); bcx = r.bcx; let t = ty::node_id_to_type(ccx.tcx, pat_id); let eq = trans::trans_compare(bcx, ast::eq, test_val, t, r.val, t); - bcx = new_sub_block_ctxt(bcx, ~"next"); - CondBr(eq.bcx, eq.val, opt_cx.llbb, bcx.llbb); + let cleanup_cx = trans::trans_block_cleanups(bcx, compare_cx); + bcx = new_sub_block_ctxt(bcx, ~"compare_next"); + CondBr(cleanup_cx, eq.val, opt_cx.llbb, bcx.llbb); } _ { } } diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 369e07080f4..9c61f569e21 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -1497,6 +1497,22 @@ fn parse_pat(p: &parser) -> @ast::pat { pat = ast::pat_tup(fields); } } + token::TILDE. { + p.bump(); + alt p.peek() { + token::LIT_STR(s) { + let sp = p.get_span(); + p.bump(); + let lit = + @{node: ast::lit_str(p.get_str(s), + ast::sk_unique), + span: sp}; + hi = lit.span.hi; + pat = ast::pat_lit(lit); + } + _ { p.fatal(~"expected string literal"); } + } + } tok { if !is_ident(tok) || is_word(p, ~"true") || is_word(p, ~"false") { let lit = parse_lit(p); diff --git a/src/test/run-pass/alt-str.rs b/src/test/run-pass/alt-str.rs index fbdf7fdbfe3..57e68552e80 100644 --- a/src/test/run-pass/alt-str.rs +++ b/src/test/run-pass/alt-str.rs @@ -1,15 +1,31 @@ // Issue #53 fn main() { - alt "test" { "not-test" { fail; } "test" { } _ { fail; } } - - tag t { tag1(str); tag2; } - - - alt tag1("test") { - tag2. { fail; } - tag1("not-test") { fail; } - tag1("test") { } + alt ~"test" { + ~"not-test" { fail; } + ~"test" { } _ { fail; } } + + tag t { tag1(istr); tag2; } + + + alt tag1(~"test") { + tag2. { fail; } + tag1(~"not-test") { fail; } + tag1(~"test") { } + _ { fail; } + } + + let x = alt ~"a" { + ~"a" { 1 } + ~"b" { 2 } + }; + assert x == 1; + + alt ~"a" { + ~"a" { } + ~"b" { } + } + }