Closed
Description
Given the following code (playground):
trait Bar {}
impl Bar for String {}
fn main() {
let s = String::from("hey");
let x: &dyn Bar = &s;
x.as_ref();
}
The output on 1.53.0-nightly (2021-04-29 478a07df05e3fe8df964)
is:
Long diagnostic
error[E0599]: the method `as_ref` exists for reference `&dyn Bar`, but its trait bounds were not satisfied
--> src/main.rs:7:5
|
1 | trait Bar {}
| --------- doesn't satisfy `dyn Bar: AsRef<_>`
...
7 | x.as_ref();
| ^^^^^^ method cannot be called on `&dyn Bar` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`dyn Bar: AsRef<_>`
which is required by `&dyn Bar: AsRef<_>`
help: consider wrapping the receiver expression with the appropriate type
|
7 | Box::new(x).as_ref();
| ^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Pin::new(x).as_ref();
| ^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Arc::new(x).as_ref();
| ^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Rc::new(x).as_ref();
| ^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Box::new(&mut x).as_ref();
| ^^^^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Pin::new(&mut x).as_ref();
| ^^^^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Arc::new(&mut x).as_ref();
| ^^^^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Rc::new(&mut x).as_ref();
| ^^^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Box::new(&x).as_ref();
| ^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Pin::new(&x).as_ref();
| ^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Arc::new(&x).as_ref();
| ^^^^^^^^^^ ^
help: consider wrapping the receiver expression with the appropriate type
|
7 | Rc::new(&x).as_ref();
| ^^^^^^^^^ ^
Box
, Pin
, Arc
, and Rc
are suggested three times each: once for x
, once for &x
, and once for &mut x
. I'm not sure if this is a problem, but this seems incredibly verbose to me.
Note that the output on stable 1.51 and beta 1.52 is different:
error[E0599]: the method `as_ref` exists for reference `&dyn Bar`, but its trait bounds were not satisfied
--> src/main.rs:7:5
|
1 | trait Bar {}
| --------- doesn't satisfy `dyn Bar: AsRef<_>`
...
7 | x.as_ref();
| ^^^^^^ method cannot be called on `&dyn Bar` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`dyn Bar: AsRef<_>`
which is required by `&dyn Bar: AsRef<_>`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `as_ref`, perhaps you need to implement it:
candidate #1: `AsRef`