about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-04-25 00:53:59 +0200
committerGitHub <noreply@github.com>2025-04-25 00:53:59 +0200
commit02ebca2784329d8712dfa7164545042c73e073e9 (patch)
tree4a1c64724a4f9d5eb21db17c3fef0b797543759b /tests
parent4c0d38b93ed22e17b18948ed64008afd495ac5b0 (diff)
parentc9deaf6aaa8e389e11e39abb9aca77e6340d176e (diff)
downloadrust-02ebca2784329d8712dfa7164545042c73e073e9.tar.gz
rust-02ebca2784329d8712dfa7164545042c73e073e9.zip
Rollup merge of #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 #135412
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/coercion/issue-73886.stderr7
-rw-r--r--tests/ui/coercion/non-primitive-cast-135412.fixed10
-rw-r--r--tests/ui/coercion/non-primitive-cast-135412.rs10
-rw-r--r--tests/ui/coercion/non-primitive-cast-135412.stderr29
4 files changed, 55 insertions, 1 deletions
diff --git a/tests/ui/coercion/issue-73886.stderr b/tests/ui/coercion/issue-73886.stderr
index a6f8ba65ab5..0d4c90017cf 100644
--- a/tests/ui/coercion/issue-73886.stderr
+++ b/tests/ui/coercion/issue-73886.stderr
@@ -8,9 +8,14 @@ error[E0605]: non-primitive cast: `u32` as `Option<_>`
   --> $DIR/issue-73886.rs:4:13
    |
 LL |     let _ = 7u32 as Option<_>;
-   |             ^^^^^^^^^^^^^^^^^ help: consider using the `From` trait instead: `Option<_>::from(7u32)`
+   |             ^^^^^^^^^^^^^^^^^
    |
    = 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
+   |
+LL -     let _ = 7u32 as Option<_>;
+LL +     let _ = Option::<_>::from(7u32);
+   |
 
 error: aborting due to 2 previous errors
 
diff --git a/tests/ui/coercion/non-primitive-cast-135412.fixed b/tests/ui/coercion/non-primitive-cast-135412.fixed
new file mode 100644
index 00000000000..5cadc9368d5
--- /dev/null
+++ b/tests/ui/coercion/non-primitive-cast-135412.fixed
@@ -0,0 +1,10 @@
+//@ run-rustfix
+
+use std::sync::Arc;
+
+fn main() {
+    let _ = Option::<_>::from(7u32);
+    //~^ ERROR non-primitive cast: `u32` as `Option<_>`
+    let _ = Arc::<str>::from("String");
+    //~^ ERROR non-primitive cast: `&'static str` as `Arc<str>`
+}
diff --git a/tests/ui/coercion/non-primitive-cast-135412.rs b/tests/ui/coercion/non-primitive-cast-135412.rs
new file mode 100644
index 00000000000..67a3ef340d2
--- /dev/null
+++ b/tests/ui/coercion/non-primitive-cast-135412.rs
@@ -0,0 +1,10 @@
+//@ run-rustfix
+
+use std::sync::Arc;
+
+fn main() {
+    let _ = 7u32 as Option<_>;
+    //~^ ERROR non-primitive cast: `u32` as `Option<_>`
+    let _ = "String" as Arc<str>;
+    //~^ ERROR non-primitive cast: `&'static str` as `Arc<str>`
+}
diff --git a/tests/ui/coercion/non-primitive-cast-135412.stderr b/tests/ui/coercion/non-primitive-cast-135412.stderr
new file mode 100644
index 00000000000..7e5861f83e9
--- /dev/null
+++ b/tests/ui/coercion/non-primitive-cast-135412.stderr
@@ -0,0 +1,29 @@
+error[E0605]: non-primitive cast: `u32` as `Option<_>`
+  --> $DIR/non-primitive-cast-135412.rs:6:13
+   |
+LL |     let _ = 7u32 as Option<_>;
+   |             ^^^^^^^^^^^^^^^^^
+   |
+   = 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
+   |
+LL -     let _ = 7u32 as Option<_>;
+LL +     let _ = Option::<_>::from(7u32);
+   |
+
+error[E0605]: non-primitive cast: `&'static str` as `Arc<str>`
+  --> $DIR/non-primitive-cast-135412.rs:8:13
+   |
+LL |     let _ = "String" 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
+   |
+LL -     let _ = "String" as Arc<str>;
+LL +     let _ = Arc::<str>::from("String");
+   |
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0605`.