Skip to content

Commit 7284969

Browse files
committed
Eliminate many match checks in rustc
1 parent 0e620ac commit 7284969

File tree

12 files changed

+61
-88
lines changed

12 files changed

+61
-88
lines changed

src/fuzzer/fuzzer.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,15 @@ pure fn safe_to_use_expr(e: ast::expr, tm: test_mode) -> bool {
6868
// If the fuzzer moves a block-ending-in-semicolon into callee
6969
// position, the pretty-printer can't preserve this even by
7070
// parenthesizing!! See email to marijn.
71-
ast::expr_if(_, _, _) => { false }
72-
ast::expr_block(_) => { false }
73-
ast::expr_match(_, _, _) => { false }
74-
ast::expr_while(_, _) => { false }
71+
ast::expr_if(*) | ast::expr_block(*)
72+
| ast::expr_match(*) | ast::expr_while(*) => { false }
7573

7674
// https://github.com/mozilla/rust/issues/929
77-
ast::expr_cast(_, _) => { false }
78-
ast::expr_assert(_) => { false }
79-
ast::expr_binary(_, _, _) => { false }
80-
ast::expr_assign(_, _) => { false }
81-
ast::expr_assign_op(_, _, _) => { false }
75+
ast::expr_cast(*) | ast::expr_assert(*) |
76+
ast::expr_binary(*) | ast::expr_assign(*) |
77+
ast::expr_assign_op(*) => { false }
8278

83-
ast::expr_fail(option::none) => { false }
79+
ast::expr_fail(option::none) |
8480
ast::expr_ret(option::none) => { false }
8581

8682
// https://github.com/mozilla/rust/issues/953

src/libsyntax/parse/classify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import ast_util::operator_prec;
66

77
fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
88
match e.node {
9-
ast::expr_if(_, _, _) | ast::expr_match(_, _, _) | ast::expr_block(_)
10-
| ast::expr_while(_, _) | ast::expr_loop(_, _)
9+
ast::expr_if(*) | ast::expr_match(*) | ast::expr_block(_)
10+
| ast::expr_while(*) | ast::expr_loop(*)
1111
| ast::expr_call(_, _, true) => false,
1212
_ => true
1313
}

src/rustc/back/link.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ mod write {
8484
if opts.save_temps {
8585
match opts.output_type {
8686
output_type_bitcode => {
87-
if opts.optimize != 0u {
87+
if opts.optimize != session::No {
8888
let filename = mk_intermediate_name(output, ~"no-opt.bc");
8989
str::as_c_str(filename, |buf| {
9090
llvm::LLVMWriteBitcodeToFile(llmod, buf)
@@ -107,7 +107,7 @@ mod write {
107107
// Also: Should we expose and use the pass lists used by the opt
108108
// tool?
109109

110-
if opts.optimize != 0u {
110+
if opts.optimize != session::No {
111111
let fpm = mk_pass_manager();
112112
llvm::LLVMAddTargetData(td.lltd, fpm.llpm);
113113

@@ -118,8 +118,8 @@ mod write {
118118
llvm::LLVMPassManagerBuilderDispose(FPMB);
119119

120120
llvm::LLVMRunPassManager(fpm.llpm, llmod);
121-
let mut threshold = 225u;
122-
if opts.optimize == 3u { threshold = 275u; }
121+
let mut threshold = 225;
122+
if opts.optimize == session::Aggressive { threshold = 275; }
123123

124124
let MPMB = llvm::LLVMPassManagerBuilderCreate();
125125
llvm::LLVMPassManagerBuilderSetOptLevel(MPMB,
@@ -146,11 +146,11 @@ mod write {
146146
let LLVMOptDefault = 2 as c_int; // -O2, -Os
147147
let LLVMOptAggressive = 3 as c_int; // -O3
148148

149-
let mut CodeGenOptLevel = match check opts.optimize {
150-
0u => LLVMOptNone,
151-
1u => LLVMOptLess,
152-
2u => LLVMOptDefault,
153-
3u => LLVMOptAggressive
149+
let mut CodeGenOptLevel = match opts.optimize {
150+
session::No => LLVMOptNone,
151+
session::Less => LLVMOptLess,
152+
session::Default => LLVMOptDefault,
153+
session::Aggressive => LLVMOptAggressive
154154
};
155155

156156
let mut FileType;

src/rustc/driver/driver.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// -*- rust -*-
22
import metadata::{creader, cstore, filesearch};
3-
import session::{session, session_};
3+
import session::{session, session_, OptLevel, No, Less, Default, Aggressive};
44
import syntax::parse;
55
import syntax::{ast, codemap};
66
import syntax::attr;
@@ -488,24 +488,24 @@ fn build_session_options(matches: getopts::matches,
488488
link::output_type_llvm_assembly | link::output_type_assembly => (),
489489
_ => debugging_opts |= session::no_asm_comments
490490
}
491-
let opt_level: uint =
491+
let opt_level =
492492
if opt_present(matches, ~"O") {
493493
if opt_present(matches, ~"opt-level") {
494494
early_error(demitter, ~"-O and --opt-level both provided");
495495
}
496-
2u
496+
Default
497497
} else if opt_present(matches, ~"opt-level") {
498498
match getopts::opt_str(matches, ~"opt-level") {
499-
~"0" => 0u,
500-
~"1" => 1u,
501-
~"2" => 2u,
502-
~"3" => 3u,
499+
~"0" => No,
500+
~"1" => Less,
501+
~"2" => Default,
502+
~"3" => Aggressive,
503503
_ => {
504504
early_error(demitter, ~"optimization level needs " +
505505
~"to be between 0-3")
506506
}
507507
}
508-
} else { 0u };
508+
} else { No };
509509
let target =
510510
match target_opt {
511511
none => host_triple(),

src/rustc/driver/session.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,19 @@ fn debugging_opts_map() -> ~[(~str, ~str, uint)] {
7070
]
7171
}
7272

73+
enum OptLevel {
74+
No, // -O0
75+
Less, // -O1
76+
Default, // -O2
77+
Aggressive // -O3
78+
}
79+
7380
type options =
7481
// The crate config requested for the session, which may be combined
7582
// with additional crate configurations during the compile process
7683
{crate_type: crate_type,
7784
static: bool,
78-
optimize: uint,
85+
optimize: OptLevel,
7986
debuginfo: bool,
8087
extra_debuginfo: bool,
8188
lint_opts: ~[(lint::lint, lint::level)],
@@ -179,6 +186,11 @@ impl session {
179186
fn debugging_opt(opt: uint) -> bool {
180187
(self.opts.debugging_opts & opt) != 0u
181188
}
189+
// This exists to help with refactoring to eliminate impossible
190+
// cases later on
191+
fn impossible_case(sp: span, msg: ~str) -> ! {
192+
self.span_bug(sp, #fmt("Impossible case reached: %s", msg));
193+
}
182194
fn ppregions() -> bool { self.debugging_opt(ppregions) }
183195
fn time_passes() -> bool { self.debugging_opt(time_passes) }
184196
fn count_llvm_insns() -> bool { self.debugging_opt(count_llvm_insns) }
@@ -199,7 +211,7 @@ fn basic_options() -> @options {
199211
@{
200212
crate_type: session::lib_crate,
201213
static: false,
202-
optimize: 0u,
214+
optimize: No,
203215
debuginfo: false,
204216
extra_debuginfo: false,
205217
lint_opts: ~[],

src/rustc/middle/astencode.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,8 @@ impl ebml::ebml_deserializer: vtable_deserialization_helpers {
509509
-> typeck::vtable_origin {
510510
do self.read_enum(~"vtable_origin") {
511511
do self.read_enum_variant |i| {
512-
match check i {
513-
0u => {
512+
match i {
513+
0 => {
514514
typeck::vtable_static(
515515
do self.read_enum_variant_arg(0u) {
516516
self.read_def_id(xcx)
@@ -523,7 +523,7 @@ impl ebml::ebml_deserializer: vtable_deserialization_helpers {
523523
}
524524
)
525525
}
526-
1u => {
526+
1 => {
527527
typeck::vtable_param(
528528
do self.read_enum_variant_arg(0u) {
529529
self.read_uint()
@@ -533,7 +533,7 @@ impl ebml::ebml_deserializer: vtable_deserialization_helpers {
533533
}
534534
)
535535
}
536-
2u => {
536+
2 => {
537537
typeck::vtable_trait(
538538
do self.read_enum_variant_arg(0u) {
539539
self.read_def_id(xcx)
@@ -543,6 +543,8 @@ impl ebml::ebml_deserializer: vtable_deserialization_helpers {
543543
}
544544
)
545545
}
546+
// hard to avoid - user input
547+
_ => fail ~"bad enum variant"
546548
}
547549
}
548550
}

src/rustc/middle/freevars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn collect_freevars(def_map: resolve3::DefMap, blk: ast::blk)
4444
visit::visit_expr(expr, depth + 1, v);
4545
}
4646
}
47-
ast::expr_fn_block(_, _, _) => {
47+
ast::expr_fn_block(*) => {
4848
visit::visit_expr(expr, depth + 1, v);
4949
}
5050
ast::expr_path(path) => {

src/rustc/middle/kind.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn check_fn(fk: visit::fn_kind, decl: fn_decl, body: blk, sp: span,
191191
// if this is the last use of the variable, then it will be
192192
// a move and not a copy
193193
let is_move = {
194-
match check cx.last_use_map.find(fn_id) {
194+
match cx.last_use_map.find(fn_id) {
195195
some(vars) => (*vars).contains(id),
196196
none => false
197197
}
@@ -218,7 +218,7 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
218218

219219
// Handle any kind bounds on type parameters
220220
do option::iter(cx.tcx.node_type_substs.find(e.id)) |ts| {
221-
let bounds = match check e.node {
221+
let bounds = match e.node {
222222
expr_path(_) => {
223223
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(e.id));
224224
ty::lookup_item_type(cx.tcx, did).bounds

src/rustc/middle/lint.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,6 @@ enum lint {
5555
non_camel_case_types
5656
}
5757

58-
// This is pretty unfortunate. We really want some sort of "deriving Enum"
59-
// type of thing.
60-
fn int_to_lint(i: int) -> lint {
61-
match check i {
62-
0 => ctypes,
63-
1 => unused_imports,
64-
2 => while_true,
65-
3 => path_statement,
66-
4 => implicit_copies,
67-
5 => unrecognized_lint,
68-
6 => non_implicitly_copyable_typarams,
69-
7 => vecs_implicitly_copyable,
70-
8 => deprecated_mode,
71-
9 => non_camel_case_types
72-
}
73-
}
74-
7558
fn level_to_str(lv: level) -> ~str {
7659
match lv {
7760
allow => ~"allow",
@@ -537,7 +520,7 @@ fn check_fn(tcx: ty::ctxt, fk: visit::fn_kind, decl: ast::fn_decl,
537520
}
538521

539522
let fn_ty = ty::node_id_to_type(tcx, id);
540-
match check ty::get(fn_ty).struct {
523+
match ty::get(fn_ty).struct {
541524
ty::ty_fn(fn_ty) => {
542525
let mut counter = 0;
543526
do vec::iter2(fn_ty.inputs, decl.inputs) |arg_ty, arg_ast| {
@@ -572,6 +555,8 @@ fn check_fn(tcx: ty::ctxt, fk: visit::fn_kind, decl: ast::fn_decl,
572555
}
573556
}
574557
}
558+
_ => tcx.sess.impossible_case(span, ~"check_fn: function has \
559+
non-fn type")
575560
}
576561
}
577562

src/rustc/middle/trans/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ fn build_closure(bcx0: block,
272272
vec::push(env_vals, env_ref(lv.val, ty, lv.kind));
273273
}
274274
capture::cap_copy => {
275-
let mv = match check ccx.maps.last_use_map.find(id) {
275+
let mv = match ccx.maps.last_use_map.find(id) {
276276
none => false,
277277
some(vars) => (*vars).contains(nid)
278278
};

src/rustc/middle/trans/consts.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
246246
let ety = ty::expr_ty(cx.tcx, e), llty = type_of::type_of(cx, ety);
247247
let basety = ty::expr_ty(cx.tcx, base);
248248
let v = const_expr(cx, base);
249-
match check (base::cast_type_kind(basety),
249+
match (base::cast_type_kind(basety),
250250
base::cast_type_kind(ety)) {
251251

252252
(base::cast_integral, base::cast_integral) => {
@@ -264,6 +264,8 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
264264
if ty::type_is_signed(ety) { llvm::LLVMConstFPToSI(v, llty) }
265265
else { llvm::LLVMConstFPToUI(v, llty) }
266266
}
267+
_ => cx.sess.impossible_case(e.span,
268+
~"bad combination of types for cast")
267269
}
268270
}
269271
ast::expr_addr_of(ast::m_imm, sub) => {

src/rustc/middle/trans/debuginfo.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fn create_compile_unit(cx: @crate_ctxt)
180180
llstr(work_dir),
181181
llstr(env!{"CFG_VERSION"}),
182182
lli1(true), // deprecated: main compile unit
183-
lli1(cx.sess.opts.optimize != 0u),
183+
lli1(cx.sess.opts.optimize != session::No),
184184
llstr(~""), // flags (???)
185185
lli32(0) // runtime version (???)
186186
];
@@ -281,7 +281,7 @@ fn size_and_align_of(cx: @crate_ctxt, t: ty::t) -> (int, int) {
281281
shape::llalign_of_pref(cx, llty) as int)
282282
}
283283

284-
fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: ast::prim_ty, span: span)
284+
fn create_basic_type(cx: @crate_ctxt, t: ty::t, span: span)
285285
-> @metadata<tydesc_md> {
286286
let cache = get_cache(cx);
287287
let tg = BasicTypeDescriptorTag;
@@ -291,29 +291,7 @@ fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: ast::prim_ty, span: span)
291291
option::none => ()
292292
}
293293

294-
let (name, encoding) = match check ty {
295-
ast::ty_bool => (~"bool", DW_ATE_boolean),
296-
ast::ty_int(m) => match m {
297-
ast::ty_char => (~"char", DW_ATE_unsigned),
298-
ast::ty_i => (~"int", DW_ATE_signed),
299-
ast::ty_i8 => (~"i8", DW_ATE_signed_char),
300-
ast::ty_i16 => (~"i16", DW_ATE_signed),
301-
ast::ty_i32 => (~"i32", DW_ATE_signed),
302-
ast::ty_i64 => (~"i64", DW_ATE_signed)
303-
},
304-
ast::ty_uint(m) => match m {
305-
ast::ty_u => (~"uint", DW_ATE_unsigned),
306-
ast::ty_u8 => (~"u8", DW_ATE_unsigned_char),
307-
ast::ty_u16 => (~"u16", DW_ATE_unsigned),
308-
ast::ty_u32 => (~"u32", DW_ATE_unsigned),
309-
ast::ty_u64 => (~"u64", DW_ATE_unsigned)
310-
},
311-
ast::ty_float(m) => match m {
312-
ast::ty_f => (~"float", DW_ATE_float),
313-
ast::ty_f32 => (~"f32", DW_ATE_float),
314-
ast::ty_f64 => (~"f64", DW_ATE_float)
315-
}
316-
};
294+
let (name, encoding) = (~"uint", DW_ATE_unsigned);
317295

318296
let fname = filename_from_span(cx, span);
319297
let file_node = create_file(cx, fname);
@@ -443,8 +421,7 @@ fn create_boxed_type(cx: @crate_ctxt, outer: ty::t, _inner: ty::t,
443421
let file_node = create_file(cx, fname);
444422
//let cu_node = create_compile_unit_metadata(cx, fname);
445423
let uint_t = ty::mk_uint(cx.tcx);
446-
let refcount_type = create_basic_type(cx, uint_t,
447-
ast::ty_uint(ast::ty_u), span);
424+
let refcount_type = create_basic_type(cx, uint_t, span);
448425
let scx = create_structure(file_node, ty_to_str(cx.tcx, outer), 0);
449426
add_member(scx, ~"refcnt", 0, sys::size_of::<uint>() as int,
450427
sys::min_align_of::<uint>() as int, refcount_type.node);
@@ -495,8 +472,7 @@ fn create_vec(cx: @crate_ctxt, vec_t: ty::t, elem_t: ty::t,
495472
let file_node = create_file(cx, fname);
496473
let elem_ty_md = create_ty(cx, elem_t, elem_ty);
497474
let scx = create_structure(file_node, ty_to_str(cx.tcx, vec_t), 0);
498-
let size_t_type = create_basic_type(cx, ty::mk_uint(cx.tcx),
499-
ast::ty_uint(ast::ty_u), vec_ty_span);
475+
let size_t_type = create_basic_type(cx, ty::mk_uint(cx.tcx), vec_ty_span);
500476
add_member(scx, ~"fill", 0, sys::size_of::<libc::size_t>() as int,
501477
sys::min_align_of::<libc::size_t>() as int, size_t_type.node);
502478
add_member(scx, ~"alloc", 0, sys::size_of::<libc::size_t>() as int,
@@ -814,7 +790,7 @@ fn create_function(fcx: fn_ctxt) -> @metadata<subprogram_md> {
814790
lli32(0i), //index into virt func
815791
/*llnull()*/ lli32(0), // base type with vtbl
816792
lli32(256), // flags
817-
lli1(cx.sess.opts.optimize != 0u),
793+
lli1(cx.sess.opts.optimize != session::No),
818794
fcx.llfn
819795
//list of template params
820796
//func decl descriptor

0 commit comments

Comments
 (0)