Additional trait restrictions prevent use of trait methods (original) (raw)

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 = Box::new(Foo) as Box; let foo = &foo; let foo = foo.downcast_ref::().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).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::().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:📑:Send + std:📑:Sync> in the current scope --> src/main.rs:23:19 | 23 | let foo = foo.downcast_ref::().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)