Skip to content

Bad error message on trait coercion #95972

Closed
@jdonszelmann

Description

@jdonszelmann

Given the following code:
play.rust-lang.org

trait Fooable {}
trait Barable: Fooable {}

struct Foo;
impl Fooable for Foo {}
impl Barable for Foo {}

fn get_fooable(hm: Option<Box<dyn Barable>>) -> Box<dyn Fooable> {
    hm.unwrap_or_else(|| Box::new(Foo))
}

fn main() {
    get_fooable(Some(Box::new(Foo)));
}

The current output is:

error[[E0658]](https://doc.rust-lang.org/stable/error-index.html#E0658): trait upcasting coercion is experimental
 [--> src/main.rs:9:5
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3ddfef978d2cb89a32b6a59a6e94d98b#)  |
9 |     hm.unwrap_or_else(|| Box::new(Foo))
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: [see issue #65991 <https://github.com/rust-lang/rust/issues/65991>](https://github.com/rust-lang/rust/issues/65991) for more information

For more information about this error, try `rustc --explain E0658`.
error: could not compile `playground` due to previous error

It should name the traits are attempted to be coerced, maybe tell me why that's not allowed (yet), etc. Right now it just highlights
the entire line. The original example I had that made me run into this issue had an even longer expression there (multiple chained
iterator methods) which just got all highlighted as one big block saying "somewhere here trait coercion is used and it's not allowed". Note that I understand why it's not allowed, I was just disappointed by the error message.

I already talked to @estebank about this, and they also thought this output may be problematic. Even though the feature may be unstable, it's quite easy to arrive at a situation where you may get this error.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions