Open
Conversation
7d6d897 to
6d3e6c1
Compare
BennoLossin
requested changes
Apr 20, 2026
Member
BennoLossin
left a comment
There was a problem hiding this comment.
Oh I like the solution of using the drop guard for the access! I have just one comment, this looks very nice :)
The scripts match "[PATCH .*]" which breaks if a single patch is involved, which is "[PATCH]". Signed-off-by: Gary Guo <gary@garyguo.net>
When a field has been initialized, `init!`/`pin_init!` create a reference
or pinned reference to the field so it can be accessed later during the
initialization of other fields. However, the reference it created is
incorrectly `&'static` rather than just the scope of the initializer.
This means that you can do
init!(Foo {
a: 1,
_: {
let b: &'static u32 = a;
}
})
which is unsound.
This is caused by `&mut (*#slot).#ident`, which actually allows arbitrary
lifetime, so this is effectively `'static`. Somewhat ironically, the safety
justification of creating the accessor is.. "SAFETY: TODO".
Fix it by adding `Deref`/`DerefMut` implementation to `DropGuard` and
derive the reference from dereferencing `DropGuard` instead. The lifetime
of `DropGuard` is exactly what we want for these accessors.
The old accessor creation code also has a purpose of preventing unaligned
fields. So a `let _ = &(*#slot).#ident` still needs to be present, but this
no longer has to care about pinning, so it is moved to just before the
guard generation.
Fixes: db96c51 ("add references to previously initialized fields")
Signed-off-by: Gary Guo <gary@garyguo.net>
6d3e6c1 to
589b3a1
Compare
BennoLossin
approved these changes
Apr 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a field has been initialized,
init!/pin_init!create a reference or pinned reference to the field so it can be accessed later during the initialization of other fields. However, the reference it created is incorrectly&'staticrather than just the scope of the initializer.This means that you can do
which is unsound.
This is caused by
&mut (*#slot).#ident, which actually allows arbitrary lifetime, so this is effectively'static. Somewhat ironically, the safety justification of creating the accessor is.. "SAFETY: TODO".Fix it by adding
Deref/DerefMutimplementation toDropGuardand derive the reference from dereferencingDropGuardinstead. The lifetime ofDropGuardis exactly what we want for these accessors.The old accessor creation code also has a purpose of preventing unaligned fields. So a
let _ = &(*#slot).#identis kept in place to avoid that unsoundness.