Skip to content

Commit ad6af7a

Browse files
committed
Auto merge of #1005 - RalfJung:cleanup, r=RalfJung
cleanup now that borrow checker knows memory is a field @christianpoveda you said, I think, that `fs.rs` could also be cleaned up to longer remove-and-then-add file descriptors from the table? Could you make a PR for that?
2 parents fbc1d91 + 5481afb commit ad6af7a

File tree

6 files changed

+18
-33
lines changed

6 files changed

+18
-33
lines changed

src/eval.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
155155
}
156156
// Store command line as UTF-16 for Windows `GetCommandLineW`.
157157
{
158-
let tcx = &{ ecx.tcx.tcx };
159158
let cmd_utf16: Vec<u16> = cmd.encode_utf16().collect();
160159
let cmd_ptr = ecx.memory.allocate(
161160
Size::from_bytes(cmd_utf16.len() as u64 * 2),
@@ -169,12 +168,12 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
169168
let mut cur_ptr = cmd_ptr;
170169
for &c in cmd_utf16.iter() {
171170
cmd_alloc.write_scalar(
172-
tcx,
171+
&*ecx.tcx,
173172
cur_ptr,
174173
Scalar::from_uint(c, char_size).into(),
175174
char_size,
176175
)?;
177-
cur_ptr = cur_ptr.offset(char_size, tcx)?;
176+
cur_ptr = cur_ptr.offset(char_size, &*ecx.tcx)?;
178177
}
179178
}
180179

src/helpers.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
112112
rng.fill_bytes(&mut data);
113113
}
114114

115-
let tcx = &{this.tcx.tcx};
116-
this.memory.get_mut(ptr.alloc_id)?.write_bytes(tcx, ptr, &data)
115+
this.memory.get_mut(ptr.alloc_id)?.write_bytes(&*this.tcx, ptr, &data)
117116
}
118117

119118
/// Visits the memory covered by `place`, sensitive to freezing: the 3rd parameter
@@ -311,8 +310,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
311310
/// Helper function to get the `TyLayout` of a `libc` type
312311
fn libc_ty_layout(&mut self, name: &str) -> InterpResult<'tcx, TyLayout<'tcx>> {
313312
let this = self.eval_context_mut();
314-
let tcx = &{ this.tcx.tcx };
315-
let ty = this.resolve_path(&["libc", name])?.ty(*tcx);
313+
let ty = this.resolve_path(&["libc", name])?.ty(*this.tcx);
316314
this.layout_of(ty)
317315
}
318316

@@ -325,14 +323,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
325323
) -> InterpResult<'tcx> {
326324
let this = self.eval_context_mut();
327325

328-
let tcx = &{ this.tcx.tcx };
329-
330326
let mut offset = Size::from_bytes(0);
331327

332328
for &imm in imms {
333329
this.write_immediate_to_mplace(
334330
*imm,
335-
place.offset(offset, None, imm.layout, tcx)?,
331+
place.offset(offset, None, imm.layout, &*this.tcx)?,
336332
)?;
337333
offset += imm.layout.size;
338334
}

src/shims/env.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
122122

123123
this.check_no_isolation("getcwd")?;
124124

125-
let tcx = &{ this.tcx.tcx };
126-
127125
let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
128-
let size = this.read_scalar(size_op)?.to_usize(&*tcx)?;
126+
let size = this.read_scalar(size_op)?.to_usize(&*this.tcx)?;
129127
// If we cannot get the current directory, we return null
130128
match env::current_dir() {
131129
Ok(cwd) => {
@@ -142,15 +140,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
142140
// `bytes.len()`, meaning that `bytes` actually fit inside tbe buffer.
143141
this.memory
144142
.get_mut(buf.alloc_id)?
145-
.write_bytes(tcx, buf, &bytes)?;
143+
.write_bytes(&*this.tcx, buf, &bytes)?;
146144
return Ok(Scalar::Ptr(buf));
147145
}
148146
let erange = this.eval_libc("ERANGE")?;
149147
this.set_last_error(erange)?;
150148
}
151149
Err(e) => this.consume_io_error(e)?,
152150
}
153-
Ok(Scalar::ptr_null(&*tcx))
151+
Ok(Scalar::ptr_null(&*this.tcx))
154152
}
155153

