Skip to content

Commit be23dcd

Browse files
authored
Merge pull request #47 from oli-obk/unreachable_bugs
replace all `unreachable!` and `panic!` calls with `bug!`
2 parents f718e77 + 7be27ec commit be23dcd

File tree

9 files changed

+48
-56
lines changed

9 files changed

+48
-56
lines changed

src/bin/miri.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
4141
let mut memory_size = 100*1024*1024; // 100MB
4242
let mut step_limit = 1000_000;
4343
let mut stack_limit = 100;
44-
fn extract_str(lit: &syntax::ast::Lit) -> syntax::parse::token::InternedString {
44+
let extract_int = |lit: &syntax::ast::Lit| -> u64 {
4545
match lit.node {
46-
syntax::ast::LitKind::Str(ref s, _) => s.clone(),
47-
_ => panic!("attribute values need to be strings"),
46+
syntax::ast::LitKind::Int(i, _) => i,
47+
_ => state.session.span_fatal(lit.span, "expected an integer literal"),
4848
}
49-
}
49+
};
5050
for attr in krate.attrs.iter() {
5151
match attr.node.value.node {
5252
MetaItemKind::List(ref name, _) if name != "miri" => {}
@@ -55,9 +55,9 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
5555
NestedMetaItemKind::MetaItem(ref inner) => match inner.node {
5656
MetaItemKind::NameValue(ref name, ref value) => {
5757
match &**name {
58-
"memory_size" => memory_size = extract_str(value).parse().expect("not a number"),
59-
"step_limit" => step_limit = extract_str(value).parse().expect("not a number"),
60-
"stack_limit" => stack_limit = extract_str(value).parse().expect("not a number"),
58+
"memory_size" => memory_size = extract_int(value) as usize,
59+
"step_limit" => step_limit = extract_int(value),
60+
"stack_limit" => stack_limit = extract_int(value) as usize,
6161
_ => state.session.span_err(item.span, "unknown miri attribute"),
6262
}
6363
}

src/interpreter/mod.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
187187
self.memory.write_f64(ptr, f)?;
188188
Ok(ptr)
189189
},
190-
Float(ConstFloat::FInfer{..}) => unreachable!(),
191-
Integral(ConstInt::Infer(_)) => unreachable!(),
192-
Integral(ConstInt::InferSigned(_)) => unreachable!(),
190+
Float(ConstFloat::FInfer{..}) |
191+
Integral(ConstInt::Infer(_)) |
192+
Integral(ConstInt::InferSigned(_)) => bug!("uninferred constants only exist before typeck"),
193193
Integral(ConstInt::I8(i)) => i2p!(i, 1),
194194
Integral(ConstInt::U8(i)) => i2p!(i, 1),
195195
Integral(ConstInt::Isize(ConstIsize::Is16(i))) |
@@ -359,7 +359,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
359359
use rustc::ty::layout::Layout::*;
360360
let tup_layout = match *dest_layout {
361361
Univariant { ref variant, .. } => variant,
362-
_ => panic!("checked bin op returns something other than a tuple"),
362+
_ => bug!("checked bin op returns something other than a tuple"),
363363
};
364364

365365
let overflowed = self.intrinsic_overflowing(op, left, right, dest)?;
@@ -446,8 +446,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
446446
Array { .. } => {
447447
let elem_size = match dest_ty.sty {
448448
ty::TyArray(elem_ty, _) => self.type_size(elem_ty) as u64,
449-
_ => panic!("tried to assign {:?} to non-array type {:?}",
450-
kind, dest_ty),
449+
_ => bug!("tried to assign {:?} to non-array type {:?}", kind, dest_ty),
451450
};
452451
let offsets = (0..).map(|i| i * elem_size);
453452
self.assign_fields(dest, offsets, operands)?;
@@ -463,7 +462,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
463462
.map(|s| s.bytes());
464463
self.assign_fields(dest, offsets, operands)?;
465464
} else {
466-
panic!("tried to assign {:?} to Layout::General", kind);
465+
bug!("tried to assign {:?} to Layout::General", kind);
467466
}
468467
}
469468

@@ -480,7 +479,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
480479
self.memory.write_isize(dest, 0)?;
481480
}
482481
} else {
483-
panic!("tried to assign {:?} to Layout::RawNullablePointer", kind);
482+
bug!("tried to assign {:?} to Layout::RawNullablePointer", kind);
484483
}
485484
}
486485

@@ -497,7 +496,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
497496
try!(self.memory.write_isize(dest, 0));
498497
}
499498
} else {
500-
panic!("tried to assign {:?} to Layout::RawNullablePointer", kind);
499+
bug!("tried to assign {:?} to Layout::RawNullablePointer", kind);
501500
}
502501
}
503502

@@ -513,7 +512,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
513512
self.memory.write_uint(dest, val, size)?;
514513
}
515514
} else {
516-
panic!("tried to assign {:?} to Layout::CEnum", kind);
515+
bug!("tried to assign {:?} to Layout::CEnum", kind);
517516
}
518517
}
519518

