Skip to content

Commit 8722d82

Browse files
committed
fix for issue 135412
1 parent b8005bf commit 8722d82

File tree

6 files changed

+77
-8
lines changed

6 files changed

+77
-8
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
501501
.must_apply_modulo_regions()
502502
{
503503
label = false;
504-
err.span_suggestion(
505-
self.span,
506-
"consider using the `From` trait instead",
507-
format!("{}::from({})", self.cast_ty, snippet),
508-
Applicability::MaybeIncorrect,
509-
);
504+
if !self.cast_ty.is_primitive() {
505+
let type_to_cast = self.cast_ty.to_string().replacen('<', "::<", 1);
506+
err.span_suggestion_verbose(
507+
self.span,
508+
"consider using the `From` trait instead",
509+
format!("{}::from({})", type_to_cast, snippet),
510+
Applicability::MaybeIncorrect,
511+
);
512+
} else {
513+
err.span_suggestion(
514+
self.span,
515+
"consider using the `From` trait instead",
516+
format!("{}::from({})", self.cast_ty, snippet),
517+
Applicability::MaybeIncorrect,
518+
);
519+
}
510520
}
511521
}
512522

tests/ui/coercion/issue-73886.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ error[E0605]: non-primitive cast: `u32` as `Option<_>`
88
--> $DIR/issue-73886.rs:4:13
99
|
1010
LL | let _ = 7u32 as Option<_>;
11-
| ^^^^^^^^^^^^^^^^^ help: consider using the `From` trait instead: `Option<_>::from(7u32)`
11+
| ^^^^^^^^^^^^^^^^^
1212
|
1313
= note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
14+
help: consider using the `From` trait instead
15+
|
16+
LL - let _ = 7u32 as Option<_>;
17+
LL + let _ = Option::<_>::from(7u32);
18+
|
1419

1520
error: aborting due to 2 previous errors
1621

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ run-rustfix
2+
3+
use std::sync::Arc;
4+
5+
fn main() {
6+
let _ = Option::<_>::from(7u32);
7+
//~^ ERROR non-primitive cast: `u32` as `Option<_>`
8+
let _ = Arc::<str>::from("String");
9+
//~^ ERROR non-primitive cast: `&'static str` as `Arc<str>`
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ run-rustfix
2+
3+
use std::sync::Arc;
4+
5+
fn main() {
6+
let _ = 7u32 as Option<_>;
7+
//~^ ERROR non-primitive cast: `u32` as `Option<_>`
8+
let _ = "String" as Arc<str>;
9+
//~^ ERROR non-primitive cast: `&'static str` as `Arc<str>`
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0605]: non-primitive cast: `u32` as `Option<_>`
2+
--> $DIR/non-primitive-cast-135412.rs:6:13
3+
|
4+
LL | let _ = 7u32 as Option<_>;
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
8+
help: consider using the `From` trait instead
9+
|
10+
LL - let _ = 7u32 as Option<_>;
11+
LL + let _ = Option::<_>::from(7u32);
12+
|
13+
14+
error[E0605]: non-primitive cast: `&'static str` as `Arc<str>`
15+
--> $DIR/non-primitive-cast-135412.rs:8:13
16+
|
17+
LL | let _ = "String" as Arc<str>;
18+
| ^^^^^^^^^^^^^^^^^^^^
19+
|
20+
= note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
21+
help: consider using the `From` trait instead
22+
|
23+
LL - let _ = "String" as Arc<str>;
24+
LL + let _ = Arc::<str>::from("String");
25+
|
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0605`.

tests/ui/issues/issue-16048.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ error[E0605]: non-primitive cast: `Foo<'a>` as `T`
1111
--> $DIR/issue-16048.rs:24:16
1212
|
1313
LL | return *self as T;
14-
| ^^^^^^^^^^ help: consider using the `From` trait instead: `T::from(*self)`
14+
| ^^^^^^^^^^
1515
|
1616
= note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
17+
help: consider using the `From` trait instead
18+
|
19+
LL - return *self as T;
20+
LL + return T::from(*self);
21+
|
1722

1823
error: aborting due to 2 previous errors
1924

0 commit comments

Comments
 (0)