Skip to content

Improved diagnostics for non-primitive cast on non-primitive types (Arc, Option) #140196

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

Merged
merged 1 commit into from
Apr 25, 2025

Conversation

Kivooeo
Copy link
Contributor

@Kivooeo Kivooeo commented Apr 23, 2025

here is a small fix that improving error messaging when user is trying to do something like this

let _ = "x" as Arc<str>;
let _ = 2 as Option<i32>;

before it looks like this

error[E0605]: non-primitive cast: `&'static str` as `Arc<str>`
 --> src\main.rs:3:13
  |
3 |     let _ = "x" as Arc<str>;
  |             ^^^^^^^^^^^^^^^ help: consider using the `From` trait instead: `Arc<str>::from("x")`

error[E0605]: non-primitive cast: `i32` as `Option<i32>`
 --> src\main.rs:4:13
  |
4 |     let _ = 2 as Option<i32>;

which looks horrible to be honest
so i made a small fix that make errors looks like this

error[E0605]: non-primitive cast: `&'static str` as `Arc<str>`
  |
3 |     let _ = "x" as Arc<str>;
  |             ^^^^^^^^^^^^^^^
  |
  = note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
help: consider using the `From` trait instead
  |
3 -     let _ = "x" as Arc<str>;
3 +     let _ = Arc::<str>::from("x");
  |

error[E0605]: non-primitive cast: `i32` as `Option<i32>`
  |
4 |     let _ = 2 as Option<i32>;
  |             ^^^^^^^^^^^^^^^^
  |
  = note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
help: consider using the `From` trait instead
  |
4 -     let _ = 2 as Option<i32>;
4 +     let _ = Option::<i32>::from(2);

What improves?

  1. Arc<str>::from("x") which makes no sense because of missing ::
  2. readability

Related Issue
fixes #135412

@rustbot
Copy link
Collaborator

rustbot commented Apr 23, 2025

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 23, 2025
@wesleywiser
Copy link
Member

Thanks @Kivooeo!

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Apr 24, 2025

📌 Commit c9deaf6 has been approved by wesleywiser

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 24, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 25, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#137096 (Stabilize flags for doctest cross compilation)
 - rust-lang#140148 (CI: use aws codebuild for job dist-arm-linux)
 - rust-lang#140187 ([AIX] Handle AIX dynamic library extensions within c-link-to-rust-dylib run-make test)
 - rust-lang#140196 (Improved diagnostics for non-primitive cast on non-primitive types (`Arc`, `Option`))
 - rust-lang#140210 (Work around cygwin issue on condvar timeout)
 - rust-lang#140213 (mention about `x.py setup` in `INSTALL.md`)
 - rust-lang#140229 (`DelimArgs` tweaks)
 - rust-lang#140248 (Fix impl block items indent)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 25, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#137096 (Stabilize flags for doctest cross compilation)
 - rust-lang#140148 (CI: use aws codebuild for job dist-arm-linux)
 - rust-lang#140187 ([AIX] Handle AIX dynamic library extensions within c-link-to-rust-dylib run-make test)
 - rust-lang#140196 (Improved diagnostics for non-primitive cast on non-primitive types (`Arc`, `Option`))
 - rust-lang#140210 (Work around cygwin issue on condvar timeout)
 - rust-lang#140213 (mention about `x.py setup` in `INSTALL.md`)
 - rust-lang#140229 (`DelimArgs` tweaks)
 - rust-lang#140248 (Fix impl block items indent)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 02ebca2 into rust-lang:master Apr 25, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 25, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 25, 2025
Rollup merge of rust-lang#140196 - Kivooeo:new-fix-two, r=wesleywiser

Improved diagnostics for non-primitive cast on non-primitive types (`Arc`, `Option`)

here is a small fix that improving error messaging when user is trying to do something like this
```rust
let _ = "x" as Arc<str>;
let _ = 2 as Option<i32>;
```

before it looks like this
```rust
error[E0605]: non-primitive cast: `&'static str` as `Arc<str>`
 --> src\main.rs:3:13
  |
3 |     let _ = "x" as Arc<str>;
  |             ^^^^^^^^^^^^^^^ help: consider using the `From` trait instead: `Arc<str>::from("x")`

error[E0605]: non-primitive cast: `i32` as `Option<i32>`
 --> src\main.rs:4:13
  |
4 |     let _ = 2 as Option<i32>;
```
which looks horrible to be honest
so i made a small fix that make errors looks like this
```rust
error[E0605]: non-primitive cast: `&'static str` as `Arc<str>`
  |
3 |     let _ = "x" as Arc<str>;
  |             ^^^^^^^^^^^^^^^
  |
  = note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
help: consider using the `From` trait instead
  |
3 -     let _ = "x" as Arc<str>;
3 +     let _ = Arc::<str>::from("x");
  |

error[E0605]: non-primitive cast: `i32` as `Option<i32>`
  |
4 |     let _ = 2 as Option<i32>;
  |             ^^^^^^^^^^^^^^^^
  |
  = note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
help: consider using the `From` trait instead
  |
4 -     let _ = 2 as Option<i32>;
4 +     let _ = Option::<i32>::from(2);
```

**What improves?**
1) `Arc<str>::from("x")` which makes no sense because of missing `::`
2) readability

**Related Issue**
fixes rust-lang#135412
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect help suggestion when attempting "arc" as Arc<str>
5 participants