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

generator: Work around invariance for assigning mutable pointer of lifetimed slice #841

Merged
merged 1 commit into from
Dec 2, 2023

Conversation

MarijnS95
Copy link
Collaborator

Fixes #837

@Ralith I'm in quite a hurry but managed to piece together a rather minimal generator change to solve the issue, but probably did a horrible job of understanding, summarizing and re-explaining the issues with variance. Feel free to suggest something better, otherwise I'll revisit it when I return.


In essence this builder function needs to adhere to two rules:

  1. No ref-after-free: the slice must outlive (uses of) the builder object;
  2. No aliasing: the slice cannot be (im)mutably used while it is mutably borrowed within a live builder object.

These two rules have been tested and are satisfied by the given builder implementation. Without this change timings seems to be borrowing itself, hence is not allowed to be used after it has been temporarily mutably borrowed inside the builder, even after that builder was dropped. Thus defeating the purpose of this "getter" API via a struct.

Without the .cast(), because mutable raw pointers are invariant (i.e. there is no subtyping relationship) the compiler complains about requiring self to outlive timings instead, which does not satisfy the two rules above.

…fetimed slice

In essence this builder function needs to adhere to two rules:
1. No ref-after-free: the slice must outlive (uses of) the builder
   object;
2. No aliasing: the slice cannot be (im)mutably used while it is mutably
   borrowed within a live builder object.

These two rules have been tested and are satisfied by the given builder
implementation.  Without this change `timings` seems to be borrowing
itself, hence is not allowed to be used after it has been temporarily
mutably borrowed inside the builder, even after that builder was
dropped.  Thus defeating the purpose of this "getter" API via a struct.

Without the `.cast()`, because mutable raw pointers are invariant
(i.e. there is no subtyping relationship) the compiler complains about
requiring `self` to outlive `timings` instead, which does not satisfy
the two rules above.
@MarijnS95 MarijnS95 requested a review from Ralith December 2, 2023 10:14
Copy link
Collaborator

@Ralith Ralith left a comment

Choose a reason for hiding this comment

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

This looks great!

@MarijnS95 MarijnS95 merged commit e5b0873 into master Dec 2, 2023
22 checks passed
@MarijnS95 MarijnS95 deleted the nv-low-latency2-timings-builder-mutable-variance branch December 2, 2023 18:52
@MarijnS95 MarijnS95 added this to the Ash 0.38 milestone Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NV_low_latency2 Impossible to use timings array after mutably borrowing in GetLatencyMarkerInfoNV
2 participants