Skip to content

Commit

Permalink
Allow float type to be casted as integer type
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nobel-sh committed Dec 21, 2024
1 parent b5c354d commit f5d683a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions gcc/rust/typecheck/rust-casts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()};

Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/cast_float_as_integer.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f5d683a

Please sign in to comment.