Skip to content

Commit 46db93a

Browse files
committed
Change error handling style for consistency
1 parent 60cf06a commit 46db93a

File tree

2 files changed

+51
-47
lines changed

2 files changed

+51
-47
lines changed

src/helpers.rs

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use std::mem;
22

3-
use rustc::ty::{self, layout::{self, Size, Align}};
43
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
54
use rustc::mir;
5+
use rustc::ty::{
6+
self,
7+
layout::{self, Align, Size},
8+
};
69

710
use rand::RngCore;
811

@@ -48,7 +51,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4851

4952
/// Write a 0 of the appropriate size to `dest`.
5053
fn write_null(&mut self, dest: PlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
51-
self.eval_context_mut().write_scalar(Scalar::from_int(0, dest.layout.size), dest)
54+
self.eval_context_mut()
55+
.write_scalar(Scalar::from_int(0, dest.layout.size), dest)
5256
}
5357

5458
/// Test if this immediate equals 0.
@@ -61,26 +65,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6165
/// Turn a Scalar into an Option<NonNullScalar>
6266
fn test_null(&self, val: Scalar<Tag>) -> InterpResult<'tcx, Option<Scalar<Tag>>> {
6367
let this = self.eval_context_ref();
64-
Ok(if this.is_null(val)? {
65-
None
66-
} else {
67-
Some(val)
68-
})
68+
Ok(if this.is_null(val)? { None } else { Some(val) })
6969
}
7070

7171
/// Get the `Place` for a local
7272
fn local_place(&mut self, local: mir::Local) -> InterpResult<'tcx, PlaceTy<'tcx, Tag>> {
7373
let this = self.eval_context_mut();
74-
let place = mir::Place { base: mir::PlaceBase::Local(local), projection: Box::new([]) };
74+
let place = mir::Place {
75+
base: mir::PlaceBase::Local(local),
76+
projection: Box::new([]),
77+
};
7578
this.eval_place(&place)
7679
}
7780

7881
/// Generate some random bytes, and write them to `dest`.
79-
fn gen_random(
80-
&mut self,
81-
ptr: Scalar<Tag>,
82-
len: usize,
83-
) -> InterpResult<'tcx> {
82+
fn gen_random(&mut self, ptr: Scalar<Tag>, len: usize) -> InterpResult<'tcx> {
8483
// Some programs pass in a null pointer and a length of 0
8584
// to their platform's random-generation function (e.g. getrandom())
8685
// on Linux. For compatibility with these programs, we don't perform
@@ -91,26 +90,30 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9190
}
9291
let this = self.eval_context_mut();
9392

94-
let ptr = this.memory().check_ptr_access(
95-
ptr,
96-
Size::from_bytes(len as u64),
97-
Align::from_bytes(1).unwrap()
98-
)?.expect("we already checked for size 0");
93+
let ptr = this
94+
.memory()
95+
.check_ptr_access(
96+
ptr,
97+
Size::from_bytes(len as u64),
98+
Align::from_bytes(1).unwrap(),
99+
)?
100+
.expect("we already checked for size 0");
99101

100102
let mut data = vec![0; len];
101103

102104
if this.machine.communicate {
103105
// Fill the buffer using the host's rng.
104106
getrandom::getrandom(&mut data)
105107
.map_err(|err| err_unsup_format!("getrandom failed: {}", err))?;
106-
}
107-
else {
108+
} else {
108109
let rng = this.memory_mut().extra.rng.get_mut();
109110
rng.fill_bytes(&mut data);
110111
}
111112

112-
let tcx = &{this.tcx.tcx};
113-
this.memory_mut().get_mut(ptr.alloc_id)?.write_bytes(tcx, ptr, &data)
113+
let tcx = &{ this.tcx.tcx };
114+
this.memory_mut()
115+
.get_mut(ptr.alloc_id)?
116+
.write_bytes(tcx, ptr, &data)
114117
}
115118

