[COMMITTED 080/146] gccrs: add ptr to int and int to ptr type cast rules

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Fri Mar 21 12:06:21 GMT 2025


From: Nobel <nobel2073@gmail.com>

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>
---
 gcc/rust/typecheck/rust-casts.cc           | 27 ++++++++++++++++++++--
 gcc/testsuite/rust/compile/ptr_int_cast.rs | 18 +++++++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/ptr_int_cast.rs

diff --git a/gcc/rust/typecheck/rust-casts.cc b/gcc/rust/typecheck/rust-casts.cc
index cf4de4b3320..694cbaa5db6 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 ();
 	}
diff --git a/gcc/testsuite/rust/compile/ptr_int_cast.rs b/gcc/testsuite/rust/compile/ptr_int_cast.rs
new file mode 100644
index 00000000000..3a2a5d563d6
--- /dev/null
+++ b/gcc/testsuite/rust/compile/ptr_int_cast.rs
@@ -0,0 +1,18 @@
+fn main(){
+    let foo = 1337;
+    let bar_ptr = &foo as *const i32;
+
+    let bar_ptr_usize = bar_ptr as usize;
+    let bar_ptr_isize = bar_ptr as isize;
+    let bar_ptr_u64 = bar_ptr as u64;
+    let bar_ptr_i64 = bar_ptr as i64;
+    let bar_ptr_i8 = bar_ptr as i8;
+    let bar_ptr_u8 = bar_ptr as u8;
+
+    let _ = bar_ptr_usize as *const i32;
+    let _ = bar_ptr_isize as *const i32;
+    let _ = bar_ptr_u64 as *const i32;
+    let _ = bar_ptr_i64 as *const i32;
+    let _ = bar_ptr_i8 as *const i32;
+    let _ = bar_ptr_u8 as *const i32;
+}
-- 
2.45.2



More information about the Gcc-rust mailing list