Skip to content

Commit

Permalink
gccrs: allow casts from numeric types to floats
Browse files Browse the repository at this point in the history
Fixes #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 <[email protected]>
  • Loading branch information
philberty committed Nov 26, 2024
1 parent 898f2e8 commit fedaa3c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gcc/rust/typecheck/rust-casts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions gcc/testsuite/rust/compile/issue-3261.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit fedaa3c

Please sign in to comment.