]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: allow casts from numeric types to floats
authorPhilip Herron <herron.philip@googlemail.com>
Tue, 26 Nov 2024 15:33:35 +0000 (15:33 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Mar 2025 11:33:04 +0000 (12:33 +0100)
Fixes Rust-GCC#3261

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::cast_rules): allow casts to float

gcc/testsuite/ChangeLog:

* rust/compile/issue-3261.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-casts.cc
gcc/testsuite/rust/compile/issue-3261.rs [new file with mode: 0644]

index 5235069fa231115bb551cb5a4467c44d25df4835..cf4de4b33205bcc666283aaf3a5f0a704addbbe0 100644 (file)
@@ -200,6 +200,16 @@ TypeCastRules::cast_rules ()
          }
          break;
 
+         case TyTy::TypeKind::FLOAT: {
+           // can only do this for number types not char
+           bool from_char
+             = from.get_ty ()->get_kind () == TyTy::TypeKind::CHAR;
+           if (!from_char)
+             return TypeCoercionRules::CoercionResult{{},
+                                                      to.get_ty ()->clone ()};
+         }
+         break;
+
        case TyTy::TypeKind::INFER:
        case TyTy::TypeKind::USIZE:
        case TyTy::TypeKind::ISIZE:
diff --git a/gcc/testsuite/rust/compile/issue-3261.rs b/gcc/testsuite/rust/compile/issue-3261.rs
new file mode 100644 (file)
index 0000000..37e974d
--- /dev/null
@@ -0,0 +1,18 @@
+// { dg-options "-w" }
+fn main() {
+    let a: i8 = 50;
+    let b = a as f32;
+    let c = a as f64;
+
+    let a: i16 = 1337;
+    let b = a as f32;
+    let c = a as f64;
+
+    let a: i32 = 1337;
+    let b = a as f32;
+    let c = a as f64;
+
+    let a: i64 = 1337;
+    let b = a as f32;
+    let c = a as f64;
+}