]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix bad cast error to bool
authoremanuele-em <micheletti.emanuele@hotmail.com>
Wed, 29 Mar 2023 01:45:01 +0000 (03:45 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:38 +0000 (18:28 +0100)
In rust is not allowed to cast from int to bool. This patch handles the case when we cast a integer to bool with 'as bool'

Fixes #2026

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::cast_rules): BOOL removed from switch cases

gcc/testsuite/ChangeLog:

* rust/compile/cast4.rs: New test.

Signed-off-by: Emanuele Micheletti <micheletti.emanuele@hotmail.com>
gcc/rust/typecheck/rust-casts.cc
gcc/testsuite/rust/compile/cast4.rs [new file with mode: 0644]

index bbcd8460a943c99ec70ef91a06d79d15b0b44613..08bdcbb4a5aa8a339e79c02afc0e49011b1b04e7 100644 (file)
@@ -80,7 +80,6 @@ TypeCastRules::cast_rules ()
            switch (to.get_ty ()->get_kind ())
              {
              case TyTy::TypeKind::CHAR:
-             case TyTy::TypeKind::BOOL:
              case TyTy::TypeKind::USIZE:
              case TyTy::TypeKind::ISIZE:
              case TyTy::TypeKind::UINT:
diff --git a/gcc/testsuite/rust/compile/cast4.rs b/gcc/testsuite/rust/compile/cast4.rs
new file mode 100644 (file)
index 0000000..ab00010
--- /dev/null
@@ -0,0 +1,5 @@
+fn main() {
+    let a: i32 = 123;
+    let u = a as bool;
+    // { dg-error "invalid cast .i32. to .bool." "" { target *-*-* } .-1 }
+}