-
Notifications
You must be signed in to change notification settings - Fork 126
Practice good Self
hygiene
#2127
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
Conversation
d555cae
to
e3c5276
Compare
Unfortunately, with the added zerocopy/zerocopy-derive/src/lib.rs Lines 320 to 327 in e3c5276
This is the test case that exhibits the ICE: zerocopy/zerocopy-derive/tests/struct_known_layout.rs Lines 50 to 59 in ac7b8e8
The problematic quality here is the lifetimes in the trailing field. Lifetimes in leading fields are unproblematic. Not yet sure how to work around this. |
Hot take: what if we don't work around it? The likelihood of someone encountering this in practice is extremely low since our MSRV is so old. If someone does, we can explain that it's a compiler bug and thus not really our fault. I'd want to figure out what toolchain fixed this ICE. If it's a recent toolchain, then I wouldn't want to go this route, but if it's like 1.58 or something, then I'm okay with it. |
The last problematic rustc version is 1.61, released May 19, 2022. |
Static lifetimes are also unproblematic. |
87046ff
to
2a40051
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## v0.8.x #2127 +/- ##
=======================================
Coverage 87.42% 87.42%
=======================================
Files 16 16
Lines 6115 6115
=======================================
Hits 5346 5346
Misses 769 769 ☔ View full report in Codecov by Sentry. |
b4e7382
to
c7cfc8d
Compare
#(::zerocopy::util::macro_util::core_reexport::mem::MaybeUninit< | ||
<#ident #ty_generics as | ||
::zerocopy::util::macro_util::Field<#leading_field_indices> | ||
>::Type | ||
>,)* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment here explaining why we indirect via Field
rather than just using the field types directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also mention #2116 in this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more change requested, but otherwise LGTM.
This commit revises the `KnownLayout` derive on `repr(C)` target structs to preserve the correct resolution of `Self` when constructing `__ZerocopyKnownLayoutMaybeUninit`. This type contains a `MaybeUninit` version of each of the target struct's field types. Previously, it achieved this by simply copying the tokens corresponding to field types from the definition of the target struct into the definition of `__ZerocopyKnownLayoutMaybeUninit` However, on types like this: #[repr(C)] struct Struct([u8; Self::N]); …this approach is insufficient. Pasted into `__ZerocopyKnownLayoutMaybeUninit`, `Self` ceases to refer to the target struct and instead refers to `__ZerocopyKnownLayoutMaybeUninit`. To preserve `Self` hygiene, this commit defines a struct for projecting the field types of the target struct based on their index: pub unsafe trait Field<Index> { type Type: ?Sized; } …then implements it for each of the field types of the target struct; e.g.: struct Index<const N: usize>; impl Field<Index<0>> for Struct { type Type = [u8; Self::N]; } With this, the fields of `__ZerocopyKnownLayoutMaybeUninit` can be defined hygienically; e.g., as `<Struct as Field<0>>::Type`. Fixes #2116
This commit revises the `KnownLayout` derive on `repr(C)` target structs to preserve the correct resolution of `Self` when constructing `__ZerocopyKnownLayoutMaybeUninit`. This type contains a `MaybeUninit` version of each of the target struct's field types. Previously, it achieved this by simply copying the tokens corresponding to field types from the definition of the target struct into the definition of `__ZerocopyKnownLayoutMaybeUninit` However, on types like this: #[repr(C)] struct Struct([u8; Self::N]); …this approach is insufficient. Pasted into `__ZerocopyKnownLayoutMaybeUninit`, `Self` ceases to refer to the target struct and instead refers to `__ZerocopyKnownLayoutMaybeUninit`. To preserve `Self` hygiene, this commit defines a struct for projecting the field types of the target struct based on their index: pub unsafe trait Field<Index> { type Type: ?Sized; } …then implements it for each of the field types of the target struct; e.g.: struct Index<const N: usize>; impl Field<Index<0>> for Struct { type Type = [u8; Self::N]; } With this, the fields of `__ZerocopyKnownLayoutMaybeUninit` can be defined hygienically; e.g., as `<Struct as Field<0>>::Type`. Fixes google#2116
This commit revises the `KnownLayout` derive on `repr(C)` target structs to preserve the correct resolution of `Self` when constructing `__ZerocopyKnownLayoutMaybeUninit`. This type contains a `MaybeUninit` version of each of the target struct's field types. Previously, it achieved this by simply copying the tokens corresponding to field types from the definition of the target struct into the definition of `__ZerocopyKnownLayoutMaybeUninit` However, on types like this: #[repr(C)] struct Struct([u8; Self::N]); …this approach is insufficient. Pasted into `__ZerocopyKnownLayoutMaybeUninit`, `Self` ceases to refer to the target struct and instead refers to `__ZerocopyKnownLayoutMaybeUninit`. To preserve `Self` hygiene, this commit defines a struct for projecting the field types of the target struct based on their index: pub unsafe trait Field<Index> { type Type: ?Sized; } …then implements it for each of the field types of the target struct; e.g.: struct Index<const N: usize>; impl Field<Index<0>> for Struct { type Type = [u8; Self::N]; } With this, the fields of `__ZerocopyKnownLayoutMaybeUninit` can be defined hygienically; e.g., as `<Struct as Field<0>>::Type`. Fixes #2116 Co-authored-by: Jack Wrenn <jswrenn@amazon.com>
This commit revises the
KnownLayout
derive onrepr(C)
target structs to preserve the correct resolution ofSelf
when constructing__ZerocopyKnownLayoutMaybeUninit
. This type contains aMaybeUninit
version of each of the target struct's field types. Previously, it achieved this by simply copying the tokens corresponding to field types from the definition of the target struct into the definition of__ZerocopyKnownLayoutMaybeUninit
However, on types like this:
…this approach is insufficient. Pasted into
__ZerocopyKnownLayoutMaybeUninit
,Self
ceases to refer to the target struct and instead refers to__ZerocopyKnownLayoutMaybeUninit
.To preserve
Self
hygiene, this commit defines a struct for projecting the field types of the target struct based on their index:…then implements it for each of the field types of the target struct; e.g.:
With this, the fields of
__ZerocopyKnownLayoutMaybeUninit
can be defined hygienically; e.g., as<Struct as Field<0>>::Type
.Fixes #2116