Replace some explicit fails with 'alt check' invocations

This commit is contained in:
Marijn Haverbeke 2012-02-15 15:42:35 +01:00
parent 5b6eb67128
commit 4b63826050
10 changed files with 52 additions and 115 deletions

View file

@ -203,12 +203,11 @@ mod write {
let LLVMOptAggressive = 3 as c_int; // -O3
let CodeGenOptLevel;
alt opts.optimize {
alt check opts.optimize {
0u { CodeGenOptLevel = LLVMOptNone; }
1u { CodeGenOptLevel = LLVMOptLess; }
2u { CodeGenOptLevel = LLVMOptDefault; }
3u { CodeGenOptLevel = LLVMOptAggressive; }
_ { fail; }
}
let FileType;

View file

@ -39,9 +39,8 @@ fn lookup_defs(cstore: cstore::cstore, cnum: ast::crate_num,
fn lookup_method_purity(cstore: cstore::cstore, did: ast::def_id)
-> ast::purity {
let cdata = cstore::get_crate_data(cstore, did.crate).data;
alt decoder::lookup_def(did.crate, cdata, did) {
alt check decoder::lookup_def(did.crate, cdata, did) {
ast::def_fn(_, p) { p }
_ { fail; }
}
}

View file

@ -284,7 +284,7 @@ fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: ast::prim_ty, span: span)
option::none {}
}
let (name, (size, align), encoding) = alt ty {
let (name, (size, align), encoding) = alt check ty {
ast::ty_bool {("bool", size_and_align_of::<bool>(), DW_ATE_boolean)}
ast::ty_int(m) { alt m {
ast::ty_char {("char", size_and_align_of::<char>(), DW_ATE_unsigned)}
@ -306,7 +306,6 @@ fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: ast::prim_ty, span: span)
ast::ty_f32 {("f32", size_and_align_of::<f32>(), DW_ATE_float)}
ast::ty_f64 {("f64", size_and_align_of::<f64>(), DW_ATE_float)}
}}
_ { fail; }
};
let fname = filename_from_span(cx, span);
@ -511,9 +510,9 @@ fn create_vec(cx: @crate_ctxt, vec_t: ty::t, elem_t: ty::t,
fn member_size_and_align(tcx: ty::ctxt, ty: @ast::ty) -> (int, int) {
alt ty.node {
ast::ty_path(_, id) {
alt tcx.def_map.get(id) {
alt check tcx.def_map.get(id) {
ast::def_prim_ty(nty) {
alt nty {
alt check nty {
ast::ty_bool { size_and_align_of::<bool>() }
ast::ty_int(m) { alt m {
ast::ty_char { size_and_align_of::<char>() }
@ -535,10 +534,8 @@ fn member_size_and_align(tcx: ty::ctxt, ty: @ast::ty) -> (int, int) {
ast::ty_f32 { size_and_align_of::<f32>() }
ast::ty_f64 { size_and_align_of::<f64>() }
}}
_ { fail; }
}
}
_ { fail; }
}
}
ast::ty_box(_) | ast::ty_uniq(_) {

View file

@ -1965,12 +1965,8 @@ fn find_impls_in_view_item(e: env, vi: @ast::view_item,
todo(node_id, name, path, span, scopes) {
resolve_import(e, local_def(node_id), name, *path, span,
scopes);
alt e.imports.get(id) {
alt check e.imports.get(id) {
resolved(_, _, _, is, _, _) { act(is); }
_ {
e.sess.bug("Undocumented invariant in \
lookup_imported_impls");
}
}
}
_ {}
@ -2003,15 +1999,13 @@ fn find_impls_in_view_item(e: env, vi: @ast::view_item,
}
}
ast::view_item_import_glob(ids, id) {
alt e.imports.get(id) {
alt check e.imports.get(id) {
is_glob(path, sc, sp) {
alt follow_import(e, sc, *path, sp) {
some(def) { find_impls_in_mod(e, def, impls, none); }
_ {}
}
}
_ { e.sess.span_bug(vi.span, "Undocumented invariant in \
find_impls_in_view_item"); }
}
}
_ {}

View file

@ -2264,7 +2264,7 @@ fn monomorphic_fn(ccx: @crate_ctxt, fn_id: ast::def_id, substs: [ty::t],
ccx.monomorphized.insert(hash_id, {llfn: lldecl, fty: mono_ty});
let psubsts = some({tys: substs, dicts: dicts, bounds: tpt.bounds});
alt map_node {
alt check map_node {
ast_map::node_item(@{node: ast::item_fn(decl, _, body), _}, _) {
trans_fn(ccx, pt, decl, body, lldecl, no_self, [],
psubsts, fn_id.node);
@ -2285,7 +2285,6 @@ fn monomorphic_fn(ccx: @crate_ctxt, fn_id: ast::def_id, substs: [ty::t],
trans_fn(ccx, pt, mth.decl, mth.body, lldecl,
impl_self(selfty), [], psubsts, fn_id.node);
}
_ { fail; }
}
some({llfn: lldecl, fty: mono_ty})
}
@ -2622,7 +2621,7 @@ fn lval_maybe_callee_to_lval(c: lval_maybe_callee, ty: ty::t) -> lval_result {
add_clean_temp(bcx, space.val, ty);
{bcx: bcx, val: space.val, kind: temporary}
} else {
alt c.env {
alt check c.env {
is_closure { {bcx: c.bcx, val: c.val, kind: c.kind} }
null_env {
let llfnty = llvm::LLVMGetElementType(val_ty(c.val));
@ -2630,7 +2629,6 @@ fn lval_maybe_callee_to_lval(c: lval_maybe_callee, ty: ty::t) -> lval_result {
null_env_ptr(c.bcx));
{bcx: c.bcx, val: llfn, kind: temporary}
}
_ { fail; }
}
}
}
@ -4462,9 +4460,8 @@ fn trans_const(cx: @crate_ctxt, e: @ast::expr, id: ast::node_id) {
}
fn trans_item(ccx: @crate_ctxt, item: ast::item) {
let path = alt ccx.tcx.items.get(item.id) {
let path = alt check ccx.tcx.items.get(item.id) {
ast_map::node_item(_, p) { p }
_ { fail; }
};
alt item.node {
ast::item_fn(decl, tps, body) {
@ -4717,8 +4714,8 @@ fn collect_native_item(ccx: @crate_ctxt,
// For true external functions: create a rust wrapper
// and link to that. The rust wrapper will handle
// switching to the C stack.
let path = *alt ccx.tcx.items.get(i.id) {
ast_map::node_native_item(_, p) { p } _ { fail; }
let path = *alt check ccx.tcx.items.get(i.id) {
ast_map::node_native_item(_, p) { p }
} + [path_name(i.ident)];
register_fn(ccx, i.span, path, "native fn", tps, i.id);
}
@ -4729,8 +4726,8 @@ fn collect_native_item(ccx: @crate_ctxt,
}
fn item_path(ccx: @crate_ctxt, i: @ast::item) -> path {
*alt ccx.tcx.items.get(i.id) {
ast_map::node_item(_, p) { p } _ { fail; }
*alt check ccx.tcx.items.get(i.id) {
ast_map::node_item(_, p) { p }
} + [path_name(i.ident)]
}

View file

@ -154,11 +154,10 @@ fn trans_monomorphized_callee(bcx: @block_ctxt, callee_id: ast::node_id,
n_method, n_param, n_bound);
}
let mname = ty::iface_methods(tcx, iface_id)[n_method].ident;
let mth = alt tcx.items.get(impl_did.node) {
let mth = alt check tcx.items.get(impl_did.node) {
ast_map::node_item(@{node: ast::item_impl(_, _, _, ms), _}, _) {
option::get(vec::find(ms, {|m| m.ident == mname}))
}
_ { fail; }
};
ret trans_static_callee(bcx, callee_id, base,
ast_util::local_def(mth.id),

View file

@ -337,7 +337,7 @@ fn ast_ty_to_ty(tcx: ty::ctxt, mode: mode, &&ast_ty: @ast::ty) -> ty::t {
ty::mk_param(tcx, n, id)
}
ast::def_self(iface_id) {
alt tcx.items.get(iface_id.node) {
alt check tcx.items.get(iface_id.node) {
ast_map::node_item(@{node: ast::item_iface(tps, _), _}, _) {
if vec::len(tps) != vec::len(path.node.types) {
tcx.sess.span_err(ast_ty.span, "incorrect number of type \
@ -347,7 +347,6 @@ fn ast_ty_to_ty(tcx: ty::ctxt, mode: mode, &&ast_ty: @ast::ty) -> ty::t {
ast_ty_to_ty(tcx, mode, ast_ty)
}))
}
_ { fail; }
}
}
_ {
@ -780,13 +779,12 @@ mod collect {
}
}
fn ensure_iface_methods(tcx: ty::ctxt, id: ast::node_id) {
alt tcx.items.get(id) {
alt check tcx.items.get(id) {
ast_map::node_item(@{node: ast::item_iface(_, ms), _}, _) {
ty::store_iface_methods(tcx, id, @vec::map(ms, {|m|
ty_of_ty_method(tcx, m_collect, m)
}));
}
_ { fail; }
}
}
fn convert_class_item(_cx: @ctxt, _parent_ty: ty::t,
@ -1564,9 +1562,8 @@ fn require_pure_call(ccx: @crate_ctxt, caller_purity: ast::purity,
alt ccx.method_map.find(callee.id) {
some(method_static(did)) {
if did.crate == ast::local_crate {
alt ccx.tcx.items.get(did.node) {
alt check ccx.tcx.items.get(did.node) {
ast_map::node_method(m, _, _) { m.decl.purity }
_ { fail; }
}
} else {
csearch::lookup_method_purity(ccx.tcx.sess.cstore, did)
@ -1608,14 +1605,11 @@ fn check_expr_with(fcx: @fn_ctxt, expr: @ast::expr, expected: ty::t) -> bool {
fn impl_self_ty(tcx: ty::ctxt, did: ast::def_id) -> {n_tps: uint, ty: ty::t} {
if did.crate == ast::local_crate {
alt tcx.items.get(did.node) {
alt check tcx.items.get(did.node) {
ast_map::node_item(@{node: ast::item_impl(ts, _, st, _),
_}, _) {
{n_tps: vec::len(ts), ty: ast_ty_to_ty(tcx, m_check, st)}
}
an_item {
tcx.sess.bug("Undocumented invariant in impl_self_ty");
}
}
} else {
let tpt = csearch::get_type(tcx, did);
@ -1678,9 +1672,8 @@ fn lookup_method_inner(fcx: @fn_ctxt, expr: @ast::expr,
for bound in *tcx.ty_param_bounds.get(did.node) {
alt bound {
ty::bound_iface(t) {
let (iid, tps) = alt ty::get(t).struct {
let (iid, tps) = alt check ty::get(t).struct {
ty::ty_iface(i, tps) { (i, tps) }
_ { fail; }
};
let ifce_methods = ty::iface_methods(tcx, iid);
alt vec::position(*ifce_methods, {|m| m.ident == name}) {
@ -1727,21 +1720,17 @@ fn lookup_method_inner(fcx: @fn_ctxt, expr: @ast::expr,
fn ty_from_did(tcx: ty::ctxt, did: ast::def_id) -> ty::t {
if did.crate == ast::local_crate {
alt tcx.items.get(did.node) {
alt check tcx.items.get(did.node) {
ast_map::node_method(m, _, _) {
let mt = ty_of_method(tcx, m_check, m);
ty::mk_fn(tcx, {proto: ast::proto_box with mt.fty})
}
_ {
tcx.sess.bug("Undocumented invariant in ty_from_did");
}
}
} else {
alt ty::get(csearch::get_type(tcx, did).ty).struct {
alt check ty::get(csearch::get_type(tcx, did).ty).struct {
ty::ty_fn(fty) {
ty::mk_fn(tcx, {proto: ast::proto_box with fty})
}
_ { fail; }
}
}
}
@ -2550,9 +2539,7 @@ fn check_decl_initializer(fcx: @fn_ctxt, nid: ast::node_id,
fn check_decl_local(fcx: @fn_ctxt, local: @ast::local) -> bool {
let bot = false;
alt fcx.locals.find(local.node.id) {
some(i) {
let t = ty::mk_var(fcx.ccx.tcx, i);
let t = ty::mk_var(fcx.ccx.tcx, fcx.locals.get(local.node.id));
write_ty(fcx.ccx.tcx, local.node.id, t);
alt local.node.init {
some(init) {
@ -2562,10 +2549,6 @@ fn check_decl_local(fcx: @fn_ctxt, local: @ast::local) -> bool {
}
let id_map = pat_util::pat_id_map(fcx.ccx.tcx, local.node.pat);
check_pat(fcx, id_map, local.node.pat, t);
}
_ { fcx.ccx.tcx.sess.span_bug(local.span, "Undocumented invariant \
in check_decl_local"); }
}
ret bot;
}
@ -2985,10 +2968,8 @@ mod dict {
fn lookup_dict(fcx: @fn_ctxt, isc: resolve::iscopes, sp: span,
ty: ty::t, iface_ty: ty::t) -> dict_origin {
let tcx = fcx.ccx.tcx;
let (iface_id, iface_tps) = alt ty::get(iface_ty).struct {
let (iface_id, iface_tps) = alt check ty::get(iface_ty).struct {
ty::ty_iface(did, tps) { (did, tps) }
_ { tcx.sess.span_bug(sp, "Undocumented invariant in lookup\
_dict"); }
};
let ty = fixup_ty(fcx, sp, ty);
alt ty::get(ty).struct {
@ -2997,12 +2978,10 @@ mod dict {
for bound in *tcx.ty_param_bounds.get(did.node) {
alt bound {
ty::bound_iface(ity) {
alt ty::get(ity).struct {
alt check ty::get(ity).struct {
ty::ty_iface(idid, _) {
if iface_id == idid { ret dict_param(n, n_bound); }
}
_ { tcx.sess.span_bug(sp, "Undocumented invariant in \
lookup_dict"); }
}
n_bound += 1u;
}
@ -3020,11 +2999,8 @@ mod dict {
for im in *impls {
let match = alt ty::impl_iface(tcx, im.did) {
some(ity) {
alt ty::get(ity).struct {
alt check ty::get(ity).struct {
ty::ty_iface(id, _) { id == iface_id }
// Bleah, abstract this
_ { tcx.sess.span_bug(sp, "Undocumented invariant \
in lookup_dict"); }
}
}
_ { false }
@ -3086,15 +3062,11 @@ mod dict {
let tcx = fcx.ccx.tcx;
let ity = option::get(ty::impl_iface(tcx, impl_did));
let iface_ty = ty::substitute_type_params(tcx, impl_tys, ity);
alt ty::get(iface_ty).struct {
alt check ty::get(iface_ty).struct {
ty::ty_iface(_, tps) {
vec::iter2(tps, iface_tys,
{|a, b| demand::simple(fcx, sp, a, b);});
}
_ {
tcx.sess.span_bug(sp, "Undocumented invariant in \
connect_iface_tps");
}
}
}

View file

@ -395,7 +395,6 @@ mod tests {
option_missing(_) { assert (ft == option_missing_); }
option_duplicated(_) { assert (ft == option_duplicated_); }
unexpected_argument(_) { assert (ft == unexpected_argument_); }
_ { fail; }
}
}
@ -406,12 +405,11 @@ mod tests {
let args = ["--test=20"];
let opts = [reqopt("test")];
let rs = getopts(args, opts);
alt rs {
alt check rs {
ok(m) {
assert (opt_present(m, "test"));
assert (opt_str(m, "test") == "20");
}
_ { fail; }
}
}

View file

@ -243,7 +243,7 @@ fn fold_enum(
desc: attrs.desc,
variants: vec::map(doc.variants) {|variant|
let attrs = astsrv::exec(srv) {|ctxt|
alt ctxt.ast_map.get(doc.id) {
alt check ctxt.ast_map.get(doc.id) {
ast_map::node_item(@{
node: ast::item_enum(ast_variants, _), _
}, _) {
@ -254,7 +254,6 @@ fn fold_enum(
attr_parser::parse_variant(ast_variant.node.attrs)
}
_ { fail "fold_enum: undocumented invariant"; }
}
};

View file

@ -45,16 +45,13 @@ fn fold_fn(
fn get_fn_sig(srv: astsrv::srv, fn_id: doc::ast_id) -> option<str> {
astsrv::exec(srv) {|ctxt|
alt ctxt.ast_map.get(fn_id) {
alt check ctxt.ast_map.get(fn_id) {
ast_map::node_item(@{
ident: ident,
node: ast::item_fn(decl, _, blk), _
}, _) {
some(pprust::fun_to_str(decl, ident, []))
}
_ {
fail "get_fn_sig: undocumented invariant";
}
}
}
}
@ -83,13 +80,12 @@ fn merge_ret_ty(
fn get_ret_ty(srv: astsrv::srv, fn_id: doc::ast_id) -> option<str> {
astsrv::exec(srv) {|ctxt|
alt ctxt.ast_map.get(fn_id) {
alt check ctxt.ast_map.get(fn_id) {
ast_map::node_item(@{
node: ast::item_fn(decl, _, _), _
}, _) {
ret_ty_to_str(decl)
}
_ { fail "get_ret_ty: undocumented invariant"; }
}
}
}
@ -133,7 +129,7 @@ fn merge_arg_tys(
fn get_arg_tys(srv: astsrv::srv, fn_id: doc::ast_id) -> [(str, str)] {
astsrv::exec(srv) {|ctxt|
alt ctxt.ast_map.get(fn_id) {
alt check ctxt.ast_map.get(fn_id) {
ast_map::node_item(@{
node: ast::item_fn(decl, _, _), _
}, _) |
@ -142,9 +138,6 @@ fn get_arg_tys(srv: astsrv::srv, fn_id: doc::ast_id) -> [(str, str)] {
}, _) {
decl_arg_tys(decl)
}
_ {
fail "get_arg_tys: undocumented invariant";
}
}
}
}
@ -171,15 +164,12 @@ fn fold_const(
{
ty: some(astsrv::exec(srv) {|ctxt|
alt ctxt.ast_map.get(doc.id) {
alt check ctxt.ast_map.get(doc.id) {
ast_map::node_item(@{
node: ast::item_const(ty, _), _
}, _) {
pprust::ty_to_str(ty)
}
_ {
fail "fold_const: undocumented invariant";
}
}
})
with doc
@ -201,7 +191,7 @@ fn fold_enum(
{
variants: vec::map(doc.variants) {|variant|
let sig = astsrv::exec(srv) {|ctxt|
alt ctxt.ast_map.get(doc.id) {
alt check ctxt.ast_map.get(doc.id) {
ast_map::node_item(@{
node: ast::item_enum(ast_variants, _), _
}, _) {
@ -212,7 +202,6 @@ fn fold_enum(
pprust::variant_to_str(ast_variant)
}
_ { fail "fold_enum: undocumented invariant"; }
}
};
@ -240,13 +229,12 @@ fn fold_res(
{
args: merge_arg_tys(srv, doc.id, doc.args),
sig: some(astsrv::exec(srv) {|ctxt|
alt ctxt.ast_map.get(doc.id) {
alt check ctxt.ast_map.get(doc.id) {
ast_map::node_item(@{
node: ast::item_res(decl, _, _, _, _), _
}, _) {
pprust::res_to_str(decl, doc.name, [])
}
_ { fail "fold_res: undocumented invariant"; }
}
})
with doc
@ -325,25 +313,23 @@ fn get_method_ret_ty(
ast_map::node_item(@{
node: ast::item_iface(_, methods), _
}, _) {
alt vec::find(methods) {|method|
alt check vec::find(methods) {|method|
method.ident == method_name
} {
some(method) {
ret_ty_to_str(method.decl)
}
_ { fail "get_method_ret_ty: undocumented invariant"; }
}
}
ast_map::node_item(@{
node: ast::item_impl(_, _, _, methods), _
}, _) {
alt vec::find(methods) {|method|
alt check vec::find(methods) {|method|
method.ident == method_name
} {
some(method) {
ret_ty_to_str(method.decl)
}
_ { fail "get_method_ret_ty: undocumented invariant"; }
}
}
_ { fail }
@ -357,32 +343,29 @@ fn get_method_sig(
method_name: str
) -> option<str> {
astsrv::exec(srv) {|ctxt|
alt ctxt.ast_map.get(item_id) {
alt check ctxt.ast_map.get(item_id) {
ast_map::node_item(@{
node: ast::item_iface(_, methods), _
}, _) {
alt vec::find(methods) {|method|
alt check vec::find(methods) {|method|
method.ident == method_name
} {
some(method) {
some(pprust::fun_to_str(method.decl, method.ident, []))
}
_ { fail "get_method_sig: undocumented invariant"; }
}
}
ast_map::node_item(@{
node: ast::item_impl(_, _, _, methods), _
}, _) {
alt vec::find(methods) {|method|
alt check vec::find(methods) {|method|
method.ident == method_name
} {
some(method) {
some(pprust::fun_to_str(method.decl, method.ident, []))
}
_ { fail "get_method_sig: undocumented invariant"; }
}
}
_ { fail "get_method_sig: undocumented invariant"; }
}
}
}