Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Incorrect help suggestion when attempting "arc" as Arc<str> #135412

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
totbuae opened this issue Jan 12, 2025 · 2 comments · Fixed by #140196
Closed

Incorrect help suggestion when attempting "arc" as Arc<str> #135412

totbuae opened this issue Jan 12, 2025 · 2 comments · Fixed by #140196
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

@totbuae
Copy link

totbuae commented Jan 12, 2025

Code

fn main() {
    use std::sync::Arc;

    let _a = "arc" as Arc<str>;
}

Current output

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

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0605`.

Desired output

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

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0605`.

Rationale and extra context

The compiler suggests:
help: consider using the From trait instead: Arc<str>::from("arc")

when it should be:
help: consider using the From trait instead: Arc::<str>::from("arc")

There's a :: missing to complete the turbofish.

Other cases

Rust Version

rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.84.0
LLVM version: 19.1.5

Anything else?

No response

@totbuae totbuae 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 Jan 12, 2025
@workingjubilee workingjubilee added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Jan 12, 2025
@fmease fmease added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Jan 12, 2025
@Harshit933
Copy link
Contributor

@rustbot claim

@Kivooeo
Copy link
Contributor

Kivooeo commented Apr 23, 2025

Since it's been around 3 months since @Harshit933 took this issue and there hasn't been any progress, I hope no one minds if I pick it up again

@rustbot claim

@rustbot rustbot assigned Kivooeo and unassigned Harshit933 Apr 23, 2025
@bors bors closed this as completed in 02ebca2 Apr 25, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue 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
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

Successfully merging a pull request may close this issue.

5 participants