@@ -524,7 +523,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
524523
Repeat(ref operand, _) => {
525524
let (elem_size, elem_align, length) = match dest_ty.sty {
526525
ty::TyArray(elem_ty, n) => (self.type_size(elem_ty), self.type_align(elem_ty), n),
527-
_ => panic!("tried to assign array-repeat to non-array type {:?}", dest_ty),
526+
_ => bug!("tried to assign array-repeat to non-array type {:?}", dest_ty),
528527
};
529528

530529
let src = self.eval_operand(operand)?;
@@ -542,9 +541,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
542541
ty::TySlice(_) => if let LvalueExtra::Length(n) = src.extra {
543542
n
544543
} else {
545-
panic!("Rvalue::Len of a slice given non-slice pointer: {:?}", src);
544+
bug!("Rvalue::Len of a slice given non-slice pointer: {:?}", src);
546545
},
547-
_ => panic!("Rvalue::Len expected array or slice, got {:?}", ty),
546+
_ => bug!("Rvalue::Len expected array or slice, got {:?}", ty),
548547
};
549548
self.memory.write_usize(dest, len)?;
550549
}
@@ -559,7 +558,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
559558
self.memory.write_usize(len_ptr, len)?;
560559
}
561560
LvalueExtra::DowncastVariant(..) =>
562-
panic!("attempted to take a reference to an enum downcast lvalue"),
561+
bug!("attempted to take a reference to an enum downcast lvalue"),
563562
}
564563
}
565564

@@ -615,7 +614,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
615614
let fn_ptr = self.memory.create_fn_ptr(def_id, substs, fn_ty);
616615
self.memory.write_ptr(dest, fn_ptr)?;
617616
},
618-
ref other => panic!("reify fn pointer on {:?}", other),
617+
ref other => bug!("reify fn pointer on {:?}", other),
619618
},
620619

621620
UnsafeFnPointer => match dest_ty.sty {
@@ -626,7 +625,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
626625
let fn_ptr = self.memory.create_fn_ptr(fn_def.def_id, fn_def.substs, unsafe_fn_ty);
627626
self.memory.write_ptr(dest, fn_ptr)?;
628627
},
629-
ref other => panic!("fn to unsafe fn cast on {:?}", other),
628+
ref other => bug!("fn to unsafe fn cast on {:?}", other),
630629
},
631630
}
632631
}
@@ -649,10 +648,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
649648
let field = &variant.fields[index];
650649
field.ty(self.tcx, substs)
651650
}
652-
_ => panic!(
653-
"non-enum for StructWrappedNullablePointer: {}",
654-
ty,
655-
),
651+
_ => bug!("non-enum for StructWrappedNullablePointer: {}", ty),
656652
};
657653

658654
self.field_path_offset(inner_ty, path)
@@ -772,15 +768,15 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
772768
if let LvalueExtra::DowncastVariant(variant_idx) = base.extra {
773769
&variants[variant_idx]
774770
} else {
775-
panic!("field access on enum had no variant index");
771+
bug!("field access on enum had no variant index");
776772
}
777773
}
778774
RawNullablePointer { .. } => {
779775
assert_eq!(field.index(), 0);
780776
return Ok(base);
781777
}
782778
StructWrappedNullablePointer { ref nonnull, .. } => nonnull,
783-
_ => panic!("field access on non-product type: {:?}", base_layout),
779+
_ => bug!("field access on non-product type: {:?}", base_layout),
784780
};
785781

786782
let offset = variant.field_offset(field.index()).bytes();
@@ -799,7 +795,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
799795
RawNullablePointer { .. } | StructWrappedNullablePointer { .. } => {
800796
return Ok(base);
801797
}
802-
_ => panic!("variant downcast on non-aggregate: {:?}", base_layout),
798+
_ => bug!("variant downcast on non-aggregate: {:?}", base_layout),
803799
}
804800
},
805801

@@ -822,7 +818,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
822818
let elem_size = match base_ty.sty {
823819
ty::TyArray(elem_ty, _) |
824820
ty::TySlice(elem_ty) => self.type_size(elem_ty),
825-
_ => panic!("indexing expected an array or slice, got {:?}", base_ty),
821+
_ => bug!("indexing expected an array or slice, got {:?}", base_ty),
826822
};
827823
let n_ptr = self.eval_operand(operand)?;
828824
let n = self.memory.read_usize(n_ptr)?;
@@ -901,7 +897,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
901897
}
902898
}
903899

904-
_ => panic!("primitive read of non-primitive type: {:?}", ty),
900+
_ => bug!("primitive read of non-primitive type: {:?}", ty),
905901
};
906902
Ok(val)
907903
}

src/interpreter/terminator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
198198
arg_srcs.push((src, ty));
199199
}
200200
}
201-
ty => panic!("expected tuple as last argument in function with 'rust-call' ABI, got {:?}", ty),
201+
ty => bug!("expected tuple as last argument in function with 'rust-call' ABI, got {:?}", ty),
202202
}
203203
}
204204

