Skip to content

Commit 4be2d6c

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 b50b73e commit 4be2d6c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

gcc/rust/typecheck/rust-casts.cc

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

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

66-
switch (from.get_ty ()->get_kind ())
67+
switch (from_type->get_kind ())
6768
{
6869
case TyTy::TypeKind::INFER: {
6970
TyTy::InferType *from_infer
70-
= static_cast<TyTy::InferType *> (from.get_ty ());
71+
= static_cast<TyTy::InferType *> (from_type);
7172
switch (from_infer->get_infer_kind ())
7273
{
7374
case TyTy::InferType::InferTypeKind::GENERAL:
@@ -289,4 +290,4 @@ TypeCastRules::emit_cast_error () const
289290
}
290291

291292
} // namespace Resolver
292-
} // namespace Rust
293+
} // namespace Rust
Lines changed: 8 additions & 0 deletions
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)