From 88c88530ec5788ada08bcf23d6c7b149337f0713 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 20 Oct 2019 12:20:48 +0200 Subject: [PATCH] use expect_none and unwrap_none where it makes sense --- src/eval.rs | 5 +---- src/lib.rs | 1 + src/machine.rs | 5 +---- src/shims/foreign_items.rs | 5 +---- src/shims/fs.rs | 7 ++++--- src/shims/intrinsics.rs | 4 ++-- src/shims/tls.rs | 2 +- src/stacked_borrows.rs | 4 ++-- 8 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index 21a7dd35212c..6fb1cd25b159 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -177,10 +177,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( } } - assert!( - args.next().is_none(), - "start lang item has more arguments than expected" - ); + args.next().expect_none("start lang item has more arguments than expected"); // Set the last_error to 0 let errno_layout = ecx.layout_of(ecx.tcx.types.u32)?; diff --git a/src/lib.rs b/src/lib.rs index 06ec33a914bf..59acff358678 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![feature(rustc_private)] +#![feature(option_expect_none, option_unwrap_none)] #![warn(rust_2018_idioms)] #![allow(clippy::cast_lossless)] diff --git a/src/machine.rs b/src/machine.rs index d20346ba468a..3878a0860538 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -244,10 +244,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> { ecx.write_scalar(Scalar::from_uint(align, arg.layout.size), arg)?; // No more arguments. - assert!( - args.next().is_none(), - "`exchange_malloc` lang item has more arguments than expected" - ); + args.next().expect_none("`exchange_malloc` lang item has more arguments than expected"); Ok(()) } diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index cfd3743089e7..b28e361b1377 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -349,10 +349,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let arg_dest = this.local_place(arg_local)?; this.write_scalar(data, arg_dest)?; - assert!( - args.next().is_none(), - "__rust_maybe_catch_panic argument has more arguments than expected" - ); + args.next().expect_none("__rust_maybe_catch_panic argument has more arguments than expected"); // We ourselves will return `0`, eventually (because we will not return if we paniced). this.write_null(dest)?; diff --git a/src/shims/fs.rs b/src/shims/fs.rs index 891474bc3bdb..ed2465cd1f97 100644 --- a/src/shims/fs.rs +++ b/src/shims/fs.rs @@ -7,6 +7,7 @@ use rustc::ty::layout::Size; use crate::stacked_borrows::Tag; use crate::*; +#[derive(Debug)] pub struct FileHandle { file: File, } @@ -103,7 +104,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let fd = options.open(path).map(|file| { let mut fh = &mut this.machine.file_handler; fh.low += 1; - fh.handles.insert(fh.low, FileHandle { file }); + fh.handles.insert(fh.low, FileHandle { file }).unwrap_none(); fh.low }); @@ -175,7 +176,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx .map(|buffer| handle.file.read(buffer)) }); // Reinsert the file handle - this.machine.file_handler.handles.insert(fd, handle); + this.machine.file_handler.handles.insert(fd, handle).unwrap_none(); this.consume_result(bytes?.map(|bytes| bytes as i64)) }) } @@ -204,7 +205,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx .get_bytes(&*this.tcx, buf, Size::from_bytes(count)) .map(|bytes| handle.file.write(bytes).map(|bytes| bytes as i64)) }); - this.machine.file_handler.handles.insert(fd, handle); + this.machine.file_handler.handles.insert(fd, handle).unwrap_none(); this.consume_result(bytes?) }) } diff --git a/src/shims/intrinsics.rs b/src/shims/intrinsics.rs index cd7db0973666..46658760cc12 100644 --- a/src/shims/intrinsics.rs +++ b/src/shims/intrinsics.rs @@ -356,7 +356,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx _ => { // Do it in memory let mplace = this.force_allocation(dest)?; - assert!(mplace.meta.is_none()); + mplace.meta.unwrap_none(); // not a zst, must be valid pointer let ptr = mplace.ptr.to_ptr()?; // we know the return place is in-bounds @@ -547,7 +547,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx _ => { // Do it in memory let mplace = this.force_allocation(dest)?; - assert!(mplace.meta.is_none()); + mplace.meta.unwrap_none(); let ptr = mplace.ptr.to_ptr()?; // We know the return place is in-bounds this.memory diff --git a/src/shims/tls.rs b/src/shims/tls.rs index 44bedbd44d2d..b6aadd31a5be 100644 --- a/src/shims/tls.rs +++ b/src/shims/tls.rs @@ -53,7 +53,7 @@ impl<'tcx> TlsData<'tcx> { data: None, dtor, }, - ); + ).unwrap_none(); trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor); new_key } diff --git a/src/stacked_borrows.rs b/src/stacked_borrows.rs index d219c1c75834..2188b9d5394a 100644 --- a/src/stacked_borrows.rs +++ b/src/stacked_borrows.rs @@ -172,7 +172,7 @@ impl GlobalState { pub fn new_call(&mut self) -> CallId { let id = self.next_call_id; trace!("new_call: Assigning ID {}", id); - self.active_calls.insert(id); + assert!(self.active_calls.insert(id)); self.next_call_id = NonZeroU64::new(id.get() + 1).unwrap(); id } @@ -189,7 +189,7 @@ impl GlobalState { self.base_ptr_ids.get(&id).copied().unwrap_or_else(|| { let tag = Tag::Tagged(self.new_ptr()); trace!("New allocation {:?} has base tag {:?}", id, tag); - self.base_ptr_ids.insert(id, tag); + self.base_ptr_ids.insert(id, tag).unwrap_none(); tag }) }