From a7d573562c7876f9d97416d20ad3b69a2cd44acb Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 30 Dec 2020 02:13:46 -0800 Subject: [PATCH] Ignore fat pointer vtables for Gc::ptr_eq MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rust doesn’t guarantee that fat pointer comparison works as expected due to duplicated vtables: https://github.com/rust-lang/rust/issues/46139 Signed-off-by: Anders Kaseorg --- gc/src/gc.rs | 9 ++++++++- gc/src/lib.rs | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/gc/src/gc.rs b/gc/src/gc.rs index d2a728d..a841cb2 100644 --- a/gc/src/gc.rs +++ b/gc/src/gc.rs @@ -1,7 +1,7 @@ use crate::trace::{Finalize, Trace}; use std::cell::{Cell, RefCell}; use std::mem; -use std::ptr::NonNull; +use std::ptr::{self, NonNull}; const INITIAL_THRESHOLD: usize = 100; @@ -119,6 +119,13 @@ impl GcBox { } impl GcBox { + /// Returns `true` if the two references refer to the same `GcBox`. + pub(crate) fn ptr_eq(this: &GcBox, other: &GcBox) -> bool { + // Use .header to ignore fat pointer vtables, to work around + // https://github.com/rust-lang/rust/issues/46139 + ptr::eq(&this.header, &other.header) + } + /// Marks this `GcBox` and marks through its data. pub(crate) unsafe fn trace_inner(&self) { let marked = self.header.marked.get(); diff --git a/gc/src/lib.rs b/gc/src/lib.rs index bf99b72..efc6e8b 100644 --- a/gc/src/lib.rs +++ b/gc/src/lib.rs @@ -90,7 +90,7 @@ impl Gc { impl Gc { /// Returns `true` if the two `Gc`s point to the same allocation. pub fn ptr_eq(this: &Gc, other: &Gc) -> bool { - ptr::eq(this.inner(), other.inner()) + GcBox::ptr_eq(this.inner(), other.inner()) } }