Skip to content

Bad error message on trait coercion #95972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jdonszelmann opened this issue Apr 12, 2022 · 1 comment · Fixed by #96378
Closed

Bad error message on trait coercion #95972

jdonszelmann opened this issue Apr 12, 2022 · 1 comment · Fixed by #96378
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jdonszelmann
Copy link
Contributor

jdonszelmann commented Apr 12, 2022

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.

@jdonszelmann jdonszelmann added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 12, 2022
@estebank estebank added D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Apr 12, 2022
@compiler-errors
Copy link
Member

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants