Skip to content
/ rust Public
forked from rust-lang/rust

Commit d4fbaa6

Browse files
committed
Auto merge of rust-lang#3022 - RalfJung:upcast, r=compiler-errors
replace AsAny hack by trait upcasting :)
2 parents 43c869d + c20c381 commit d4fbaa6

File tree

4 files changed

+22
-31
lines changed

4 files changed

+22
-31
lines changed

src/tools/miri/src/helpers.rs

-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pub mod convert;
22

3-
use std::any::Any;
43
use std::cmp;
54
use std::iter;
65
use std::num::NonZeroUsize;
@@ -25,24 +24,6 @@ use rand::RngCore;
2524

2625
use crate::*;
2726

28-
/// A trait to work around not having trait object upcasting:
29-
/// Add `AsAny` as supertrait and your trait objects can be turned into `&dyn Any` on which you can
30-
/// then call `downcast`.
31-
pub trait AsAny: Any {
32-
fn as_any(&self) -> &dyn Any;
33-
fn as_any_mut(&mut self) -> &mut dyn Any;
34-
}
35-
impl<T: Any> AsAny for T {
36-
#[inline(always)]
37-
fn as_any(&self) -> &dyn Any {
38-
self
39-
}
40-
#[inline(always)]
41-
fn as_any_mut(&mut self) -> &mut dyn Any {
42-
self
43-
}
44-
}
45-
4627
// This mapping should match `decode_error_kind` in
4728
// <https://github.com/rust-lang/rust/blob/master/library/std/src/sys/unix/mod.rs>.
4829
const UNIX_IO_ERROR_TABLE: &[(&str, std::io::ErrorKind)] = {

src/tools/miri/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![feature(round_ties_even)]
1111
#![feature(os_str_bytes)]
1212
#![feature(lint_reasons)]
13+
#![feature(trait_upcasting)]
1314
// Configure clippy and other lints
1415
#![allow(
1516
clippy::collapsible_else_if,

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

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::any::Any;
12
use std::borrow::Cow;
23
use std::collections::BTreeMap;
34
use std::convert::TryInto;
@@ -24,7 +25,7 @@ pub struct FileHandle {
2425
writable: bool,
2526
}
2627

27-
pub trait FileDescriptor: std::fmt::Debug + helpers::AsAny {
28+
pub trait FileDescriptor: std::fmt::Debug + Any {
2829
fn name(&self) -> &'static str;
2930

3031
fn read<'tcx>(
@@ -72,6 +73,18 @@ pub trait FileDescriptor: std::fmt::Debug + helpers::AsAny {
7273
}
7374
}
7475

76+
impl dyn FileDescriptor {
77+
#[inline(always)]
78+
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
79+
(self as &dyn Any).downcast_ref()
80+
}
81+
82+
#[inline(always)]
83+
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
84+
(self as &mut dyn Any).downcast_mut()
85+
}
86+
}
87+
7588
impl FileDescriptor for FileHandle {
7689
fn name(&self) -> &'static str {
7790
"FILE"
@@ -689,7 +702,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
689702
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {
690703
// FIXME: Support fullfsync for all FDs
691704
let FileHandle { file, writable } =
692-
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
705+
file_descriptor.downcast_ref::<FileHandle>().ok_or_else(|| {
693706
err_unsup_format!(
694707
"`F_FULLFSYNC` is only supported on file-backed file descriptors"
695708
)
@@ -1522,7 +1535,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15221535
if let Some(file_descriptor) = this.machine.file_handler.handles.get_mut(&fd) {
15231536
// FIXME: Support ftruncate64 for all FDs
15241537
let FileHandle { file, writable } =
1525-
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
1538+
file_descriptor.downcast_ref::<FileHandle>().ok_or_else(|| {
15261539
err_unsup_format!(
15271540
"`ftruncate64` is only supported on file-backed file descriptors"
15281541
)
@@ -1568,7 +1581,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15681581
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {
15691582
// FIXME: Support fsync for all FDs
15701583
let FileHandle { file, writable } =
1571-
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
1584+
file_descriptor.downcast_ref::<FileHandle>().ok_or_else(|| {
15721585
err_unsup_format!("`fsync` is only supported on file-backed file descriptors")
15731586
})?;
15741587
let io_result = maybe_sync_file(file, *writable, File::sync_all);
@@ -1593,7 +1606,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15931606
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {
15941607
// FIXME: Support fdatasync for all FDs
15951608
let FileHandle { file, writable } =
1596-
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
1609+
file_descriptor.downcast_ref::<FileHandle>().ok_or_else(|| {
15971610
err_unsup_format!(
15981611
"`fdatasync` is only supported on file-backed file descriptors"
15991612
)
@@ -1643,7 +1656,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
16431656
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {
16441657
// FIXME: Support sync_data_range for all FDs
16451658
let FileHandle { file, writable } =
1646-
file_descriptor.as_any().downcast_ref::<FileHandle>().ok_or_else(|| {
1659+
file_descriptor.downcast_ref::<FileHandle>().ok_or_else(|| {
16471660
err_unsup_format!(
16481661
"`sync_data_range` is only supported on file-backed file descriptors"
16491662
)
@@ -1953,7 +1966,6 @@ impl FileMetadata {
19531966
let file = match option {
19541967
Some(file_descriptor) =>
19551968
&file_descriptor
1956-
.as_any()
19571969
.downcast_ref::<FileHandle>()
19581970
.ok_or_else(|| {
19591971
err_unsup_format!(

src/tools/miri/src/shims/unix/linux/fd.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::cell::Cell;
2+
13
use rustc_middle::ty::ScalarInt;
24

35
use crate::*;
@@ -7,8 +9,6 @@ use socketpair::SocketPair;
79

810
use shims::unix::fs::EvalContextExt as _;
911

10-
use std::cell::Cell;
11-
1212
pub mod epoll;
1313
pub mod event;
1414
pub mod socketpair;
@@ -81,7 +81,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
8181

8282
if let Some(epfd) = this.machine.file_handler.handles.get_mut(&epfd) {
8383
let epfd = epfd
84-
.as_any_mut()
8584
.downcast_mut::<Epoll>()
8685
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_ctl`"))?;
8786

@@ -93,7 +92,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
9392
} else if op == epoll_ctl_del {
9493
if let Some(epfd) = this.machine.file_handler.handles.get_mut(&epfd) {
9594
let epfd = epfd
96-
.as_any_mut()
9795
.downcast_mut::<Epoll>()
9896
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_ctl`"))?;
9997

@@ -154,7 +152,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
154152

155153
if let Some(epfd) = this.machine.file_handler.handles.get_mut(&epfd) {
156154
let _epfd = epfd
157-
.as_any_mut()
158155
.downcast_mut::<Epoll>()
159156
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_wait`"))?;
160157

0 commit comments

Comments
 (0)