diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-06-01 16:50:44 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-06-02 11:18:01 +0100 |
commit | a89eb96d79ec3d001579fca8e4bead46080bba7d (patch) | |
tree | 8cbd019d977cbc1a0c1c58d39722f45f684a7bc2 | |
parent | fc6f6a7179d3015f4e658e4b6bda2098796cd2c7 (diff) | |
download | gcc-a89eb96d79ec3d001579fca8e4bead46080bba7d.zip gcc-a89eb96d79ec3d001579fca8e4bead46080bba7d.tar.gz gcc-a89eb96d79ec3d001579fca8e4bead46080bba7d.tar.bz2 |
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
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.cc | 6 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/issue-1289.rs | 42 |
2 files changed, 48 insertions, 0 deletions
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<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 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<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 +} |