]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
typecheck: Fix casting error behind generics
authorbl7awy <mahadelr19@gmail.com>
Tue, 28 Feb 2023 17:53:01 +0000 (20:53 +0300)
committerPhilip Herron <philip.herron@embecosm.com>
Wed, 1 Mar 2023 00:16:04 +0000 (00:16 +0000)
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 <mahadelr19@gmail.com>
gcc/rust/typecheck/rust-casts.cc
gcc/testsuite/rust/compile/cast_generics.rs [new file with mode: 0644]

index 0ecb50f7d1d7a607a70a14b9808dc29358680a07..f41c7134a75c57b05b9ababfd6255365eb730551 100644 (file)
@@ -60,15 +60,16 @@ TypeCastRules::cast_rules ()
   // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/cast.rs#L596
   // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/cast.rs#L654
 
-  rust_debug ("cast_rules from={%s} to={%s}",
-             from.get_ty ()->debug_str ().c_str (),
+  TyTy::BaseType *from_type = from.get_ty ()->destructure ();
+
+  rust_debug ("cast_rules from={%s} to={%s}", from_type->debug_str ().c_str (),
              to.get_ty ()->debug_str ().c_str ());
 
-  switch (from.get_ty ()->get_kind ())
+  switch (from_type->get_kind ())
     {
       case TyTy::TypeKind::INFER: {
        TyTy::InferType *from_infer
-         = static_cast<TyTy::InferType *> (from.get_ty ());
+         = static_cast<TyTy::InferType *> (from_type);
        switch (from_infer->get_infer_kind ())
          {
          case TyTy::InferType::InferTypeKind::GENERAL:
@@ -290,4 +291,4 @@ TypeCastRules::emit_cast_error () const
 }
 
 } // namespace Resolver
-} // namespace Rust
+} // namespace Rust
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/cast_generics.rs b/gcc/testsuite/rust/compile/cast_generics.rs
new file mode 100644 (file)
index 0000000..7d18596
--- /dev/null
@@ -0,0 +1,8 @@
+fn test<T>(a: T) -> T {
+  a
+}
+
+fn main() {
+  let t: i32 = test(123 as i32) as i32;
+  // { dg-warning "unused name" "" { target *-*-* } .-1 }
+}