You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use core::fmt::Debug;fnfoo(_:&Box<dynDebug>){}fnmain(){foo(&{Box::new(())});foo(&(Box::new(())));foo(&Box::new(()));}
I expected to see this happen: The same behavior from all three calls to foo. Based on my reading of RFC 401 and the reference, all three should compile; but independent of that, it seems that all three should behave the same.
Instead, this happened: The first call coerces successfully while the other two fail.
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
--> src/main.rs:6:9
|
6 | foo(&( Box::new(()) ));
| --- ^^^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `()`
| |
| arguments to this function are incorrect
|
= note: expected reference `&Box<(dyn Debug + 'static)>`
found reference `&Box<()>`
note: function defined here
--> src/main.rs:2:4
|
2 | fn foo(_: &Box<dyn Debug>) {}
| ^^^ ------------------
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
--> src/main.rs:7:9
|
7 | foo(& Box::new(()) );
| --- ^^^^^^^^^^^^^^^ expected trait object `dyn Debug`, found `()`
| |
| arguments to this function are incorrect
|
= note: expected reference `&Box<(dyn Debug + 'static)>`
found reference `&Box<()>`
note: function defined here
--> src/main.rs:2:4
|
2 | fn foo(_: &Box<dyn Debug>) {}
| ^^^ ------------------
For more information about this error, try `rustc --explain E0308`.
@steffahn pointed out that this can't actually happen (without impractical levels of magic); I'll try to file an issue against the reference if someone doesn't beat me to it.
The different behavior based on the presence of {} is still surprising, but can probably also be considered an incorrect or incomplete documentation issue; I'll probably close this unless there is some indication that something deeper is actually going on. (There are a number of issues open around blocks influencing Deref coercion that I thought might be related.)
I tried this code:
I expected to see this happen: The same behavior from all three calls to
foo
. Based on my reading of RFC 401 and the reference, all three should compile; but independent of that, it seems that all three should behave the same.Instead, this happened: The first call coerces successfully while the other two fail.
Meta
Playground (stable 1.63.0, Beta version: 1.64.0-beta.7 (2022-09-16 b31188e), Nightly version: 1.66.0-nightly (2022-09-17 98ad6a5)).
@rustbot label +A-coercions
The text was updated successfully, but these errors were encountered: