Skip to content
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

Diagnostics for conversions erroneously say that 'as' only works between primitive types. #47136

Open
alercah opened this issue Jan 2, 2018 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alercah
Copy link
Contributor

alercah commented Jan 2, 2018

See https://play.rust-lang.org/?gist=7194dac428b4355e3f2872e9c0206800&version=stable

The note says "an as expression can only be used to convert between primitive types. Consider using the From trait", but as show by the expression Baz as i32, this is not true.

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. 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 Feb 6, 2018
@ArekPiekarz
Copy link
Contributor

Another example of this problem is here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=068d6b1e9a91bb347071acc829d88dd7

It demonstrates that it is possible to use as cast for conversion between Rc types, which are not primitive. Namely a Rc<Bar> type which implements FooObserver trait can be cast to Rc<dyn FooObserver> when cloning it.

@awaitlink
Copy link
Contributor

This note is also unhelpful with casting to &dyn Trait/&mut dyn Trait (playground):

trait Trait {}

struct Struct;
impl Trait for Struct {}

fn main() {
    let s = Struct; // or `let mut s = Struct;`

    let _ = s as &dyn Trait; // or `let _ = s as &mut dyn Trait;`
}

Compiler output:

error[E0605]: non-primitive cast: `Struct` as `&dyn Trait`
 --> src/main.rs:9:13
  |
9 |     let _ = s as &dyn Trait; // or `let _ = s as &mut dyn Trait;`
  |             ^^^^^^^^^^^^^^^
  |
  = note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait

I would imagine something like this:

error[E0605]: non-primitive cast: `Struct` as `&dyn Trait`
 --> src/main.rs:9:13
  |
9 |     let _ = s as &dyn Trait; // or `let _ = s as &mut dyn Trait;`
  |             ^^^^^^^^^^^^^^^
  |
  = help: try casting `&Struct` instead: `&s as &dyn Trait`

See also related issue #44602.

@estebank estebank added D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jun 13, 2020
estebank added a commit to estebank/rust that referenced this issue Jun 15, 2020
- Suggest borrowing expression if it would allow cast to work.
- Suggest using `<Type>::from(<expr>)` when appropriate.
- Minor tweak to `;` typo suggestion.

Partily address rust-lang#47136.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 16, 2020
…dtwco

Tweak "non-primitive cast" error

- Suggest borrowing expression if it would allow cast to work.
- Suggest using `<Type>::from(<expr>)` when appropriate.
- Minor tweak to `;` typo suggestion.

Partily address rust-lang#47136.
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 16, 2020
…dtwco

Tweak "non-primitive cast" error

- Suggest borrowing expression if it would allow cast to work.
- Suggest using `<Type>::from(<expr>)` when appropriate.
- Minor tweak to `;` typo suggestion.

Partily address rust-lang#47136.
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 16, 2020
…dtwco

Tweak "non-primitive cast" error

- Suggest borrowing expression if it would allow cast to work.
- Suggest using `<Type>::from(<expr>)` when appropriate.
- Minor tweak to `;` typo suggestion.

Partily address rust-lang#47136.
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 16, 2020
…dtwco

Tweak "non-primitive cast" error

- Suggest borrowing expression if it would allow cast to work.
- Suggest using `<Type>::from(<expr>)` when appropriate.
- Minor tweak to `;` typo suggestion.

Partily address rust-lang#47136.
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 18, 2020
…dtwco

Tweak "non-primitive cast" error

- Suggest borrowing expression if it would allow cast to work.
- Suggest using `<Type>::from(<expr>)` when appropriate.
- Minor tweak to `;` typo suggestion.

Partily address rust-lang#47136.
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants