]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Allow float type to be casted as integer type
authorNobel <nobel2073@gmail.com>
Sat, 21 Dec 2024 18:11:39 +0000 (23:56 +0545)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 21 Mar 2025 11:56:56 +0000 (12:56 +0100)
gccrs now should be able to cast float types as numeric.

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::cast_rules): Add rule.

gcc/testsuite/ChangeLog:

* rust/compile/cast_float_as_integer.rs: New test.

Signed-off-by: Nobel Singh <nobel2073@gmail.com>
gcc/rust/typecheck/rust-casts.cc
gcc/testsuite/rust/compile/cast_float_as_integer.rs [new file with mode: 0644]

index 694cbaa5db6085761fc372b2e3ef11e81d928069..90bdef1fd3c14b9c606ecc7f8b4ee2c4b762858b 100644 (file)
@@ -235,6 +235,12 @@ TypeCastRules::cast_rules ()
     case TyTy::TypeKind::FLOAT:
       switch (to.get_ty ()->get_kind ())
        {
+       case TyTy::TypeKind::USIZE:
+       case TyTy::TypeKind::ISIZE:
+       case TyTy::TypeKind::UINT:
+       case TyTy::TypeKind::INT:
+         return TypeCoercionRules::CoercionResult{{}, to.get_ty ()->clone ()};
+
        case TyTy::TypeKind::FLOAT:
          return TypeCoercionRules::CoercionResult{{}, to.get_ty ()->clone ()};
 
diff --git a/gcc/testsuite/rust/compile/cast_float_as_integer.rs b/gcc/testsuite/rust/compile/cast_float_as_integer.rs
new file mode 100644 (file)
index 0000000..e6b86db
--- /dev/null
@@ -0,0 +1,10 @@
+// { dg-options "-w" }
+fn main(){
+    let foo:f64 = 13.37;
+    let _ = foo as i64;
+    let _ = foo as u64;
+    let _ = foo as isize;
+    let _ = foo as usize;
+    let _ = foo as i8;
+    let _ = foo as u8;
+}