Skip to content

Commit ba64f48

Browse files
committed
Fix parameter of io error helper function
`set_last_error_from_io_error` works with only the error kind, and discards the payload. Fix its signature to make it explicit.
1 parent 892f706 commit ba64f48

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

src/helpers.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
460460
this.read_scalar(&errno_place.into())?.check_init()
461461
}
462462

463-
/// Sets the last OS error using a `std::io::Error`. This function tries to produce the most
463+
/// Sets the last OS error using a `std::io::ErrorKind`. This function tries to produce the most
464464
/// similar OS error from the `std::io::ErrorKind` and sets it as the last OS error.
465-
fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
465+
fn set_last_error_from_io_error(&mut self, err_kind: std::io::ErrorKind) -> InterpResult<'tcx> {
466466
use std::io::ErrorKind::*;
467467
let this = self.eval_context_mut();
468468
let target = &this.tcx.sess.target;
469469
let target_os = &target.os;
470470
let last_error = if target.families.contains(&"unix".to_owned()) {
471-
this.eval_libc(match e.kind() {
471+
this.eval_libc(match err_kind {
472472
ConnectionRefused => "ECONNREFUSED",
473473
ConnectionReset => "ECONNRESET",
474474
PermissionDenied => "EPERM",
@@ -484,18 +484,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
484484
AlreadyExists => "EEXIST",
485485
WouldBlock => "EWOULDBLOCK",
486486
_ => {
487-
throw_unsup_format!("io error {} cannot be transformed into a raw os error", e)
487+
throw_unsup_format!(
488+
"io error {:?} cannot be transformed into a raw os error",
489+
err_kind
490+
)
488491
}
489492
})?
490493
} else if target.families.contains(&"windows".to_owned()) {
491494
// FIXME: we have to finish implementing the Windows equivalent of this.
492495
this.eval_windows(
493496
"c",
494-
match e.kind() {
497+
match err_kind {
495498
NotFound => "ERROR_FILE_NOT_FOUND",
496499
_ => throw_unsup_format!(
497-
"io error {} cannot be transformed into a raw os error",
498-
e
500+
"io error {:?} cannot be transformed into a raw os error",
501+
err_kind
499502
),
500503
},
501504
)?
@@ -521,7 +524,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
521524
match result {
522525
Ok(ok) => Ok(ok),
523526
Err(e) => {
524-
self.eval_context_mut().set_last_error_from_io_error(e)?;
527+
self.eval_context_mut().set_last_error_from_io_error(e.kind())?;
525528
Ok((-1).into())
526529
}
527530
}

src/shims/env.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::convert::TryFrom;
22
use std::env;
33
use std::ffi::{OsStr, OsString};
4-
use std::io::{Error, ErrorKind};
4+
use std::io::ErrorKind;
55

66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_mir::interpret::Pointer;
@@ -324,8 +324,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
324324

325325
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
326326
this.reject_in_isolation("getcwd", reject_with)?;
327-
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
328-
this.set_last_error_from_io_error(err)?;
327+
this.set_last_error_from_io_error(ErrorKind::NotFound)?;
329328
return Ok(Scalar::null_ptr(&*this.tcx));
330329
}
331330

@@ -340,7 +339,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
340339
let erange = this.eval_libc("ERANGE")?;
341340
this.set_last_error(erange)?;
342341
}
343-
Err(e) => this.set_last_error_from_io_error(e)?,
342+
Err(e) => this.set_last_error_from_io_error(e.kind())?,
344343
}
345344

346345
Ok(Scalar::null_ptr(&*this.tcx))
@@ -357,8 +356,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
357356

358357
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
359358
this.reject_in_isolation("GetCurrentDirectoryW", reject_with)?;
360-
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
361-
this.set_last_error_from_io_error(err)?;
359+
this.set_last_error_from_io_error(ErrorKind::NotFound)?;
362360
return Ok(0);
363361
}
364362

@@ -369,7 +367,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
369367
match env::current_dir() {
370368
Ok(cwd) =>
371369
return Ok(windows_check_buffer_size(this.write_path_to_wide_str(&cwd, buf, size)?)),
372-
Err(e) => this.set_last_error_from_io_error(e)?,
370+
Err(e) => this.set_last_error_from_io_error(e.kind())?,
373371
}
374372
Ok(0)
375373
}
@@ -384,8 +382,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
384382

385383
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
386384
this.reject_in_isolation("chdir", reject_with)?;
387-
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
388-
this.set_last_error_from_io_error(err)?;
385+
this.set_last_error_from_io_error(ErrorKind::NotFound)?;
389386

390387
return Ok(-1);
391388
}
@@ -395,7 +392,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
395392
match env::set_current_dir(path) {
396393
Ok(()) => Ok(0),
397394
Err(e) => {
398-
this.set_last_error_from_io_error(e)?;
395+
this.set_last_error_from_io_error(e.kind())?;
399396
Ok(-1)
400397
}
401398
}
@@ -413,8 +410,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
413410

414411
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
415412
this.reject_in_isolation("SetCurrentDirectoryW", reject_with)?;
416-
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
417-
this.set_last_error_from_io_error(err)?;
413+
this.set_last_error_from_io_error(ErrorKind::NotFound)?;
418414

419415
return Ok(0);
420416
}
@@ -424,7 +420,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
424420
match env::set_current_dir(path) {
425421
Ok(()) => Ok(1),
426422
Err(e) => {
427-
this.set_last_error_from_io_error(e)?;
423+
this.set_last_error_from_io_error(e.kind())?;
428424
Ok(0)
429425
}
430426
}

src/shims/posix/fs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
634634
match dup_result {
635635
Ok(dup_fd) => Ok(fh.insert_fd_with_min_fd(dup_fd, start)),
636636
Err(e) => {
637-
this.set_last_error_from_io_error(e)?;
637+
this.set_last_error_from_io_error(e.kind())?;
638638
Ok(-1)
639639
}
640640
}
@@ -707,7 +707,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
707707
Ok(read_bytes)
708708
}
709709
Err(e) => {
710-
this.set_last_error_from_io_error(e)?;
710+
this.set_last_error_from_io_error(e.kind())?;
711711
Ok(-1)
712712
}
713713
}
@@ -1118,7 +1118,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11181118
Ok(Scalar::from_machine_usize(id, this))
11191119
}
11201120
Err(e) => {
1121-
this.set_last_error_from_io_error(e)?;
1121+
this.set_last_error_from_io_error(e.kind())?;
11221122
Ok(Scalar::null_ptr(this))
11231123
}
11241124
}
@@ -1462,7 +1462,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
14621462
Ok(path_bytes.len().try_into().unwrap())
14631463
}
14641464
Err(e) => {
1465-
this.set_last_error_from_io_error(e)?;
1465+
this.set_last_error_from_io_error(e.kind())?;
14661466
Ok(-1)
14671467
}
14681468
}
@@ -1526,7 +1526,7 @@ impl FileMetadata {
15261526
let metadata = match metadata {
15271527
Ok(metadata) => metadata,
15281528
Err(e) => {
1529-
ecx.set_last_error_from_io_error(e)?;
1529+
ecx.set_last_error_from_io_error(e.kind())?;
15301530
return Ok(None);
15311531
}
15321532
};

0 commit comments

Comments
 (0)