1
Fork 0

Merge pull request #47 from oli-obk/unreachable_bugs

replace all `unreachable!` and `panic!` calls with `bug!`
This commit is contained in:
Scott Olson 2016-09-07 02:53:33 -06:00 committed by GitHub
commit be23dcdb8c
9 changed files with 48 additions and 56 deletions

View file

@ -41,12 +41,12 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
let mut memory_size = 100*1024*1024; // 100MB
let mut step_limit = 1000_000;
let mut stack_limit = 100;
fn extract_str(lit: &syntax::ast::Lit) -> syntax::parse::token::InternedString {
let extract_int = |lit: &syntax::ast::Lit| -> u64 {
match lit.node {
syntax::ast::LitKind::Str(ref s, _) => s.clone(),
_ => panic!("attribute values need to be strings"),
syntax::ast::LitKind::Int(i, _) => i,
_ => state.session.span_fatal(lit.span, "expected an integer literal"),
}
}
};
for attr in krate.attrs.iter() {
match attr.node.value.node {
MetaItemKind::List(ref name, _) if name != "miri" => {}
@ -55,9 +55,9 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
NestedMetaItemKind::MetaItem(ref inner) => match inner.node {
MetaItemKind::NameValue(ref name, ref value) => {
match &**name {
"memory_size" => memory_size = extract_str(value).parse().expect("not a number"),
"step_limit" => step_limit = extract_str(value).parse().expect("not a number"),
"stack_limit" => stack_limit = extract_str(value).parse().expect("not a number"),
"memory_size" => memory_size = extract_int(value) as usize,
"step_limit" => step_limit = extract_int(value),
"stack_limit" => stack_limit = extract_int(value) as usize,
_ => state.session.span_err(item.span, "unknown miri attribute"),
}
}

View file

@ -187,9 +187,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
self.memory.write_f64(ptr, f)?;
Ok(ptr)
},
Float(ConstFloat::FInfer{..}) => unreachable!(),
Integral(ConstInt::Infer(_)) => unreachable!(),
Integral(ConstInt::InferSigned(_)) => unreachable!(),
Float(ConstFloat::FInfer{..}) |
Integral(ConstInt::Infer(_)) |
Integral(ConstInt::InferSigned(_)) => bug!("uninferred constants only exist before typeck"),
Integral(ConstInt::I8(i)) => i2p!(i, 1),
Integral(ConstInt::U8(i)) => i2p!(i, 1),
Integral(ConstInt::Isize(ConstIsize::Is16(i))) |
@ -359,7 +359,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
use rustc::ty::layout::Layout::*;
let tup_layout = match *dest_layout {
Univariant { ref variant, .. } => variant,
_ => panic!("checked bin op returns something other than a tuple"),
_ => bug!("checked bin op returns something other than a tuple"),
};
let overflowed = self.intrinsic_overflowing(op, left, right, dest)?;
@ -446,8 +446,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
Array { .. } => {
let elem_size = match dest_ty.sty {
ty::TyArray(elem_ty, _) => self.type_size(elem_ty) as u64,
_ => panic!("tried to assign {:?} to non-array type {:?}",
kind, dest_ty),
_ => bug!("tried to assign {:?} to non-array type {:?}", kind, dest_ty),
};
let offsets = (0..).map(|i| i * elem_size);
self.assign_fields(dest, offsets, operands)?;
@ -463,7 +462,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
.map(|s| s.bytes());
self.assign_fields(dest, offsets, operands)?;
} else {
panic!("tried to assign {:?} to Layout::General", kind);
bug!("tried to assign {:?} to Layout::General", kind);
}
}
@ -480,7 +479,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
self.memory.write_isize(dest, 0)?;
}
} else {
panic!("tried to assign {:?} to Layout::RawNullablePointer", kind);
bug!("tried to assign {:?} to Layout::RawNullablePointer", kind);
}
}
@ -497,7 +496,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
try!(self.memory.write_isize(dest, 0));
}
} else {
panic!("tried to assign {:?} to Layout::RawNullablePointer", kind);
bug!("tried to assign {:?} to Layout::RawNullablePointer", kind);
}
}
@ -513,7 +512,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
self.memory.write_uint(dest, val, size)?;
}
} else {
panic!("tried to assign {:?} to Layout::CEnum", kind);
bug!("tried to assign {:?} to Layout::CEnum", kind);
}
}
@ -524,7 +523,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
Repeat(ref operand, _) => {
let (elem_size, elem_align, length) = match dest_ty.sty {
ty::TyArray(elem_ty, n) => (self.type_size(elem_ty), self.type_align(elem_ty), n),
_ => panic!("tried to assign array-repeat to non-array type {:?}", dest_ty),
_ => bug!("tried to assign array-repeat to non-array type {:?}", dest_ty),
};
let src = self.eval_operand(operand)?;
@ -542,9 +541,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
ty::TySlice(_) => if let LvalueExtra::Length(n) = src.extra {
n
} else {
panic!("Rvalue::Len of a slice given non-slice pointer: {:?}", src);
bug!("Rvalue::Len of a slice given non-slice pointer: {:?}", src);
},
_ => panic!("Rvalue::Len expected array or slice, got {:?}", ty),
_ => bug!("Rvalue::Len expected array or slice, got {:?}", ty),
};
self.memory.write_usize(dest, len)?;
}
@ -559,7 +558,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
self.memory.write_usize(len_ptr, len)?;
}
LvalueExtra::DowncastVariant(..) =>
panic!("attempted to take a reference to an enum downcast lvalue"),
bug!("attempted to take a reference to an enum downcast lvalue"),
}
}
@ -615,7 +614,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
let fn_ptr = self.memory.create_fn_ptr(def_id, substs, fn_ty);
self.memory.write_ptr(dest, fn_ptr)?;
},
ref other => panic!("reify fn pointer on {:?}", other),
ref other => bug!("reify fn pointer on {:?}", other),
},
UnsafeFnPointer => match dest_ty.sty {
@ -626,7 +625,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
let fn_ptr = self.memory.create_fn_ptr(fn_def.def_id, fn_def.substs, unsafe_fn_ty);
self.memory.write_ptr(dest, fn_ptr)?;
},
ref other => panic!("fn to unsafe fn cast on {:?}", other),
ref other => bug!("fn to unsafe fn cast on {:?}", other),
},
}
}
@ -649,10 +648,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
let field = &variant.fields[index];
field.ty(self.tcx, substs)
}
_ => panic!(
"non-enum for StructWrappedNullablePointer: {}",
ty,
),
_ => bug!("non-enum for StructWrappedNullablePointer: {}", ty),
};
self.field_path_offset(inner_ty, path)
@ -772,7 +768,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
if let LvalueExtra::DowncastVariant(variant_idx) = base.extra {
&variants[variant_idx]
} else {
panic!("field access on enum had no variant index");
bug!("field access on enum had no variant index");
}
}
RawNullablePointer { .. } => {
@ -780,7 +776,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
return Ok(base);
}
StructWrappedNullablePointer { ref nonnull, .. } => nonnull,
_ => panic!("field access on non-product type: {:?}", base_layout),
_ => bug!("field access on non-product type: {:?}", base_layout),
};
let offset = variant.field_offset(field.index()).bytes();
@ -799,7 +795,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
RawNullablePointer { .. } | StructWrappedNullablePointer { .. } => {
return Ok(base);
}
_ => panic!("variant downcast on non-aggregate: {:?}", base_layout),
_ => bug!("variant downcast on non-aggregate: {:?}", base_layout),
}
},
@ -822,7 +818,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
let elem_size = match base_ty.sty {
ty::TyArray(elem_ty, _) |
ty::TySlice(elem_ty) => self.type_size(elem_ty),
_ => panic!("indexing expected an array or slice, got {:?}", base_ty),
_ => bug!("indexing expected an array or slice, got {:?}", base_ty),
};
let n_ptr = self.eval_operand(operand)?;
let n = self.memory.read_usize(n_ptr)?;
@ -901,7 +897,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
}
}
_ => panic!("primitive read of non-primitive type: {:?}", ty),
_ => bug!("primitive read of non-primitive type: {:?}", ty),
};
Ok(val)
}

