Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop creating references unnecessarily to compare pointers by-address #31

Merged
merged 1 commit into from
Aug 28, 2024

Conversation

JoJoDeveloping
Copy link
Contributor

@JoJoDeveloping JoJoDeveloping commented Aug 23, 2024

The test common::deque::basics contains several lines that look like this:

assert!(std::ptr::eq(head_d, unsafe { node1_ptr.as_ref() }));

What is happening is that we assert that head_c, a reference, is equal by-address to node1_ptr, a NonNull<_>. However, the way we do this is by creating a reference, instead of using as_ptr.

The current way has several downsides:

  • It requires an unsafe block. Comparing pointer addresses is safe so this should be unnecessary.
  • It is unsound, at least under Tree Borrows rules. The references created there violate the uniqueness of references/Boxes created internally in the Deque, which trips the Deque later when it tries to use these internally-held references.
  • It is unnecessary -- comparing the result of as_ptr works just the same, maybe even faster due to less intermediary methods.

As such, the test should be updated to only use as_ptr. When done, it actually makes the tests pass under Tree Borrows.

See also moka-rs/moka#452

The test `common::deque::basics` contains several lines that look like this:
```
assert!(std::ptr::eq(head_d, unsafe { node1_ptr.as_ref() }));
```

What is happening is that we assert that `head_c`, a reference, is equal
by-address to `node1_ptr`, a `NonNull<_>`. However, the way we do this
is by creating a reference, instead of using `as_ptr`.

The current way has several downsides:
* It requires an `unsafe` block. Comparing pointer addresses is safe so
  this should be unnecessary.
* **It is unsound**, at least under Tree Borrows rules.
  The references created there violate the uniqueness of references/Boxes
  created internally in the `Deque`, which trips the `Deque` later when
  it tries to use these internally-held references.
* It is unnecessary -- comparing the result of `as_ptr` works just the same,
  maybe even faster due to less intermediary methods.

As such, the test should be updated to only use `as_ptr`. When done, it
actually makes the tests pass under Tree Borrows.
Copy link

codecov bot commented Aug 24, 2024

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@tatsuya6502 tatsuya6502 changed the base branch from main to v0.10.x August 28, 2024 22:58
@tatsuya6502 tatsuya6502 changed the base branch from v0.10.x to main August 28, 2024 22:59
@tatsuya6502 tatsuya6502 self-assigned this Aug 28, 2024
@tatsuya6502 tatsuya6502 added the bug Something isn't working label Aug 28, 2024
@tatsuya6502 tatsuya6502 added this to the v0.10.4 milestone Aug 28, 2024
Copy link
Member

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again!

@tatsuya6502 tatsuya6502 merged commit 9a99b47 into moka-rs:main Aug 28, 2024
14 checks passed
tatsuya6502 added a commit that referenced this pull request Sep 8, 2024
Apply the fix in #31 to the `v0.10.x` branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants