aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorEmanuele Micheletti <micheletti.emanuele@hotmail.com>2023-03-31 12:50:36 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2024-01-16 18:28:43 +0100
commitbdb4e2930ecbb96289819833c495ac59214b38b5 (patch)
tree6640ae414e63722e8e5bf2f0000b8696dd5e78a4 /gcc
parentcb424c7229b688efd11465a8565177810c3fc255 (diff)
downloadgcc-bdb4e2930ecbb96289819833c495ac59214b38b5.zip
gcc-bdb4e2930ecbb96289819833c495ac59214b38b5.tar.gz
gcc-bdb4e2930ecbb96289819833c495ac59214b38b5.tar.bz2
gccrs: Fix bad cast as a char
In rust cast to char is allowed only from u8 type. This patch handles fix the case when the type is infered from an integer value, allowing only the u8 case' Fixes #2027 gcc/rust/ChangeLog: * typecheck/rust-casts.cc (TypeCastRules::cast_rules): case INTEGRAL handles TypeKind::CHAR gcc/testsuite/ChangeLog: * rust/compile/cast5.rs: New test. Signed-off-by: Emanuele Micheletti <micheletti.emanuele@hotmail.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-casts.cc17
-rw-r--r--gcc/testsuite/rust/compile/cast5.rs12
2 files changed, 27 insertions, 2 deletions
diff --git a/gcc/rust/typecheck/rust-casts.cc b/gcc/rust/typecheck/rust-casts.cc
index 08bdcbb..8e5e65e 100644
--- a/gcc/rust/typecheck/rust-casts.cc
+++ b/gcc/rust/typecheck/rust-casts.cc
@@ -64,7 +64,6 @@ TypeCastRules::cast_rules ()
rust_debug ("cast_rules from={%s} to={%s}", from_type->debug_str ().c_str (),
to.get_ty ()->debug_str ().c_str ());
-
switch (from_type->get_kind ())
{
case TyTy::TypeKind::INFER: {
@@ -79,7 +78,21 @@ TypeCastRules::cast_rules ()
case TyTy::InferType::InferTypeKind::INTEGRAL:
switch (to.get_ty ()->get_kind ())
{
- case TyTy::TypeKind::CHAR:
+ case TyTy::TypeKind::CHAR: {
+ // only u8 and char
+ bool was_uint
+ = from.get_ty ()->get_kind () == TyTy::TypeKind::UINT;
+ bool was_u8
+ = was_uint
+ && (static_cast<TyTy::UintType *> (from.get_ty ())
+ ->get_uint_kind ()
+ == TyTy::UintType::UintKind::U8);
+ if (was_u8)
+ return TypeCoercionRules::CoercionResult{
+ {}, to.get_ty ()->clone ()};
+ }
+ break;
+
case TyTy::TypeKind::USIZE:
case TyTy::TypeKind::ISIZE:
case TyTy::TypeKind::UINT:
diff --git a/gcc/testsuite/rust/compile/cast5.rs b/gcc/testsuite/rust/compile/cast5.rs
new file mode 100644
index 0000000..ecc10c1
--- /dev/null
+++ b/gcc/testsuite/rust/compile/cast5.rs
@@ -0,0 +1,12 @@
+fn main() {
+ const A: char = 0x1F888 as char;
+ // { dg-error "invalid cast .<integer>. to .char." "" { target *-*-* } .-1 }
+ const B: char = 129160 as char;
+ // { dg-error "invalid cast .<integer>. to .char." "" { target *-*-* } .-1 }
+ const C: i32 = 42;
+ const D: char = C as char;
+ // { dg-error "invalid cast .i32. to .char." "" { target *-*-* } .-1 }
+ const E: char = '\u{01F888}';
+ const F: u8 = 42;
+ const G: char= F as char;
+}