Skip to content

Commit 30859d9

Browse files
committed
replace as_file_handle by as_any
1 parent b7294a4 commit 30859d9

File tree

1 file changed

+38
-14
lines changed
  • src/tools/miri/src/shims/unix

1 file changed

+38
-14
lines changed

src/tools/miri/src/shims/unix/fs.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ pub struct FileHandle {
2727
pub trait FileDescriptor: std::fmt::Debug + helpers::AsAny {
2828
fn name(&self) -> &'static str;
2929

30-
fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> {
31-
throw_unsup_format!("{} cannot be used as FileHandle", self.name());
32-
}
33-
3430
fn read<'tcx>(
3531
&mut self,
3632
_communicate_allowed: bool,
@@ -79,10 +75,6 @@ impl FileDescriptor for FileHandle {
7975
"FILE"
8076
}
8177

82-
fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> {
83-
Ok(self)
84-
}
85-
8678
fn read<'tcx>(
8779
&mut self,
8880
communicate_allowed: bool,
@@ -687,7 +679,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
687679
} else if this.tcx.sess.target.os == "macos" && cmd == this.eval_libc_i32("F_FULLFSYNC") {
688680
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {
689681
// FIXME: Support fullfsync for all FDs
690-
let FileHandle { file, writable } = file_descriptor.as_file_handle()?;
682+
let FileHandle { file, writable } =
683+
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
684+
err_unsup_format!(
685+
"`F_FULLFSYNC` is only supported on file-backed file descriptors"
686+
)
687+
})?;
691688
let io_result = maybe_sync_file(file, *writable, File::sync_all);
692689
this.try_unwrap_io_result(io_result)
693690
} else {
@@ -1523,7 +1520,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15231520
Ok(Scalar::from_i32(
15241521
if let Some(file_descriptor) = this.machine.file_handler.handles.get_mut(&fd) {
15251522
// FIXME: Support ftruncate64 for all FDs
1526-
let FileHandle { file, writable } = file_descriptor.as_file_handle()?;
1523+
let FileHandle { file, writable } =
1524+
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
1525+
err_unsup_format!(
1526+
"`ftruncate64` is only supported on file-backed file descriptors"
1527+
)
1528+
})?;
15271529
if *writable {
15281530
if let Ok(length) = length.try_into() {
15291531
let result = file.set_len(length);
@@ -1564,7 +1566,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15641566

15651567
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {
15661568
// FIXME: Support fsync for all FDs
1567-
let FileHandle { file, writable } = file_descriptor.as_file_handle()?;
1569+
let FileHandle { file, writable } =
1570+
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
1571+
err_unsup_format!("`fsync` is only supported on file-backed file descriptors")
1572+
})?;
15681573
let io_result = maybe_sync_file(file, *writable, File::sync_all);
15691574
this.try_unwrap_io_result(io_result)
15701575
} else {
@@ -1586,7 +1591,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15861591

15871592
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {
15881593
// FIXME: Support fdatasync for all FDs
1589-
let FileHandle { file, writable } = file_descriptor.as_file_handle()?;
1594+
let FileHandle { file, writable } =
1595+
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
1596+
err_unsup_format!(
1597+
"`fdatasync` is only supported on file-backed file descriptors"
1598+
)
1599+
})?;
15901600
let io_result = maybe_sync_file(file, *writable, File::sync_data);
15911601
this.try_unwrap_io_result(io_result)
15921602
} else {
@@ -1631,7 +1641,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
16311641

16321642
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {
16331643
// FIXME: Support sync_data_range for all FDs
1634-
let FileHandle { file, writable } = file_descriptor.as_file_handle()?;
1644+
let FileHandle { file, writable } =
1645+
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
1646+
err_unsup_format!(
1647+
"`sync_data_range` is only supported on file-backed file descriptors"
1648+
)
1649+
})?;
16351650
let io_result = maybe_sync_file(file, *writable, File::sync_data);
16361651
Ok(Scalar::from_i32(this.try_unwrap_io_result(io_result)?))
16371652
} else {
@@ -1935,7 +1950,16 @@ impl FileMetadata {
19351950
) -> InterpResult<'tcx, Option<FileMetadata>> {
19361951
let option = ecx.machine.file_handler.handles.get(&fd);
19371952
let file = match option {
1938-
Some(file_descriptor) => &file_descriptor.as_file_handle()?.file,
1953+
Some(file_descriptor) =>
1954+
&file_descriptor
1955+
.as_any()
1956+
.downcast_ref::<FileHandle>()
1957+
.ok_or_else(|| {
1958+
err_unsup_format!(
1959+
"obtaining metadata is only supported on file-backed file descriptors"
1960+
)
1961+
})?
1962+
.file,
19391963
None => return ecx.handle_not_found().map(|_: i32| None),
19401964
};
19411965
let metadata = file.metadata();

0 commit comments

Comments
 (0)