-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
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 |
This note is also unhelpful with casting to 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. |
- 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.
…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.
…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.
…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.
…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.
…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.
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 theFrom
trait", but as show by the expressionBaz as i32
, this is not true.The text was updated successfully, but these errors were encountered: