public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Update Reference and Pointer types equality interface
@ 2022-06-08 12:51 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:51 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:a89eb96d79ec3d001579fca8e4bead46080bba7d

commit a89eb96d79ec3d001579fca8e4bead46080bba7d
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Wed Jun 1 16:50:44 2022 +0100

    Update Reference and Pointer types equality interface
    
    Thse types are not equal unless their mutabilities are equal so this patch
    adds in the missing if statements to ensure that two pointers or two
    references account for their mutability.
    
    Fixes #1289

Diff:
---
 gcc/rust/typecheck/rust-tyty.cc          |  6 +++++
 gcc/testsuite/rust/compile/issue-1289.rs | 42 ++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index 84aa8354536..c509d9b845b 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -2441,6 +2441,9 @@ ReferenceType::is_equal (const BaseType &other) const
     return false;
 
   auto other2 = static_cast<const ReferenceType &> (other);
+  if (mutability () != other2.mutability ())
+    return false;
+
   return get_base ()->is_equal (*other2.get_base ());
 }
 
@@ -2535,6 +2538,9 @@ PointerType::is_equal (const BaseType &other) const
     return false;
 
   auto other2 = static_cast<const PointerType &> (other);
+  if (mutability () != other2.mutability ())
+    return false;
+
   return get_base ()->is_equal (*other2.get_base ());
 }
 
diff --git a/gcc/testsuite/rust/compile/issue-1289.rs b/gcc/testsuite/rust/compile/issue-1289.rs
new file mode 100644
index 00000000000..8634f1d7e4e
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-1289.rs
@@ -0,0 +1,42 @@
+extern "C" {
+    fn printf(s: *const i8, ...);
+}
+
+mod intrinsics {
+    extern "rust-intrinsic" {
+        pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
+    }
+}
+
+#[lang = "mut_ptr"]
+impl<T> *mut T {
+    pub const unsafe fn offset(self, count: isize) -> *mut T {
+        unsafe { intrinsics::offset(self, count) as *mut T }
+    }
+
+    pub const unsafe fn add(self, count: usize) -> Self {
+        unsafe { self.offset(count as isize) }
+    }
+}
+
+#[lang = "const_ptr"]
+impl<T> *const T {
+    pub const unsafe fn offset(self, count: isize) -> *mut T {
+        // { dg-warning "associated function is never used" "" { target *-*-* } .-1 }
+        unsafe { intrinsics::offset(self, count) as *mut T }
+    }
+
+    pub const unsafe fn add(self, count: usize) -> Self {
+        // { dg-warning "associated function is never used" "" { target *-*-* } .-1 }
+        unsafe { self.offset(count as isize) }
+    }
+}
+
+fn main() -> i32 {
+    let a: *mut _ = &mut 123;
+    unsafe {
+        let _b = a.add(123);
+    }
+
+    0
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:51 [gcc/devel/rust/master] Update Reference and Pointer types equality interface Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).