From: Philip Herron <[email protected]>

Rust semantics are that all raw pointers can alias but then we get stronger
garentees on reference types.

Fixes Rust-GCC#4536

gcc/rust/ChangeLog:

        * rust-gcc.cc (pointer_type): turn on can alias

gcc/testsuite/ChangeLog:

        * rust/execute/raw-pointer-aliasing.rs: New test.

Signed-off-by: Philip Herron <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/1ce8d91871c231c695589197141184200519852f

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4666

 gcc/rust/rust-gcc.cc                          |  2 +-
 .../rust/execute/raw-pointer-aliasing.rs      | 25 +++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/execute/raw-pointer-aliasing.rs

diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index ab02bcd55..00f356381 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -444,7 +444,7 @@ pointer_type (tree to_type)
 {
   if (error_operand_p (to_type))
     return error_mark_node;
-  tree type = build_pointer_type (to_type);
+  tree type = build_pointer_type_for_mode (to_type, VOIDmode, true);
   return type;
 }
 
diff --git a/gcc/testsuite/rust/execute/raw-pointer-aliasing.rs 
b/gcc/testsuite/rust/execute/raw-pointer-aliasing.rs
new file mode 100644
index 000000000..d19730b04
--- /dev/null
+++ b/gcc/testsuite/rust/execute/raw-pointer-aliasing.rs
@@ -0,0 +1,25 @@
+// { dg-options "-O2" }
+
+#![feature(no_core)]
+#![no_core]
+
+extern "C" {
+    fn malloc(n: u64) -> *mut u8;
+}
+
+fn f() -> i32 {
+    unsafe {
+        let p = malloc(4) as *mut i32;
+        *p = 27;
+        *(p as *mut i16) = 42;
+        *p
+    }
+}
+
+fn main() -> i32 {
+    if f() == 27 {
+        1
+    } else {
+        0
+    }
+}

base-commit: 33c94177a20f00ac0b849b531927a32127235102
-- 
2.54.0

Reply via email to