Skip to content

Cast error doesn't suggest boxed traits but maybe it should. #44602

Open
@quadrupleslap

Description

@quadrupleslap

I have a function call that's something like:

test(&[&"nani?" as &_, &"=_=".to_owned() as &_]);

The first &_ is interpreted as a no-op cast, and the second is interpreted as a &String -> &&str. I did this to try and get a list of boxed traits, but the error message is pretty misleading unless you notice what the compiler's actually inferring, and almost makes it sound like as can't be used in this situation at all:

error[E0605]: non-primitive cast: `&std::string::String` as `&&str`
...
  = note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait

I don't know how, but it would be nice if it could automagically recommend boxed traits in similar situations.

Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-diagnosticsArea: Messages for errors, warnings, and lintsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-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