diff --git a/src/shims/unix/fs.rs b/src/shims/unix/fs.rs index dc31237a3199..c9f35c048917 100644 --- a/src/shims/unix/fs.rs +++ b/src/shims/unix/fs.rs @@ -24,38 +24,56 @@ struct FileHandle { } trait FileDescriptor: std::fmt::Debug { - fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle>; + fn name(&self) -> &'static str; + + fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { + throw_unsup_format!("{} cannot be used as FileHandle", self.name()); + } fn read<'tcx>( &mut self, - communicate_allowed: bool, - bytes: &mut [u8], - ) -> InterpResult<'tcx, io::Result>; + _communicate_allowed: bool, + _bytes: &mut [u8], + ) -> InterpResult<'tcx, io::Result> { + throw_unsup_format!("cannot read from {}", self.name()); + } fn write<'tcx>( &self, - communicate_allowed: bool, - bytes: &[u8], - ) -> InterpResult<'tcx, io::Result>; + _communicate_allowed: bool, + _bytes: &[u8], + ) -> InterpResult<'tcx, io::Result> { + throw_unsup_format!("cannot write to {}", self.name()); + } fn seek<'tcx>( &mut self, - communicate_allowed: bool, - offset: SeekFrom, - ) -> InterpResult<'tcx, io::Result>; + _communicate_allowed: bool, + _offset: SeekFrom, + ) -> InterpResult<'tcx, io::Result> { + throw_unsup_format!("cannot seek on {}", self.name()); + } fn close<'tcx>( self: Box, _communicate_allowed: bool, - ) -> InterpResult<'tcx, io::Result>; + ) -> InterpResult<'tcx, io::Result> { + throw_unsup_format!("cannot close {}", self.name()); + } fn dup(&mut self) -> io::Result>; #[cfg(unix)] - fn as_unix_host_fd(&self) -> Option; + fn as_unix_host_fd(&self) -> Option { + None + } } impl FileDescriptor for FileHandle { + fn name(&self) -> &'static str { + "FILE" + } + fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { Ok(self) } @@ -126,8 +144,8 @@ impl FileDescriptor for FileHandle { } impl FileDescriptor for io::Stdin { - fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { - throw_unsup_format!("stdin cannot be used as FileHandle"); + fn name(&self) -> &'static str { + "stdin" } fn read<'tcx>( @@ -142,29 +160,6 @@ impl FileDescriptor for io::Stdin { Ok(Read::read(self, bytes)) } - fn write<'tcx>( - &self, - _communicate_allowed: bool, - _bytes: &[u8], - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("cannot write to stdin"); - } - - fn seek<'tcx>( - &mut self, - _communicate_allowed: bool, - _offset: SeekFrom, - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("cannot seek on stdin"); - } - - fn close<'tcx>( - self: Box, - _communicate_allowed: bool, - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("stdin cannot be closed"); - } - fn dup(&mut self) -> io::Result> { Ok(Box::new(io::stdin())) } @@ -176,16 +171,8 @@ impl FileDescriptor for io::Stdin { } impl FileDescriptor for io::Stdout { - fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { - throw_unsup_format!("stdout cannot be used as FileHandle"); - } - - fn read<'tcx>( - &mut self, - _communicate_allowed: bool, - _bytes: &mut [u8], - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("cannot read from stdout"); + fn name(&self) -> &'static str { + "stdout" } fn write<'tcx>( @@ -205,21 +192,6 @@ impl FileDescriptor for io::Stdout { Ok(result) } - fn seek<'tcx>( - &mut self, - _communicate_allowed: bool, - _offset: SeekFrom, - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("cannot seek on stdout"); - } - - fn close<'tcx>( - self: Box, - _communicate_allowed: bool, - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("stdout cannot be closed"); - } - fn dup(&mut self) -> io::Result> { Ok(Box::new(io::stdout())) } @@ -231,16 +203,8 @@ impl FileDescriptor for io::Stdout { } impl FileDescriptor for io::Stderr { - fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { - throw_unsup_format!("stderr cannot be used as FileHandle"); - } - - fn read<'tcx>( - &mut self, - _communicate_allowed: bool, - _bytes: &mut [u8], - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("cannot read from stderr"); + fn name(&self) -> &'static str { + "stderr" } fn write<'tcx>( @@ -253,21 +217,6 @@ impl FileDescriptor for io::Stderr { Ok(Write::write(&mut { self }, bytes)) } - fn seek<'tcx>( - &mut self, - _communicate_allowed: bool, - _offset: SeekFrom, - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("cannot seek on stderr"); - } - - fn close<'tcx>( - self: Box, - _communicate_allowed: bool, - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("stderr cannot be closed"); - } - fn dup(&mut self) -> io::Result> { Ok(Box::new(io::stderr())) } @@ -282,16 +231,8 @@ impl FileDescriptor for io::Stderr { struct DummyOutput; impl FileDescriptor for DummyOutput { - fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { - throw_unsup_format!("stderr and stdout cannot be used as FileHandle"); - } - - fn read<'tcx>( - &mut self, - _communicate_allowed: bool, - _bytes: &mut [u8], - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("cannot read from stderr or stdout"); + fn name(&self) -> &'static str { + "stderr and stdout" } fn write<'tcx>( @@ -303,29 +244,9 @@ impl FileDescriptor for DummyOutput { Ok(Ok(bytes.len())) } - fn seek<'tcx>( - &mut self, - _communicate_allowed: bool, - _offset: SeekFrom, - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("cannot seek on stderr or stdout"); - } - - fn close<'tcx>( - self: Box, - _communicate_allowed: bool, - ) -> InterpResult<'tcx, io::Result> { - throw_unsup_format!("stderr and stdout cannot be closed"); - } - fn dup<'tcx>(&mut self) -> io::Result> { Ok(Box::new(DummyOutput)) } - - #[cfg(unix)] - fn as_unix_host_fd(&self) -> Option { - None - } } #[derive(Debug)] diff --git a/tests/fail/fs/close_stdout.rs b/tests/fail/fs/close_stdout.rs index 86a6239f5f36..e4eab5fd6964 100644 --- a/tests/fail/fs/close_stdout.rs +++ b/tests/fail/fs/close_stdout.rs @@ -7,6 +7,6 @@ fn main() { unsafe { - libc::close(1); //~ ERROR: stdout cannot be closed + libc::close(1); //~ ERROR: cannot close stdout } } diff --git a/tests/fail/fs/close_stdout.stderr b/tests/fail/fs/close_stdout.stderr index 08f744a5c458..eb2c54e05f1f 100644 --- a/tests/fail/fs/close_stdout.stderr +++ b/tests/fail/fs/close_stdout.stderr @@ -1,8 +1,8 @@ -error: unsupported operation: stdout cannot be closed +error: unsupported operation: cannot close stdout --> $DIR/close_stdout.rs:LL:CC | LL | libc::close(1); - | ^^^^^^^^^^^^^^ stdout cannot be closed + | ^^^^^^^^^^^^^^ cannot close stdout | = help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support = note: backtrace: