From a89eb96d79ec3d001579fca8e4bead46080bba7d Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 1 Jun 2022 16:50:44 +0100 Subject: 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 --- gcc/rust/typecheck/rust-tyty.cc | 6 +++++ gcc/testsuite/rust/compile/issue-1289.rs | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 gcc/testsuite/rust/compile/issue-1289.rs (limited to 'gcc') diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 84aa835..c509d9b 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 (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 (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 0000000..8634f1d --- /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(dst: *const T, offset: isize) -> *const T; + } +} + +#[lang = "mut_ptr"] +impl *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 *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 +} -- cgit v1.1