Skip to content

Commit fba35e1

Browse files
committed
Require alts to be exhaustive
middle::check_alt does the work. Lots of changes to add default cases into alts that were previously inexhaustive.
1 parent 813a55d commit fba35e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+766
-367
lines changed

src/cargo/cargo.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ fn load_link(mis: [@ast::meta_item]) -> (option::t<str>,
9393
_ { }
9494
}
9595
}
96+
_ { fail "load_link: meta items must be name-values"; }
9697
}
9798
}
9899
(name, vers, uuid)
@@ -135,6 +136,7 @@ fn load_pkg(filename: str) -> option::t<pkg> {
135136
uuid = u;
136137
}
137138
}
139+
_ { fail "load_pkg: pkg attributes may not contain meta_words"; }
138140
}
139141
}
140142

src/comp/back/rpath.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import util::filesearch;
1111

1212
export get_rpath_flags;
1313

14+
pure fn not_win32(os: session::os) -> bool {
15+
alt os {
16+
session::os_win32 { false }
17+
_ { true }
18+
}
19+
}
20+
1421
fn get_rpath_flags(sess: session::session, out_filename: str) -> [str] {
1522
let os = sess.targ_cfg.os;
1623

@@ -99,12 +106,13 @@ fn get_rpaths_relative_to_output(os: session::os,
99106
fn get_rpath_relative_to_output(os: session::os,
100107
cwd: fs::path,
101108
output: fs::path,
102-
&&lib: fs::path) -> str {
109+
&&lib: fs::path) : not_win32(os) -> str {
103110
// Mac doesn't appear to support $ORIGIN
104111
let prefix = alt os {
105112
session::os_linux { "$ORIGIN" + fs::path_sep() }
106113
session::os_freebsd { "$ORIGIN" + fs::path_sep() }
107114
session::os_macos { "@executable_path" + fs::path_sep() }
115+
session::os_win32 { std::util::unreachable(); }
108116
};
109117

110118
prefix + get_relative_to(
@@ -309,24 +317,31 @@ mod test {
309317
#[test]
310318
#[cfg(target_os = "linux")]
311319
fn test_rpath_relative() {
312-
let res = get_rpath_relative_to_output(session::os_linux,
320+
let o = session::os_linux;
321+
check not_win32(o);
322+
let res = get_rpath_relative_to_output(o,
313323
"/usr", "bin/rustc", "lib/libstd.so");
314-
assert res == "$ORIGIN/../lib";
324+
assert res == "$ORIGIN/../lib";
315325
}
316326

317327
#[test]
318328
#[cfg(target_os = "freebsd")]
319329
fn test_rpath_relative() {
320-
let res = get_rpath_relative_to_output(session::os_freebsd,
330+
let o = session::os_freebsd;
331+
check not_win32(o);
332+
let res = get_rpath_relative_to_output(o,
321333
"/usr", "bin/rustc", "lib/libstd.so");
322-
assert res == "$ORIGIN/../lib";
334+
assert res == "$ORIGIN/../lib";
323335
}
324336

325337
#[test]
326338
#[cfg(target_os = "macos")]
327339
fn test_rpath_relative() {
328-
let res = get_rpath_relative_to_output(session::os_macos,
329-
"/usr", "bin/rustc", "lib/libstd.so");
340+
// this is why refinements would be nice
341+
let o = session::os_macos;
342+
check not_win32(o);
343+
let res = get_rpath_relative_to_output(o, "/usr", "bin/rustc",
344+
"lib/libstd.so");
330345
assert res == "@executable_path/../lib";
331346
}
332347

src/comp/driver/driver.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,8 @@ mod test {
608608
let match =
609609
alt getopts::getopts(["--test"], opts()) {
610610
ok(m) { m }
611+
err(f) { fail "test_switch_implies_cfg_test: " +
612+
getopts::fail_str(f); }
611613
};
612614
let sessopts = build_session_options(match, diagnostic::emit);
613615
let sess = build_session(sessopts, "", diagnostic::emit);
@@ -622,6 +624,8 @@ mod test {
622624
let match =
623625
alt getopts::getopts(["--test", "--cfg=test"], opts()) {
624626
ok(m) { m }
627+
err(f) { fail "test_switch_implies_cfg_test_unless_cfg_test: " +
628+
getopts::fail_str(f); }
625629
};
626630
let sessopts = build_session_options(match, diagnostic::emit);
627631
let sess = build_session(sessopts, "", diagnostic::emit);

src/comp/driver/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type session = @{targ_cfg: @config,
5858
parse_sess: parse_sess,
5959
codemap: codemap::codemap,
6060
// For a library crate, this is always none
61-
mutable main_fn: option::t<node_id>,
61+
mutable main_fn: option::t<(node_id, codemap::span)>,
6262
span_diagnostic: diagnostic::span_handler,
6363
filesearch: filesearch::filesearch,
6464
mutable building_library: bool,

src/comp/metadata/decoder.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import std::{ebml, map, io};
44
import io::writer_util;
55
import syntax::{ast, ast_util};
6+
import driver::session::session;
67
import front::attr;
78
import middle::ty;
89
import common::*;
@@ -302,7 +303,9 @@ fn get_iface_methods(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
302303
let bounds = item_ty_param_bounds(mth, tcx, cdata);
303304
let name = item_name(mth);
304305
let ty = doc_type(mth, tcx, cdata);
305-
let fty = alt ty::struct(tcx, ty) { ty::ty_fn(f) { f } };
306+
let fty = alt ty::struct(tcx, ty) { ty::ty_fn(f) { f }
307+
_ { tcx.sess.bug("get_iface_methods: id has non-function type");
308+
} };
306309
result += [{ident: name, tps: bounds, fty: fty}];
307310
}
308311
@result

src/comp/metadata/encoder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import middle::trans::common::crate_ctxt;
1010
import middle::ty;
1111
import middle::ty::node_id_to_type;
1212
import front::attr;
13+
import driver::session::session;
1314

1415
export encode_metadata;
1516
export encoded_ty;
@@ -275,6 +276,8 @@ fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: ebml::writer, md: _mod,
275276
}
276277
}
277278
}
279+
_ { ecx.ccx.tcx.sess.bug("encode_info_for_mod: \
280+
undocumented invariant"); }
278281
}
279282
ebml::end_tag(ebml_w);
280283
}

src/comp/metadata/tyencode.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io::writer_util;
55
import std::map::hashmap;
66
import option::{some, none};
77
import syntax::ast::*;
8+
import driver::session::session;
89
import middle::ty;
910
import syntax::print::pprust::*;
1011

@@ -213,6 +214,10 @@ fn enc_ty_fn(w: io::writer, cx: @ctxt, ft: ty::fn_ty) {
213214
by_copy { w.write_char('+'); }
214215
by_ref { w.write_char('='); }
215216
by_val { w.write_char('#'); }
217+
// tediously, this has to be there until there's a way
218+
// to constraint post-typeck types not to contain a mode_infer
219+
mode_infer { cx.tcx.sess.bug("enc_ty_fn: shouldn't see \
220+
mode_infer"); }
216221
}
217222
enc_ty(w, cx, arg.ty);
218223
}

src/comp/middle/alias.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import syntax::visit;
66
import visit::vt;
77
import core::{vec, option};
88
import std::list;
9+
import std::util::unreachable;
910
import option::{some, none, is_none};
1011
import list::list;
1112
import driver::session::session;
1213
import pat_util::*;
14+
import util::ppaux::ty_to_str;
1315

1416
// This is not an alias-analyser (though it would merit from becoming one, or
1517
// getting input from one, to be more precise). It is a pass that checks
@@ -575,6 +577,11 @@ fn copy_is_expensive(tcx: ty::ctxt, ty: ty::t) -> bool {
575577
for f in fs { sum += score_ty(tcx, f.mt.ty); }
576578
sum
577579
}
580+
_ {
581+
tcx.sess.warn(#fmt("score_ty: unexpected type %s",
582+
ty_to_str(tcx, ty)));
583+
1u // ???
584+
}
578585
};
579586
}
580587
ret score_ty(tcx, ty) > 8u;
@@ -611,6 +618,7 @@ fn pattern_roots(tcx: ty::ctxt, mut: option::t<unsafe_ty>, pat: @ast::pat)
611618
let ty = ty::node_id_to_type(tcx, pat.id);
612619
let m = alt ty::struct(tcx, ty) {
613620
ty::ty_box(mt) { mt.mut != ast::imm }
621+
_ { tcx.sess.span_bug(pat.span, "box pat has non-box type"); }
614622
},
615623
c = if m {some(contains(ty)) } else { mut };
616624
walk(tcx, c, p, set);
@@ -619,6 +627,7 @@ fn pattern_roots(tcx: ty::ctxt, mut: option::t<unsafe_ty>, pat: @ast::pat)
619627
let ty = ty::node_id_to_type(tcx, pat.id);
620628
let m = alt ty::struct(tcx, ty) {
621629
ty::ty_uniq(mt) { mt.mut != ast::imm }
630+
_ { tcx.sess.span_bug(pat.span, "uniq pat has non-uniq type"); }
622631
},
623632
c = if m { some(contains(ty)) } else { mut };
624633
walk(tcx, c, p, set);
@@ -672,6 +681,10 @@ fn append_invalid(dest: list<@invalid>, src: list<@invalid>,
672681
}
673682
cur = *tail;
674683
}
684+
list::nil {
685+
fail "append_invalid: stop doesn't appear to be \
686+
a postfix of src";
687+
}
675688
}
676689
}
677690
ret dest;
@@ -686,6 +699,10 @@ fn filter_invalid(src: list<@invalid>, bs: [binding]) -> list<@invalid> {
686699
if !is_none(p) { out = list::cons(head, @out); }
687700
cur = *tail;
688701
}
702+
list::nil {
703+
// typestate would help...
704+
unreachable();
705+
}
689706
}
690707
}
691708
ret out;

