From 277e8971aa0a2fa42e1cdee9f2b4ef1e4609e706 Mon Sep 17 00:00:00 2001 From: Nobel Date: Sat, 21 Dec 2024 23:29:05 +0545 Subject: [PATCH] Allow float types to be casted as numeric type gccrs 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_numeric.rs: New test. Signed-off-by: Nobel Singh --- gcc/rust/typecheck/rust-casts.cc | 6 ++++++ gcc/testsuite/rust/compile/cast_float_as_numeric.rs | 10 ++++++++++ 2 files changed, 16 insertions(+) create mode 100644 gcc/testsuite/rust/compile/cast_float_as_numeric.rs diff --git a/gcc/rust/typecheck/rust-casts.cc b/gcc/rust/typecheck/rust-casts.cc index fb7951032491..c6c1a890e74d 100644 --- a/gcc/rust/typecheck/rust-casts.cc +++ b/gcc/rust/typecheck/rust-casts.cc @@ -225,6 +225,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_numeric.rs b/gcc/testsuite/rust/compile/cast_float_as_numeric.rs new file mode 100644 index 000000000000..45f4730a146f --- /dev/null +++ b/gcc/testsuite/rust/compile/cast_float_as_numeric.rs @@ -0,0 +1,10 @@ +// { dg-options "-w" } +fn main(){ + let foo = 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; +}