116119
/// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter
@@ -123,10 +126,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
123126
) -> InterpResult<'tcx> {
124127
let this = self.eval_context_ref();
125128
trace!("visit_frozen(place={:?}, size={:?})", *place, size);
126-
debug_assert_eq!(size,
129+
debug_assert_eq!(
130+
size,
127131
this.size_and_align_of_mplace(place)?
128-
.map(|(size, _)| size)
129-
.unwrap_or_else(|| place.layout.size)
132+
.map(|(size, _)| size)
133+
.unwrap_or_else(|| place.layout.size)
130134
);
131135
// Store how far we proceeded into the place so far. Everything to the left of
132136
// this offset has already been handled, in the sense that the frozen parts
@@ -146,11 +150,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
146150
let frozen_size = unsafe_cell_offset - end_offset;
147151
// Everything between the end_ptr and this `UnsafeCell` is frozen.
148152
if frozen_size != Size::ZERO {
149-
action(end_ptr, frozen_size, /*frozen*/true)?;
153+
action(end_ptr, frozen_size, /*frozen*/ true)?;
150154
}
151155
// This `UnsafeCell` is NOT frozen.
152156
if unsafe_cell_size != Size::ZERO {
153-
action(unsafe_cell_ptr, unsafe_cell_size, /*frozen*/false)?;
157+
action(unsafe_cell_ptr, unsafe_cell_size, /*frozen*/ false)?;
154158
}
155159
// Update end end_ptr.
156160
end_ptr = unsafe_cell_ptr.wrapping_offset(unsafe_cell_size, this);
@@ -164,7 +168,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
164168
unsafe_cell_action: |place| {
165169
trace!("unsafe_cell_action on {:?}", place.ptr);
166170
// We need a size to go on.
167-
let unsafe_cell_size = this.size_and_align_of_mplace(place)?
171+
let unsafe_cell_size = this
172+
.size_and_align_of_mplace(place)?
168173
.map(|(size, _)| size)
169174
// for extern types, just cover what we can
170175
.unwrap_or_else(|| place.layout.size);
@@ -187,18 +192,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
187192
/// Visiting the memory covered by a `MemPlace`, being aware of
188193
/// whether we are inside an `UnsafeCell` or not.
189194
struct UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
190-
where F: FnMut(MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>
195+
where
196+
F: FnMut(MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>,
191197
{
192198
ecx: &'ecx MiriEvalContext<'mir, 'tcx>,
193199
unsafe_cell_action: F,
194200
}
195201

196-
impl<'ecx, 'mir, 'tcx, F>
197-
ValueVisitor<'mir, 'tcx, Evaluator<'tcx>>
198-
for
199-
UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
202+
impl<'ecx, 'mir, 'tcx, F> ValueVisitor<'mir, 'tcx, Evaluator<'tcx>>
203+
for UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
200204
where
201-
F: FnMut(MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>
205+
F: FnMut(MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>,
202206
{
203207
type V = MPlaceTy<'tcx, Tag>;
204208

@@ -208,11 +212,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
208212
}
209213

210214
// Hook to detect `UnsafeCell`.
211-
fn visit_value(&mut self, v: MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>
212-
{
215+
fn visit_value(&mut self, v: MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
213216
trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty);
214217
let is_unsafe_cell = match v.layout.ty.kind {
215-
ty::Adt(adt, _) => Some(adt.did) == self.ecx.tcx.lang_items().unsafe_cell_type(),
218+
ty::Adt(adt, _) => {
219+
Some(adt.did) == self.ecx.tcx.lang_items().unsafe_cell_type()
220+
}
216221
_ => false,
217222
};
218223
if is_unsafe_cell {
@@ -249,7 +254,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
249254
fn visit_aggregate(
250255
&mut self,
251256
place: MPlaceTy<'tcx, Tag>,
252-
fields: impl Iterator<Item=InterpResult<'tcx, MPlaceTy<'tcx, Tag>>>,
257+
fields: impl Iterator<Item = InterpResult<'tcx, MPlaceTy<'tcx, Tag>>>,
253258
) -> InterpResult<'tcx> {
254259
match place.layout.fields {
255260
layout::FieldPlacement::Array { .. } => {
@@ -259,7 +264,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
259264
}
260265
layout::FieldPlacement::Arbitrary { .. } => {
261266
// Gather the subplaces and sort them before visiting.
262-
let mut places = fields.collect::<InterpResult<'tcx, Vec<MPlaceTy<'tcx, Tag>>>>()?;
267+
let mut places =
268+
fields.collect::<InterpResult<'tcx, Vec<MPlaceTy<'tcx, Tag>>>>()?;
263269
places.sort_by_key(|place| place.ptr.assert_ptr().offset);
264270
self.walk_aggregate(place, places.into_iter().map(Ok))
265271
}
@@ -271,8 +277,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
271277
}
272278

273279
// We have to do *something* for unions.
274-
fn visit_union(&mut self, v: MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>
275-
{
280+
fn visit_union(&mut self, v: MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
276281
// With unions, we fall back to whatever the type says, to hopefully be consistent
277282
// with LLVM IR.
278283
// FIXME: are we consistent, and is this really the behavior we want?
@@ -285,8 +290,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
285290
}
286291

287292
// We should never get to a primitive, but always short-circuit somewhere above.
288-
fn visit_primitive(&mut self, _v: MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx>
289-
{
293+
fn visit_primitive(&mut self, _v: MPlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
290294
bug!("we should always short-circuit before coming to a primitive")
291295
}
292296
}
@@ -296,12 +300,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
296300
fn eval_libc(&mut self, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
297301
self.eval_context_mut()
298302
.eval_path_scalar(&["libc", name])?
299-
.ok_or_else(|| err_unsup_format!("Path libc::{} cannot be resolved.", name).into())
300-
.and_then(|scalar| scalar.not_undef())
303+
.ok_or_else(|| err_unsup_format!("Path libc::{} cannot be resolved.", name))?
304+
.not_undef()
301305
}
302306

303307
/// Helper function to get a `libc` constant as an `i32`.
304308
fn eval_libc_i32(&mut self, name: &str) -> InterpResult<'tcx, i32> {
305-
self.eval_libc(name).and_then(|scalar| scalar.to_i32())
309+
self.eval_libc(name)?.to_i32()
306310
}
307311
}

src/shims/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
135135
let mut bytes = cwd.display().to_string().into_bytes();
136136
// If `size` is smaller or equal than the `bytes.len()`, writing `bytes` plus the
137137
// required null terminator to memory using the `buf` pointer would cause an
138-
// overflow, the desired behavior in this case is to return null.
138+
// overflow. The desired behavior in this case is to return null.
139139
if (bytes.len() as u64) < size {
140140
// We add a `/0` terminator
141141
bytes.push(0);

0 commit comments

Comments
 (0)