Skip to content

Commit

Permalink
Fix: Object::from_ref no longer forgetting to increment the strong co…
Browse files Browse the repository at this point in the history
…unt.

Fixes #5851
I haven't been able to produce consistently any program that could exhibit the issue this PR is solving, the only hint I had that this happened was the report of #5851, which I reasoned manually must come from this function that is being skipped. By making it opaque to the compiler, we prevent optimizations that would skip the strong count from increasing.
Alternative to this would be to pass &Rc<Self> instead of &Self, which is what https://github.com/dafny-lang/dafny/tree/fix-object-safety-rust does, but &Rc does not make traits object-safe, which means we can't yet use them in the code.
I tried also passing a second arguments so that we directly have a `Rc<>` and don't need to recover it from the &T, but `this: Rc<Self>` again makes the trait not object-safe, and `this: Rc<dyn Any>` causes an issue because we can't create this argument when what we have is only the trait, and trait upcast to Any is currently unsound in Rust.
This PR is the best workaround I found so far.
  • Loading branch information
MikaelMayer committed Oct 24, 2024
1 parent a38e8a4 commit 4f89b4a
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Source/DafnyRuntime/DafnyRuntimeRust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3508,7 +3508,8 @@ impl <T: ?Sized> AsRef<T> for Object<T> {
fn increment_strong_count<T: ?Sized>(data: *const T) {
// SAFETY: This method is called only on values that were constructed from an Rc
unsafe {
Rc::increment_strong_count(data);
// Black box avoids the compiler wrongly inferring that increment strong count does nothing since the data it was applied to can be traced to be borrowed
::std::hint::black_box(Rc::increment_strong_count(data));
}
}

Expand Down

0 comments on commit 4f89b4a

Please sign in to comment.