@@ -522,7 +522,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
522522
// ty: def_ty(tcx, def_id, substs)
523523
// }
524524
}
525-
vtable => unreachable!("resolved vtable bad vtable {:?} in trans", vtable),
525+
vtable => bug!("resolved vtable bad vtable {:?} in trans", vtable),
526526
}
527527
}
528528

src/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
303303
println!("(deallocated)");
304304
continue;
305305
},
306-
(Some(_), Some(_)) => unreachable!(),
306+
(Some(_), Some(_)) => bug!("miri invariant broken: an allocation id exists that points to both a function and a memory location"),
307307
};
308308

309309
for i in 0..alloc.bytes.len() {
@@ -481,7 +481,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
481481
2 => Ok(self.layout.i16_align.abi() as usize),
482482
4 => Ok(self.layout.i32_align.abi() as usize),
483483
8 => Ok(self.layout.i64_align.abi() as usize),
484-
_ => panic!("bad integer size"),
484+
_ => bug!("bad integer size"),
485485
}
486486
}
487487

src/primval.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
4949
BitOr => $v($l | $r),
5050

5151
// these have already been handled
52-
Shl => unreachable!(),
53-
Shr => unreachable!(),
52+
Shl | Shr => bug!("`{}` operation should already have been handled", bin_op.to_hir_binop().as_str()),
5453

5554
Eq => Bool($l == $r),
5655
Ne => Bool($l != $r),
@@ -72,11 +71,8 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
7271
Rem => $v($l % $r),
7372

7473
// invalid float ops
75-
BitXor => unreachable!(),
76-
BitAnd => unreachable!(),
77-
BitOr => unreachable!(),
78-
Shl => unreachable!(),
79-
Shr => unreachable!(),
74+
BitXor | BitAnd | BitOr |
75+
Shl | Shr => bug!("`{}` is not a valid operation on floats", bin_op.to_hir_binop().as_str()),
8076

8177
Eq => Bool($l == $r),
8278
Ne => Bool($l != $r),
@@ -108,7 +104,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
108104
I16(_) | U16(_) => 16,
109105
I32(_) | U32(_) => 32,
110106
I64(_) | U64(_) => 64,
111-
_ => unreachable!(),
107+
_ => bug!("bad MIR: bitshift lhs is not integral"),
112108
};
113109
assert!(type_bits.is_power_of_two());
114110
// turn into `u32` because `overflowing_sh{l,r}` only take `u32`
@@ -121,7 +117,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
121117
U16(i) => i as u32,
122118
U32(i) => i as u32,
123119
U64(i) => i as u32,
124-
_ => panic!("bad MIR: bitshift rhs is not integral"),
120+
_ => bug!("bad MIR: bitshift rhs is not integral"),
125121
};
126122
// apply mask
127123
let r = r & (type_bits - 1);
@@ -130,7 +126,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
130126
match bin_op {
131127
Shl => overflow!($v, U32, $l, overflowing_shl, $r),
132128
Shr => overflow!($v, U32, $l, overflowing_shr, $r),
133-
_ => unreachable!(),
129+
_ => bug!("it has already been checked that this is a shift op"),
134130
}
135131
})
136132
}
@@ -143,7 +139,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
143139
U16(l) => shift!(U16, l, r),
144140
U32(l) => shift!(U32, l, r),
145141
U64(l) => shift!(U64, l, r),
146-
_ => unreachable!(),
142+
_ => bug!("bad MIR: bitshift lhs is not integral (should already have been checked)"),
147143
};
148144
return Ok((val, false));
149145
},
@@ -168,7 +164,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
168164
Le => Bool(l <= r),
169165
Gt => Bool(l > r),
170166
Ge => Bool(l >= r),
171-
_ => panic!("invalid char op: {:?}", bin_op),
167+
_ => bug!("invalid char op: {:?}", bin_op),
172168
},
173169

174170
(Bool(l), Bool(r)) => {

tests/compile-fail/oom.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#![feature(custom_attribute)]
2-
#![miri(memory_size="0")]
1+
#![feature(custom_attribute, attr_literals)]
2+
#![miri(memory_size=0)]
33

44
fn bar() {
55
let x = 5;

tests/compile-fail/oom2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#![feature(custom_attribute)]
2-
#![miri(memory_size="1000")]
1+
#![feature(custom_attribute, attr_literals)]
2+
#![miri(memory_size=1000)]
33

44
fn bar(i: i32) {
55
if i < 1000 {

tests/compile-fail/stack_limit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#![feature(custom_attribute)]
2-
#![miri(stack_limit="2")]
1+
#![feature(custom_attribute, attr_literals)]
2+
#![miri(stack_limit=2)]
33

44
fn bar() {
55
foo();

tests/compile-fail/timeout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//error-pattern: reached the configured maximum execution time
2-
#![feature(custom_attribute)]
3-
#![miri(step_limit="1000")]
2+
#![feature(custom_attribute, attr_literals)]
3+
#![miri(step_limit=1000)]
44

55
fn main() {
66
for i in 0..1000000 {

0 commit comments

Comments
 (0)