Skip to content

Commit 00e6c2c

Browse files
committed
typecheck: Fix casting error behind generics
gcc/rust/ChangeLog: * typecheck/rust-casts.cc (TypeCastRules::cast_rules): Perform destructure on `from` type. gcc/testsuite/ChangeLog: * rust/compile/cast_generics.rs: New test. Signed-off-by: Mahmoud Mohamed <[email protected]>
1 parent 41890d2 commit 00e6c2c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

gcc/rust/typecheck/rust-casts.cc

+6-5
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,16 @@ TypeCastRules::cast_rules ()
6060
// https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/cast.rs#L596
6161
// https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/cast.rs#L654
6262

63-
rust_debug ("cast_rules from={%s} to={%s}",
64-
from.get_ty ()->debug_str ().c_str (),
63+
TyTy::BaseType *from_type = from.get_ty ()->destructure ();
64+
65+
rust_debug ("cast_rules from={%s} to={%s}", from_type->debug_str ().c_str (),
6566
to.get_ty ()->debug_str ().c_str ());
6667

67-
switch (from.get_ty ()->get_kind ())
68+
switch (from_type->get_kind ())
6869
{
6970
case TyTy::TypeKind::INFER: {
7071
TyTy::InferType *from_infer
71-
= static_cast<TyTy::InferType *> (from.get_ty ());
72+
= static_cast<TyTy::InferType *> (from_type);
7273
switch (from_infer->get_infer_kind ())
7374
{
7475
case TyTy::InferType::InferTypeKind::GENERAL:
@@ -290,4 +291,4 @@ TypeCastRules::emit_cast_error () const
290291
}
291292

292293
} // namespace Resolver
293-
} // namespace Rust
294+
} // namespace Rust
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn test<T>(a: T) -> T {
2+
a
3+
}
4+
5+
fn main() {
6+
let t: i32 = test(123 as i32) as i32;
7+
// { dg-warning "unused name" "" { target *-*-* } .-1 }
8+
}

0 commit comments

Comments
 (0)