156154
fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {

src/shims/foreign_items.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4242

4343
fn malloc(&mut self, size: u64, zero_init: bool, kind: MiriMemoryKind) -> Scalar<Tag> {
4444
let this = self.eval_context_mut();
45-
let tcx = &{ this.tcx.tcx };
4645
if size == 0 {
4746
Scalar::from_int(0, this.pointer_size())
4847
} else {
@@ -55,7 +54,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5554
this.memory
5655
.get_mut(ptr.alloc_id)
5756
.unwrap()
58-
.write_repeat(tcx, ptr, 0, Size::from_bytes(size))
57+
.write_repeat(&*this.tcx, ptr, 0, Size::from_bytes(size))
5958
.unwrap();
6059
}
6160
Scalar::Ptr(ptr)
@@ -90,12 +89,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9089
}
9190
} else {
9291
let old_ptr = this.force_ptr(old_ptr)?;
93-
let memory = &mut this.memory;
9492
if new_size == 0 {
95-
memory.deallocate(old_ptr, None, kind.into())?;
93+
this.memory.deallocate(old_ptr, None, kind.into())?;
9694
Ok(Scalar::from_int(0, this.pointer_size()))
9795
} else {
98-
let new_ptr = memory.reallocate(
96+
let new_ptr = this.memory.reallocate(
9997
old_ptr,
10098
None,
10199
Size::from_bytes(new_size),
@@ -334,7 +332,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
334332
// for the TLS destructors, and of course `eval_main`.
335333
let mir = this.load_mir(f_instance.def, None)?;
336334
let ret_place =
337-
MPlaceTy::dangling(this.layout_of(this.tcx.mk_unit())?, this).into();
335+
MPlaceTy::dangling(this.layout_of(tcx.mk_unit())?, this).into();
338336
this.push_stack_frame(
339337
f_instance,
340338
mir.span,
@@ -471,7 +469,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
471469
"write" => {
472470
let fd = this.read_scalar(args[0])?.to_i32()?;
473471
let buf = this.read_scalar(args[1])?.not_undef()?;
474-
let n = this.read_scalar(args[2])?.to_usize(&*this.tcx)?;
472+
let n = this.read_scalar(args[2])?.to_usize(tcx)?;
475473
trace!("Called write({:?}, {:?}, {:?})", fd, buf, n);
476474
let result = if fd == 1 || fd == 2 {
477475
// stdout/stderr
@@ -993,10 +991,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
993991

994992
fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
995993
let this = self.eval_context_mut();
996-
let tcx = &{ this.tcx.tcx };
997994
let errno_ptr = this.machine.last_error.unwrap();
998995
this.memory.get_mut(errno_ptr.alloc_id)?.write_scalar(
999-
tcx,
996+
&*this.tcx,
1000997
errno_ptr,
1001998
scalar.into(),
1002999
Size::from_bits(32),
@@ -1005,11 +1002,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
10051002

10061003
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
10071004
let this = self.eval_context_mut();
1008-
let tcx = &{ this.tcx.tcx };
10091005
let errno_ptr = this.machine.last_error.unwrap();
10101006
this.memory
10111007
.get(errno_ptr.alloc_id)?
1012-
.read_scalar(tcx, errno_ptr, Size::from_bits(32))?
1008+
.read_scalar(&*this.tcx, errno_ptr, Size::from_bits(32))?
10131009
.not_undef()
10141010
}
10151011

src/shims/fs.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
157157

158158
this.check_no_isolation("read")?;
159159

160-
let tcx = &{ this.tcx.tcx };
161-
162160
let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?;
163161
// Reading zero bytes should not change `buf`
164162
if count == 0 {
@@ -173,7 +171,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
173171
let bytes = this.force_ptr(buf_scalar).and_then(|buf| {
174172
this.memory
175173
.get_mut(buf.alloc_id)?
176-
.get_bytes_mut(tcx, buf, Size::from_bytes(count))
174+
.get_bytes_mut(&*this.tcx, buf, Size::from_bytes(count))
177175
.map(|buffer| handle.file.read(buffer))
178176
});
179177
// Reinsert the file handle
@@ -192,8 +190,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
192190

193191
this.check_no_isolation("write")?;
194192

195-
let tcx = &{ this.tcx.tcx };
196-
197193
let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?;
198194
// Writing zero bytes should not change `buf`
199195
if count == 0 {
@@ -205,7 +201,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
205201
this.remove_handle_and(fd, |mut handle, this| {
206202
let bytes = this.memory.get(buf.alloc_id).and_then(|alloc| {
207203
alloc
208-
.get_bytes(tcx, buf, Size::from_bytes(count))
204+
.get_bytes(&*this.tcx, buf, Size::from_bytes(count))
209205
.map(|bytes| handle.file.write(bytes).map(|bytes| bytes as i64))
210206
});
211207
this.machine.file_handler.handles.insert(fd, handle);

src/shims/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2828
// (as opposed to through a place), we have to remember to erase any tag
2929
// that might still hang around!
3030

31-
let intrinsic_name = &*this.tcx.item_name(instance.def_id()).as_str();
31+
let intrinsic_name = &*tcx.item_name(instance.def_id()).as_str();
3232
match intrinsic_name {
3333
"arith_offset" => {
3434
let offset = this.read_scalar(args[1])?.to_isize(this)?;

0 commit comments

Comments
 (0)