Skip to content

Commit 0d2732a

Browse files
authored
Rollup merge of rust-lang#73965 - davidtwco:issue-73886-non-primitive-slice-cast, r=estebank
typeck: check for infer before type impls trait Fixes rust-lang#73886. This PR checks that the target type of the cast (an error related to which is being reported) does not have types to be inferred before checking if it implements the `From` trait. r? @estebank
2 parents 80a162e + bddb266 commit 0d2732a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/librustc_typeck/check/cast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
387387
// Check for infer types because cases like `Option<{integer}>` would
388388
// panic otherwise.
389389
if !expr_ty.has_infer_types()
390+
&& !ty.has_infer_types()
390391
&& fcx.tcx.type_implements_trait((
391392
from_trait,
392393
ty,

src/test/ui/issues/issue-73886.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let _ = &&[0] as &[_];
3+
//~^ ERROR non-primitive cast: `&&[i32; 1]` as `&[_]`
4+
let _ = 7u32 as Option<_>;
5+
//~^ ERROR non-primitive cast: `u32` as `std::option::Option<_>`
6+
}

src/test/ui/issues/issue-73886.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0605]: non-primitive cast: `&&[i32; 1]` as `&[_]`
2+
--> $DIR/issue-73886.rs:2:13
3+
|
4+
LL | let _ = &&[0] as &[_];
5+
| ^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
7+
error[E0605]: non-primitive cast: `u32` as `std::option::Option<_>`
8+
--> $DIR/issue-73886.rs:4:13
9+
|
10+
LL | let _ = 7u32 as Option<_>;
11+
| ^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0605`.

0 commit comments

Comments
 (0)