Skip to content

Commit

Permalink
Allow float types to be casted as numeric type
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nobel-sh committed Dec 21, 2024
1 parent b5c354d commit 277e897
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_numeric.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 277e897

Please sign in to comment.