Skip to content
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

[beta] backports #75261

Merged
merged 15 commits into from
Aug 7, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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 <david@davidtw.co>
davidtwco authored and Mark-Simulacrum committed Aug 7, 2020
commit ed591d00401f3b258f9939e8d3a668a9f11a1b24
1 change: 1 addition & 0 deletions src/librustc_typeck/check/cast.rs
Original file line number Diff line number Diff line change
@@ -387,6 +387,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
// Check for infer types because cases like `Option<{integer}>` would
// panic otherwise.
if !expr_ty.has_infer_types()
&& !ty.has_infer_types()
&& fcx.tcx.type_implements_trait((
from_trait,
ty,
6 changes: 6 additions & 0 deletions src/test/ui/issues/issue-73886.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
let _ = &&[0] as &[_];
//~^ ERROR non-primitive cast: `&&[i32; 1]` as `&[_]`
let _ = 7u32 as Option<_>;
//~^ ERROR non-primitive cast: `u32` as `std::option::Option<_>`
}
15 changes: 15 additions & 0 deletions src/test/ui/issues/issue-73886.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0605]: non-primitive cast: `&&[i32; 1]` as `&[_]`
--> $DIR/issue-73886.rs:2:13
|
LL | let _ = &&[0] as &[_];
| ^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error[E0605]: non-primitive cast: `u32` as `std::option::Option<_>`
--> $DIR/issue-73886.rs:4:13
|
LL | let _ = 7u32 as Option<_>;
| ^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error: aborting due to 2 previous errors

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