From eec8198c4144617bbb858934a3e4bb0aef2d7d2c Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Tue, 26 Nov 2024 15:33:35 +0000 Subject: [PATCH] gccrs: allow casts from numeric types to floats 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 --- gcc/rust/typecheck/rust-casts.cc | 10 ++++++++++ gcc/testsuite/rust/compile/issue-3261.rs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 gcc/testsuite/rust/compile/issue-3261.rs diff --git a/gcc/rust/typecheck/rust-casts.cc b/gcc/rust/typecheck/rust-casts.cc index 87b31b5820f6..fb7951032491 100644 --- a/gcc/rust/typecheck/rust-casts.cc +++ b/gcc/rust/typecheck/rust-casts.cc @@ -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 index 000000000000..37e974d61696 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-3261.rs @@ -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; +}