src/comp/middle/ast_map.rs

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ fn map_fn(cx: ctx, _fk: visit::fn_kind, decl: fn_decl, _body: blk,
4444
}
4545

4646
fn map_local(cx: ctx, loc: @local) {
47-
pat_util::pat_bindings(loc.node.pat) {|p|
48-
cx.map.insert(p.id, node_local(cx.local_id));
47+
pat_util::pat_bindings(loc.node.pat) {|p_id, _s, _p|
48+
cx.map.insert(p_id, node_local(cx.local_id));
4949
cx.local_id += 1u;
5050
};
5151
}
5252

5353
fn map_arm(cx: ctx, arm: arm) {
54-
pat_util::pat_bindings(arm.pats[0]) {|p|
55-
cx.map.insert(p.id, node_local(cx.local_id));
54+
pat_util::pat_bindings(arm.pats[0]) {|p_id, _s, _p|
55+
cx.map.insert(p_id, node_local(cx.local_id));
5656
cx.local_id += 1u;
5757
};
5858
}
@@ -79,50 +79,6 @@ fn map_expr(cx: ctx, ex: @expr) {
7979
cx.map.insert(ex.id, node_expr(ex));
8080
}
8181

82-
fn node_span(node: ast_node) -> codemap::span {
83-
alt node {
84-
node_item(item) { item.span }
85-
node_native_item(nitem) { nitem.span }
86-
node_expr(expr) { expr.span }
87-
}
88-
}
89-
90-
#[cfg(test)]
91-
mod test {
92-
import syntax::ast_util;
93-
94-
#[test]
95-
fn test_node_span_item() {
96-
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
97-
let node =
98-
node_item(@{ident: "test",
99-
attrs: [],
100-
id: 0,
101-
node: item_mod({view_items: [], items: []}),
102-
span: expected});
103-
assert (node_span(node) == expected);
104-
}
105-
106-
#[test]
107-
fn test_node_span_native_item() {
108-
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
109-
let node =
110-
node_native_item(@{ident: "test",
111-
attrs: [],
112-
node: native_item_ty,
113-
id: 0,
114-
span: expected});
115-
assert (node_span(node) == expected);
116-
}
117-
118-
#[test]
119-
fn test_node_span_expr() {
120-
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
121-
let node = node_expr(@{id: 0, node: expr_break, span: expected});
122-
assert (node_span(node) == expected);
123-
}
124-
}
125-
12682
// Local Variables:
12783
// mode: rust
12884
// fill-column: 78;

0 commit comments

Comments
 (0)