aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorNobel <nobel2073@gmail.com>2024-12-23 04:04:44 +0545
committerArthur Cohen <arthur.cohen@embecosm.com>2025-03-21 12:55:43 +0100
commit3396da354e52f17b8dfd2bf13b06e30b2ac2176b (patch)
tree32749417b2d12680e9bae2c842d8516d5ab58026 /gcc/rust
parent6b27b7807545d985abde8a332c9f37e0d8657fb8 (diff)
downloadgcc-3396da354e52f17b8dfd2bf13b06e30b2ac2176b.zip
gcc-3396da354e52f17b8dfd2bf13b06e30b2ac2176b.tar.gz
gcc-3396da354e52f17b8dfd2bf13b06e30b2ac2176b.tar.bz2
gccrs: add ptr to int and int to ptr type cast rules
Added rules to allow type casting pointer as integer types (u*,i*) and integer types to be casted as pointer. gcc/rust/ChangeLog: * typecheck/rust-casts.cc (TypeCastRules::cast_rules): Add rule. gcc/testsuite/ChangeLog: * rust/compile/ptr_int_cast.rs: New test. Signed-off-by: Nobel Singh <nobel2073@gmail.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/typecheck/rust-casts.cc27
1 files changed, 25 insertions, 2 deletions
diff --git a/gcc/rust/typecheck/rust-casts.cc b/gcc/rust/typecheck/rust-casts.cc
index cf4de4b..694cbaa 100644
--- a/gcc/rust/typecheck/rust-casts.cc
+++ b/gcc/rust/typecheck/rust-casts.cc
@@ -210,6 +210,16 @@ TypeCastRules::cast_rules ()
}
break;
+ case TyTy::TypeKind::POINTER: {
+ // char can't be casted as a ptr
+ 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:
@@ -254,12 +264,25 @@ TypeCastRules::cast_rules ()
case TyTy::TypeKind::POINTER:
switch (to.get_ty ()->get_kind ())
{
+ case TyTy::TypeKind::USIZE:
+ case TyTy::TypeKind::ISIZE:
+ case TyTy::TypeKind::UINT:
+ case TyTy::TypeKind::INT: {
+ // refs should not cast to numeric type
+ bool from_ptr
+ = from.get_ty ()->get_kind () == TyTy::TypeKind::POINTER;
+ if (from_ptr)
+ {
+ return TypeCoercionRules::CoercionResult{
+ {}, to.get_ty ()->clone ()};
+ }
+ }
+ break;
+
case TyTy::TypeKind::REF:
case TyTy::TypeKind::POINTER:
return check_ptr_ptr_cast ();
- // FIXME can you cast a pointer to a integral type?
-
default:
return TypeCoercionRules::CoercionResult::get_error ();
}