Post

The fact that `&Box<dyn Any>` is coercible to `&dyn Any` is such a big footgun ...

0 reposts16 likes

Usually you want a reference to the `dyn Any` inside the box, `&Box<dyn Any>` will unfortunately be a reference to the box itself, just that the box is now unsized to `dyn Any`.

Ah d'oh, in my head i was assuming you're getting the reference inside the box, didn't even think about getting the reference to the box. That is a massive footgun indeed

Yes, that's reasonable to assume because deref coercions literally let you do that. It's just that the impl Any for Box<dyn Any> causes unsizing coercion to take precedence. We should look into changing this behavior in future editions.

This should be upgraded to warn by default and promoted to rustc imo. Is it ever correct to want the other thing? (Still MaybeIncorrect fix because it can break existing code I guess)

I think it would be a code smell to do the other thing on purpose, but you could write correct code that did so if you really wanted to, and maybe someone has. Regardless, I agree that warning by default would make a lot of sense. Not sure how to make that happen, though.

For rustc, open a PR and nominate for lang. The most natural order would be if it were upgraded in clippy first. It isn't strictly necessary but gives confidence in the impl. If you face resistance to that, lang could weigh in and say we want this, assuming the rest of the team agrees with me.