Skip to content

Commit

Permalink
Ignore fat pointer vtables for Gc::ptr_eq
Browse files Browse the repository at this point in the history
Rust doesn’t guarantee that fat pointer comparison works as expected
due to duplicated vtables:

rust-lang/rust#46139

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk committed Dec 30, 2020
1 parent 752398b commit a7d5735
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion gc/src/gc.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -119,6 +119,13 @@ impl<T: Trace> GcBox<T> {
}

impl<T: Trace + ?Sized> GcBox<T> {
/// Returns `true` if the two references refer to the same `GcBox`.
pub(crate) fn ptr_eq(this: &GcBox<T>, other: &GcBox<T>) -> 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();
Expand Down
2 changes: 1 addition & 1 deletion gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<T: Trace> Gc<T> {
impl<T: Trace + ?Sized> Gc<T> {
/// Returns `true` if the two `Gc`s point to the same allocation.
pub fn ptr_eq(this: &Gc<T>, other: &Gc<T>) -> bool {
ptr::eq(this.inner(), other.inner())
GcBox::ptr_eq(this.inner(), other.inner())
}
}

Expand Down

0 comments on commit a7d5735

Please sign in to comment.