diff options
| author | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-01-30 21:00:57 -0800 |
|---|---|---|
| committer | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-01-31 10:08:24 -0800 |
| commit | fba35e1a3c87892823d1f4d436b9f00a7864cf16 (patch) | |
| tree | 1fadaaee99ef266bd2f709fb2aa5577184ab611e /src/comp/syntax | |
| parent | 813a55d89135efb716dd80e96453a091a7cfc631 (diff) | |
| download | rust-fba35e1a3c87892823d1f4d436b9f00a7864cf16.tar.gz rust-fba35e1a3c87892823d1f4d436b9f00a7864cf16.zip | |
Require alts to be exhaustive
middle::check_alt does the work. Lots of changes to add default cases into alts that were previously inexhaustive.
Diffstat (limited to 'src/comp/syntax')
| -rw-r--r-- | src/comp/syntax/ast.rs | 4 | ||||
| -rw-r--r-- | src/comp/syntax/ast_util.rs | 35 | ||||
| -rw-r--r-- | src/comp/syntax/ext/simplext.rs | 13 | ||||
| -rw-r--r-- | src/comp/syntax/parse/parser.rs | 19 | ||||
| -rw-r--r-- | src/comp/syntax/print/pprust.rs | 33 | ||||
| -rw-r--r-- | src/comp/syntax/visit.rs | 1 |
6 files changed, 57 insertions, 48 deletions
diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index 1436f9b08f4..f52afc0875c 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -328,14 +328,10 @@ enum ty_ { ty_uniq(mt), ty_vec(mt), ty_ptr(mt), - ty_task, - ty_port(@ty), - ty_chan(@ty), ty_rec([ty_field]), ty_fn(proto, fn_decl), ty_tup([@ty]), ty_path(@path, node_id), - ty_type, ty_constr(@ty, [@ty_constr]), ty_mac(mac), // ty_infer means the type should be inferred instead of it having been diff --git a/src/comp/syntax/ast_util.rs b/src/comp/syntax/ast_util.rs index 68b157cecb8..baaa3022eb3 100644 --- a/src/comp/syntax/ast_util.rs +++ b/src/comp/syntax/ast_util.rs @@ -19,8 +19,10 @@ fn path_name_i(idents: [ident]) -> str { str::connect(idents, "::") } fn local_def(id: node_id) -> def_id { ret {crate: local_crate, node: id}; } -fn variant_def_ids(d: def) -> {tg: def_id, var: def_id} { - alt d { def_variant(enum_id, var_id) { ret {tg: enum_id, var: var_id}; } } +fn variant_def_ids(d: def) -> {enm: def_id, var: def_id} { + alt d { def_variant(enum_id, var_id) { + ret {enm: enum_id, var: var_id}; } + _ { fail "non-variant in variant_def_ids"; } } } fn def_id_of_def(d: def) -> def_id { @@ -77,6 +79,7 @@ fn is_path(e: @expr) -> bool { fn int_ty_to_str(t: int_ty) -> str { alt t { + ty_char { "u8" } // ??? ty_i { "" } ty_i8 { "i8" } ty_i16 { "i16" } ty_i32 { "i32" } ty_i64 { "i64" } } @@ -86,7 +89,7 @@ fn int_ty_max(t: int_ty) -> u64 { alt t { ty_i8 { 0x80u64 } ty_i16 { 0x800u64 } - ty_char | ty_i32 { 0x80000000u64 } + ty_i | ty_char | ty_i32 { 0x80000000u64 } // actually ni about ty_i ty_i64 { 0x8000000000000000u64 } } } @@ -102,7 +105,7 @@ fn uint_ty_max(t: uint_ty) -> u64 { alt t { ty_u8 { 0xffu64 } ty_u16 { 0xffffu64 } - ty_u32 { 0xffffffffu64 } + ty_u | ty_u32 { 0xffffffffu64 } // actually ni about ty_u ty_u64 { 0xffffffffffffffffu64 } } } @@ -223,12 +226,14 @@ fn eval_const_expr(e: @expr) -> const_val { const_float(f) { const_float(-f) } const_int(i) { const_int(-i) } const_uint(i) { const_uint(-i) } + _ { fail "eval_const_expr: bad neg argument"; } } } expr_unary(not, inner) { alt eval_const_expr(inner) { const_int(i) { const_int(!i) } const_uint(i) { const_uint(!i) } + _ { fail "eval_const_expr: bad not argument"; } } } expr_binary(op, a, b) { @@ -240,6 +245,7 @@ fn eval_const_expr(e: @expr) -> const_val { rem { const_float(a % b) } eq { fromb(a == b) } lt { fromb(a < b) } le { fromb(a <= b) } ne { fromb(a != b) } ge { fromb(a >= b) } gt { fromb(a > b) } + _ { fail "eval_const_expr: can't apply this binop to floats"; } } } (const_int(a), const_int(b)) { @@ -253,6 +259,7 @@ fn eval_const_expr(e: @expr) -> const_val { eq { fromb(a == b) } lt { fromb(a < b) } le { fromb(a <= b) } ne { fromb(a != b) } ge { fromb(a >= b) } gt { fromb(a > b) } + _ { fail "eval_const_expr: can't apply this binop to ints"; } } } (const_uint(a), const_uint(b)) { @@ -267,11 +274,17 @@ fn eval_const_expr(e: @expr) -> const_val { eq { fromb(a == b) } lt { fromb(a < b) } le { fromb(a <= b) } ne { fromb(a != b) } ge { fromb(a >= b) } gt { fromb(a > b) } + _ { fail "eval_const_expr: can't apply this binop to uints"; } } } + _ { fail "eval_constr_expr: bad binary arguments"; } } } expr_lit(lit) { lit_to_const(lit) } + // Precondition? + _ { + fail "eval_const_expr: non-constant expression"; + } } } @@ -324,6 +337,9 @@ fn compare_const_vals(a: const_val, b: const_val) -> int { 1 } } + _ { + fail "compare_const_vals: ill-typed comparison"; + } } } @@ -341,6 +357,17 @@ fn ident_to_path(s: span, i: ident) -> @path { @respan(s, {global: false, idents: [i], types: []}) } +pure fn is_unguarded(&&a: arm) -> bool { + alt a.guard { + none { true } + _ { false } + } +} + +pure fn unguarded_pat(a: arm) -> option::t<[@pat]> { + if is_unguarded(a) { some(a.pats) } else { none } +} + // Provides an extra node_id to hang callee information on, in case the // operator is deferred to a user-supplied method. The parser is responsible // for reserving this id. diff --git a/src/comp/syntax/ext/simplext.rs b/src/comp/syntax/ext/simplext.rs index 5477c495883..b7fdfc46908 100644 --- a/src/comp/syntax/ext/simplext.rs +++ b/src/comp/syntax/ext/simplext.rs @@ -4,6 +4,7 @@ import codemap::span; import core::{vec, option}; import std::map::{hashmap, new_str_hash}; import option::{some, none}; +import driver::session::session; import base::{ext_ctxt, normal}; @@ -460,11 +461,6 @@ fn p_t_s_rec(cx: ext_ctxt, m: matchable, s: selector, b: binders) { } } } - - - - - /* TODO: handle embedded types and blocks, at least */ expr_mac(mac) { p_t_s_r_mac(cx, mac, s, b); @@ -483,6 +479,9 @@ fn p_t_s_rec(cx: ext_ctxt, m: matchable, s: selector, b: binders) { } } } + _ { + cx.session().bug("undocumented invariant in p_t_s_rec"); + } } } @@ -718,6 +717,10 @@ fn add_new_extension(cx: ext_ctxt, sp: span, arg: @expr, // FIXME: check duplicates (or just simplify // the macro arg situation) } + _ { + cx.span_bug(mac.span, "undocumented invariant in \ + add_extension"); + } } } _ { diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 1c81eda7df5..95c37b9b8e9 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -258,7 +258,7 @@ fn check_bad_word(p: parser) { } } -fn parse_ty_fn(proto: ast::proto, p: parser) -> ast::ty_ { +fn parse_ty_fn(p: parser) -> ast::fn_decl { fn parse_fn_input_ty(p: parser) -> ast::arg { let mode = parse_arg_mode(p); let name = if is_plain_ident(p) && p.look_ahead(1u) == token::COLON { @@ -275,9 +275,9 @@ fn parse_ty_fn(proto: ast::proto, p: parser) -> ast::ty_ { // auto constrs = parse_constrs(~[], p); let constrs: [@ast::constr] = []; let (ret_style, ret_ty) = parse_ret_ty(p); - ret ast::ty_fn(proto, {inputs: inputs.node, output: ret_ty, + ret {inputs: inputs.node, output: ret_ty, purity: ast::impure_fn, cf: ret_style, - constraints: constrs}); + constraints: constrs}; } fn parse_ty_methods(p: parser) -> [ast::ty_method] { @@ -287,15 +287,10 @@ fn parse_ty_methods(p: parser) -> [ast::ty_method] { expect_word(p, "fn"); let ident = parse_method_name(p); let tps = parse_ty_params(p); - let f = parse_ty_fn(ast::proto_bare, p), fhi = p.last_span.hi; + let d = parse_ty_fn(p), fhi = p.last_span.hi; expect(p, token::SEMI); - alt f { - ast::ty_fn(_, d) { {ident: ident, attrs: attrs, decl: d, tps: tps, - span: ast_util::mk_sp(flo, fhi)} - } - } - }, p).node + span: ast_util::mk_sp(flo, fhi)}}, p).node } fn parse_mt(p: parser) -> ast::mt { @@ -506,10 +501,10 @@ fn parse_ty(p: parser, colons_before_params: bool) -> @ast::ty { ast::proto_bare { p.warn("fn is deprecated, use native fn"); } _ { /* fallthrough */ } } - t = parse_ty_fn(proto, p); + t = ast::ty_fn(proto, parse_ty_fn(p)); } else if eat_word(p, "native") { expect_word(p, "fn"); - t = parse_ty_fn(ast::proto_bare, p); + t = ast::ty_fn(ast::proto_bare, parse_ty_fn(p)); } else if p.token == token::MOD_SEP || is_ident(p.token) { let path = parse_path(p); t = ast::ty_path(path, p.get_id()); diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index c361beb21ce..fbfa5f4f433 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -336,17 +336,6 @@ fn print_type(s: ps, &&ty: @ast::ty) { word(s.s, "]"); } ast::ty_ptr(mt) { word(s.s, "*"); print_mt(s, mt); } - ast::ty_task { word(s.s, "task"); } - ast::ty_port(t) { - word(s.s, "port<"); - print_type(s, t); - word(s.s, ">"); - } - ast::ty_chan(t) { - word(s.s, "chan<"); - print_type(s, t); - word(s.s, ">"); - } ast::ty_rec(fields) { word(s.s, "{"); fn print_field(s: ps, f: ast::ty_field) { @@ -370,12 +359,18 @@ fn print_type(s: ps, &&ty: @ast::ty) { print_ty_fn(s, some(proto), d, none, none); } ast::ty_path(path, _) { print_path(s, path, false); } - ast::ty_type { word(s.s, "type"); } ast::ty_constr(t, cs) { print_type(s, t); space(s.s); word(s.s, ast_ty_constrs_str(cs)); } + ast::ty_mac(_) { + fail "print_type doesn't know how to print a ty_mac"; + } + ast::ty_infer { + fail "print_type shouldn't see a ty_infer"; + } + } end(s); } @@ -703,11 +698,6 @@ fn print_if(s: ps, test: @ast::expr, blk: ast::blk, alt els { some(_else) { alt _else.node { - - - - - // "another else-if" ast::expr_if(i, t, e) { cbox(s, indent_unit - 1u); @@ -718,11 +708,6 @@ fn print_if(s: ps, test: @ast::expr, blk: ast::blk, print_block(s, t); do_else(s, e); } - - - - - // "final else" ast::expr_block(b) { cbox(s, indent_unit - 1u); @@ -730,6 +715,10 @@ fn print_if(s: ps, test: @ast::expr, blk: ast::blk, word(s.s, " else "); print_block(s, b); } + // BLEAH, constraints would be great here + _ { + fail "print_if saw if with weird alternative"; + } } } _ {/* fall through */ } diff --git a/src/comp/syntax/visit.rs b/src/comp/syntax/visit.rs index 1fb6991256e..2ba0567c213 100644 --- a/src/comp/syntax/visit.rs +++ b/src/comp/syntax/visit.rs @@ -165,7 +165,6 @@ fn visit_ty<E>(t: @ty, e: E, v: vt<E>) { v.visit_ty(decl.output, e, v); } ty_path(p, _) { visit_path(p, e, v); } - ty_type {/* no-op */ } ty_constr(t, cs) { v.visit_ty(t, e, v); for tc: @spanned<constr_general_<@path, node_id>> in cs { |