View file

@ -198,7 +198,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
arg_srcs.push((src, ty));
}
}
ty => panic!("expected tuple as last argument in function with 'rust-call' ABI, got {:?}", ty),
ty => bug!("expected tuple as last argument in function with 'rust-call' ABI, got {:?}", ty),
}
}
@ -522,7 +522,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
// ty: def_ty(tcx, def_id, substs)
// }
}
vtable => unreachable!("resolved vtable bad vtable {:?} in trans", vtable),
vtable => bug!("resolved vtable bad vtable {:?} in trans", vtable),
}
}

View file

@ -303,7 +303,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
println!("(deallocated)");
continue;
},
(Some(_), Some(_)) => unreachable!(),
(Some(_), Some(_)) => bug!("miri invariant broken: an allocation id exists that points to both a function and a memory location"),
};
for i in 0..alloc.bytes.len() {
@ -481,7 +481,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
2 => Ok(self.layout.i16_align.abi() as usize),
4 => Ok(self.layout.i32_align.abi() as usize),
8 => Ok(self.layout.i64_align.abi() as usize),
_ => panic!("bad integer size"),
_ => bug!("bad integer size"),
}
}

View file

@ -49,8 +49,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
BitOr => $v($l | $r),
// these have already been handled
Shl => unreachable!(),
Shr => unreachable!(),
Shl | Shr => bug!("`{}` operation should already have been handled", bin_op.to_hir_binop().as_str()),
Eq => Bool($l == $r),
Ne => Bool($l != $r),
@ -72,11 +71,8 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
Rem => $v($l % $r),
// invalid float ops
BitXor => unreachable!(),
BitAnd => unreachable!(),
BitOr => unreachable!(),
Shl => unreachable!(),
Shr => unreachable!(),
BitXor | BitAnd | BitOr |
Shl | Shr => bug!("`{}` is not a valid operation on floats", bin_op.to_hir_binop().as_str()),
Eq => Bool($l == $r),
Ne => Bool($l != $r),
@ -108,7 +104,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
I16(_) | U16(_) => 16,
I32(_) | U32(_) => 32,
I64(_) | U64(_) => 64,
_ => unreachable!(),
_ => bug!("bad MIR: bitshift lhs is not integral"),
};
assert!(type_bits.is_power_of_two());
// 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
U16(i) => i as u32,
U32(i) => i as u32,
U64(i) => i as u32,
_ => panic!("bad MIR: bitshift rhs is not integral"),
_ => bug!("bad MIR: bitshift rhs is not integral"),
};
// apply mask
let r = r & (type_bits - 1);
@ -130,7 +126,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
match bin_op {
Shl => overflow!($v, U32, $l, overflowing_shl, $r),
Shr => overflow!($v, U32, $l, overflowing_shr, $r),
_ => unreachable!(),
_ => bug!("it has already been checked that this is a shift op"),
}
})
}
@ -143,7 +139,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
U16(l) => shift!(U16, l, r),
U32(l) => shift!(U32, l, r),
U64(l) => shift!(U64, l, r),
_ => unreachable!(),
_ => bug!("bad MIR: bitshift lhs is not integral (should already have been checked)"),
};
return Ok((val, false));
},
@ -168,7 +164,7 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
Le => Bool(l <= r),
Gt => Bool(l > r),
Ge => Bool(l >= r),
_ => panic!("invalid char op: {:?}", bin_op),
_ => bug!("invalid char op: {:?}", bin_op),
},
(Bool(l), Bool(r)) => {

View file

@ -1,5 +1,5 @@
#![feature(custom_attribute)]
#![miri(memory_size="0")]
#![feature(custom_attribute, attr_literals)]
#![miri(memory_size=0)]
fn bar() {
let x = 5;

View file

@ -1,5 +1,5 @@
#![feature(custom_attribute)]
#![miri(memory_size="1000")]
#![feature(custom_attribute, attr_literals)]
#![miri(memory_size=1000)]
fn bar(i: i32) {
if i < 1000 {

View file

@ -1,5 +1,5 @@
#![feature(custom_attribute)]
#![miri(stack_limit="2")]
#![feature(custom_attribute, attr_literals)]
#![miri(stack_limit=2)]
fn bar() {
foo();

View file

@ -1,6 +1,6 @@
//error-pattern: reached the configured maximum execution time
#![feature(custom_attribute)]
#![miri(step_limit="1000")]
#![feature(custom_attribute, attr_literals)]
#![miri(step_limit=1000)]
fn main() {
for i in 0..1000000 {