Skip to content

replace all unreachable! and panic! calls with bug! #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 15 additions & 12 deletions src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use miri::{eval_main, run_mir_passes};
use rustc::session::Session;
use rustc::mir::mir_map::MirMap;
use rustc_driver::{driver, CompilerCalls, Compilation};
use syntax::ast::MetaItemKind;
use syntax::ast::{MetaItemKind, NestedMetaItemKind};

struct MiriCompilerCalls;

Expand Down Expand Up @@ -41,25 +41,28 @@ 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" => {}
MetaItemKind::List(_, ref items) => for item in items {
match item.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"),
_ => state.session.span_err(item.span, "unknown miri attribute"),
NestedMetaItemKind::MetaItem(ref inner) => match inner.node {
MetaItemKind::NameValue(ref name, ref value) => {
match &**name {
"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"),
}
}
}
_ => state.session.span_err(inner.span, "miri attributes need to be of key = value kind"),
},
_ => state.session.span_err(item.span, "miri attributes need to be of key = value kind"),
}
},
Expand Down
56 changes: 26 additions & 30 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))) |
Expand Down Expand Up @@ -292,7 +292,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
// TODO(solson): Is this inefficient? Needs investigation.
let ty = self.monomorphize(ty, substs);

self.tcx.normalizing_infer_ctxt(Reveal::All).enter(|infcx| {
self.tcx.infer_ctxt(None, None, Reveal::All).enter(|infcx| {
// TODO(solson): Report this error properly.
ty.layout(&infcx).unwrap()
})
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -446,15 +446,14 @@ 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)?;
}

General { discr, ref variants, .. } => {
if let mir::AggregateKind::Adt(adt_def, variant, _) = *kind {
if let mir::AggregateKind::Adt(adt_def, variant, _, _) = *kind {
let discr_val = adt_def.variants[variant].disr_val.to_u64_unchecked();
let discr_size = discr.size().bytes() as usize;
self.memory.write_uint(dest, discr_val, discr_size)?;
Expand All @@ -463,12 +462,12 @@ 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);
}
}

RawNullablePointer { nndiscr, .. } => {
if let mir::AggregateKind::Adt(_, variant, _) = *kind {
if let mir::AggregateKind::Adt(_, variant, _, _) = *kind {
if nndiscr == variant as u64 {
assert_eq!(operands.len(), 1);
let operand = &operands[0];
Expand All @@ -480,12 +479,12 @@ 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);
}
}

StructWrappedNullablePointer { nndiscr, ref nonnull, ref discrfield } => {
if let mir::AggregateKind::Adt(_, variant, _) = *kind {
if let mir::AggregateKind::Adt(_, variant, _, _) = *kind {
if nndiscr == variant as u64 {
let offsets = iter::once(0)
.chain(nonnull.offset_after_field.iter().map(|s| s.bytes()));
Expand All @@ -497,13 +496,13 @@ 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);
}
}

CEnum { discr, signed, .. } => {
assert_eq!(operands.len(), 0);
if let mir::AggregateKind::Adt(adt_def, variant, _) = *kind {
if let mir::AggregateKind::Adt(adt_def, variant, _, _) = *kind {
let val = adt_def.variants[variant].disr_val.to_u64_unchecked();
let size = discr.size().bytes() as usize;

Expand All @@ -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);
}
}

Expand All @@ -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)?;
Expand All @@ -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)?;
}
Expand All @@ -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"),
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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),
},
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -772,15 +768,15 @@ 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 { .. } => {
assert_eq!(field.index(), 0);
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();
Expand All @@ -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),
}
},

Expand All @@ -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)?;
Expand Down Expand Up @@ -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)
}
Expand Down
22 changes: 14 additions & 8 deletions src/interpreter/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
}

let block = self.frame().block;
let stmt = self.frame().stmt;
let stmt_id = self.frame().stmt;
let mir = self.mir();
let basic_block = &mir.basic_blocks()[block];

if let Some(ref stmt) = basic_block.statements.get(stmt) {
if let Some(ref stmt) = basic_block.statements.get(stmt_id) {
let mut new = Ok(0);
ConstantExtractor {
span: stmt.source_info.span,
Expand All @@ -37,7 +37,10 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
ecx: self,
mir: &mir,
new_constants: &mut new,
}.visit_statement(block, stmt);
}.visit_statement(block, stmt, mir::Location {
block: block,
statement_index: stmt_id,
});
if new? == 0 {
self.statement(stmt)?;
}
Expand All @@ -55,7 +58,10 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
ecx: self,
mir: &mir,
new_constants: &mut new,
}.visit_terminator(block, terminator);
}.visit_terminator(block, terminator, mir::Location {
block: block,
statement_index: stmt_id,
});
if new? == 0 {
self.terminator(terminator)?;
}
Expand Down Expand Up @@ -135,8 +141,8 @@ impl<'a, 'b, 'tcx> ConstantExtractor<'a, 'b, 'tcx> {
}

impl<'a, 'b, 'tcx> Visitor<'tcx> for ConstantExtractor<'a, 'b, 'tcx> {
fn visit_constant(&mut self, constant: &mir::Constant<'tcx>) {
self.super_constant(constant);
fn visit_constant(&mut self, constant: &mir::Constant<'tcx>, location: mir::Location) {
self.super_constant(constant, location);
match constant.literal {
// already computed by rustc
mir::Literal::Value { .. } => {}
Expand Down Expand Up @@ -170,8 +176,8 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ConstantExtractor<'a, 'b, 'tcx> {
}
}

fn visit_lvalue(&mut self, lvalue: &mir::Lvalue<'tcx>, context: LvalueContext) {
self.super_lvalue(lvalue, context);
fn visit_lvalue(&mut self, lvalue: &mir::Lvalue<'tcx>, context: LvalueContext, location: mir::Location) {
self.super_lvalue(lvalue, context, location);
if let mir::Lvalue::Static(def_id) = *lvalue {
let substs = subst::Substs::empty(self.ecx.tcx);
let span = self.span;
Expand Down
Loading