- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.9k
 
Closed
Description
Adding Send + Sync restrictions to an Any trait object causes the trait object to lose the downcast_ref method
https://play.rust-lang.org/?gist=9c665d721a9c6a2845f55e56d97ffe27&version=stable
use std::any::Any;
struct Foo;
// This compiles
fn main() {
    let foo: Box<Any> = Box::new(Foo) as Box<Any>;
    let foo = &foo;
    let foo = foo.downcast_ref::<Foo>().unwrap();
}
// This compiles
fn main_2() {
    let foo: Box<Any + Send + Sync> = Box::new(Foo) as Box<Any + Send + Sync>;
    let foo = &foo;
    let foo = Any::downcast_ref::<Foo>(foo).unwrap();
}
// This doesn't compile
fn main_3() {
    let foo: Box<Any + Send + Sync> = Box::new(Foo) as Box<Any + Send + Sync>;
    let foo = &foo;
    let foo = foo.downcast_ref::<Foo>().unwrap();
}I would expect main_3 to compile, because main_2 compiles, however main_3 fails to compile with the error
error[E0599]: no method named `downcast_ref` found for type `&std::boxed::Box<std::any::Any + std::marker::Send + std::marker::Sync>` in the current scope
  --> src/main.rs:23:19
   |
23 |     let foo = foo.downcast_ref::<Foo>().unwrap();
   |                   ^^^^^^^^^^^^
error: aborting due to previous error(s)
error: Could not compile `playground`.
To learn more, run the command again with --verbose.Meta
$ rustc --version
rustc 1.19.0 (0ade339 2017-07-17)
Metadata
Metadata
Assignees
Labels
No labels