Skip to content

[wip] Introduce Ptr::project_map#2984

Open
jswrenn wants to merge 1 commit intoG06edd27f8b77b5ad480ca94749098bb4b0808342from
Gc3e9ee9afa6945e10c35be84d30c970894395c96
Open

[wip] Introduce Ptr::project_map#2984
jswrenn wants to merge 1 commit intoG06edd27f8b77b5ad480ca94749098bb4b0808342from
Gc3e9ee9afa6945e10c35be84d30c970894395c96

Conversation

@jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Feb 4, 2026

Enables piecewise initialization.

gherrit-pr-id: Gc3e9ee9afa6945e10c35be84d30c970894395c96
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @jswrenn, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant enhancement to pointer manipulation by adding a project_map method to the Ptr type. This new functionality is designed to enable more flexible and controlled piecewise initialization of data structures. It achieves this by allowing developers to project a field, apply a mapping function, and then safely assume the validity of the original pointer. Supporting this, new Idx and Map types have been introduced to refine how tuple fields are identified and how validity components are managed within the type system.

Highlights

  • New Ptr::project_map method: Introduced a new project_map method on the Ptr struct, which allows mapping a projected field and then assuming the validity of the original pointer. This is intended to facilitate piecewise initialization.
  • Introduction of Idx struct: A new Idx struct was added in src/lib.rs to serve as a field visibility token for public tuple fields, replacing the previous use of () in HasField and ProjectField implementations for tuples.
  • New Map trait for validity components: A Map trait was introduced in src/pointer/invariant.rs to provide a mechanism for replacing a validity component within a type's invariants, which is utilized by the new project_map method.
  • Tuple HasField and ProjectField updates: The macro for implementing HasField and ProjectField for tuples in src/impls.rs was updated to use the new crate::Idx struct for field identification and to implement the new crate::invariant::Map trait.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/impls.rs
    • Implemented crate::invariant::Map for tuples, allowing validity components to be replaced.
    • Updated crate::HasField and crate::ProjectField implementations for tuples to use crate::Idx<{ $CurrI }> instead of () for field identification.
    • Adjusted comments to reflect the change from () to crate::Idx in HasField and ProjectField implementations.
  • src/lib.rs
    • Introduced the Idx<const INDEX: u16> struct, serving as a field visibility token for public tuple fields.
  • src/pointer/invariant.rs
    • Added the Map<Replacement: Validity, Field> trait, which defines a mechanism to replace a validity component in a type's invariants.
  • src/pointer/ptr.rs
    • Added the project_map method to the Ptr struct, enabling the mapping of projected fields and subsequent assumption of the original pointer's validity.
Activity
  • The pull request is currently marked as a Work In Progress ([wip]), indicating that further changes or refinements are expected.
  • The author, jswrenn, has linked this PR to related issues or pull requests: [wip] Introduce Ptr::project_map #2984 and [WIP] Introduce compound Ptr validity #2966, suggesting it's part of a larger effort.
  • The primary goal of this PR is to enable piecewise initialization.
  • A // SAFETY: TODO comment is present in the newly added project_map function, indicating that the safety invariants for this new method still need to be fully addressed and documented.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces Ptr::project_map to enable piecewise initialization of data structures. This is a valuable addition. The supporting machinery, including the invariant::Map trait and its implementation for tuples, seems correct.

However, the implementation of project_map itself in src/pointer/ptr.rs has a critical soundness issue. It uses unsafe { self.assume_validity() } without proper justification, and the logic seems to incorrectly handle the result of the mapping closure. As indicated by the // SAFETY: TODO comment, this is likely a work in progress, but it's crucial to address this before merging.

Comment on lines +941 to +947
let _mapped = match self.reborrow().project() {
Ok(field) => map(field),
Err(err) => return Err(err),
};
// SAFETY: TODO
Ok(unsafe { self.assume_validity() })
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This implementation has a critical soundness issue.

The map closure is called on a reborrow of self, and its resulting Ptr is immediately discarded (assigned to _mapped). This means any type-level proof of a validity change is lost.

Subsequently, unsafe { self.assume_validity() } is called without justification. Since map operated on a temporary borrow, it cannot logically change the validity of the original self. This unsafe block is unsound as written and, as the // SAFETY: TODO comment indicates, lacks the required safety proof.

This could lead to Ptrs with incorrect validity invariants, causing undefined behavior. The map closure's returned Ptr must be used to prove that the operation is sound before assume_validity can be justified.

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 0% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.55%. Comparing base (5902593) to head (7a3d360).

Files with missing lines Patch % Lines
src/pointer/ptr.rs 0.00% 27 Missing ⚠️
Additional details and impacted files
@@                              Coverage Diff                              @@
##           G06edd27f8b77b5ad480ca94749098bb4b0808342    #2984      +/-   ##
=============================================================================
- Coverage                                      91.96%   91.55%   -0.41%     
=============================================================================
  Files                                             19       19              
  Lines                                           6033     6060      +27     
=============================================================================
  Hits                                            5548     5548              
- Misses                                           485      512      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants