Skip to content

Commit ed591d0

Browse files
davidtwcoMark-Simulacrum
authored andcommitted
typeck: check for infer before type impls trait
This commit 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. Signed-off-by: David Wood <[email protected]>
1 parent 887c4c9 commit ed591d0

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)