Skip to content

Commit 27b7ae4

Browse files
committed
Fix type arity error in padding check
Use `Self` in padding check instead of type name. Fixes #1642 Makes progress towards #671
1 parent 1271aa0 commit 27b7ae4

File tree

8 files changed

+82
-10
lines changed

8 files changed

+82
-10
lines changed

zerocopy-derive/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1286,10 +1286,10 @@ fn impl_block<D: DataExt>(
12861286
let t = tag.iter();
12871287
parse_quote! {
12881288
(): ::zerocopy::util::macro_util::PaddingFree<
1289-
#type_ident,
1289+
Self,
12901290
{
12911291
#validator_context
1292-
::zerocopy::#validator_macro!(#type_ident, #(#t,)* #(#variant_types),*)
1292+
::zerocopy::#validator_macro!(Self, #(#t,)* #(#variant_types),*)
12931293
}
12941294
>
12951295
}

zerocopy-derive/src/output_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ fn test_into_bytes() {
322322
u8: ::zerocopy::IntoBytes,
323323
u8: ::zerocopy::IntoBytes,
324324
(): ::zerocopy::util::macro_util::PaddingFree<
325-
Foo,
326-
{ ::zerocopy::struct_has_padding!(Foo, [u8, u8]) },
325+
Self,
326+
{ ::zerocopy::struct_has_padding!(Self, [u8, u8]) },
327327
>,
328328
{
329329
fn only_derive_is_allowed_to_implement_this_trait() {}

zerocopy-derive/tests/ui-msrv/enum.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ error: must have #[repr(C)] or #[repr(Int)] attribute in order to guarantee this
265265
|
266266
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
267267

268+
error: generic parameters may not be used in const operations
269+
--> tests/ui-msrv/enum.rs:576:7
270+
|
271+
576 | A(T),
272+
| ^ cannot perform const operation using `T`
273+
|
274+
= note: type parameters may not be used in const expressions
275+
268276
error[E0658]: custom discriminant values are not allowed in enums with tuple or struct variants
269277
--> tests/ui-msrv/enum.rs:136:9
270278
|
@@ -383,3 +391,16 @@ error[E0277]: the trait bound `(): PaddingFree<IntoBytes3, true>` is not satisfi
383391
<() as PaddingFree<T, false>>
384392
= help: see issue #48214
385393
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
394+
395+
error: generic `Self` types are currently not permitted in anonymous constants
396+
--> tests/ui-msrv/enum.rs:573:10
397+
|
398+
573 | #[derive(IntoBytes)]
399+
| ^^^^^^^^^
400+
|
401+
note: not a concrete type
402+
--> tests/ui-msrv/enum.rs:573:10
403+
|
404+
573 | #[derive(IntoBytes)]
405+
| ^^^^^^^^^
406+
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

zerocopy-derive/tests/ui-nightly/enum.rs

+6
Original file line numberDiff line numberDiff line change
@@ -569,3 +569,9 @@ enum IntoBytes4 {
569569
enum IntoBytes5 {
570570
A(u32),
571571
}
572+
573+
#[derive(IntoBytes)]
574+
#[repr(u8)]
575+
enum IntoBytes6<T> {
576+
A(T),
577+
}

zerocopy-derive/tests/ui-nightly/enum.stderr

+22
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ error: must have #[repr(C)] or #[repr(Int)] attribute in order to guarantee this
265265
|
266266
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
267267

268+
error: generic parameters may not be used in const operations
269+
--> tests/ui-nightly/enum.rs:576:7
270+
|
271+
576 | A(T),
272+
| ^ cannot perform const operation using `T`
273+
|
274+
= note: type parameters may not be used in const expressions
275+
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
276+
268277
error[E0565]: meta item in `repr` must be an identifier
269278
--> tests/ui-nightly/enum.rs:19:8
270279
|
@@ -487,6 +496,19 @@ help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
487496
9 + #![feature(trivial_bounds)]
488497
|
489498

499+
error: generic `Self` types are currently not permitted in anonymous constants
500+
--> tests/ui-nightly/enum.rs:573:10
501+
|
502+
573 | #[derive(IntoBytes)]
503+
| ^^^^^^^^^
504+
|
505+
note: not a concrete type
506+
--> tests/ui-nightly/enum.rs:573:10
507+
|
508+
573 | #[derive(IntoBytes)]
509+
| ^^^^^^^^^
510+
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
511+
490512
error[E0277]: the trait bound `bool: FromBytes` is not satisfied
491513
--> tests/ui-nightly/enum.rs:191:10
492514
|

zerocopy-derive/tests/ui-nightly/struct.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
333333
|
334334

335335
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
336-
--> tests/ui-nightly/struct.rs:127:8
336+
--> tests/ui-nightly/struct.rs:125:10
337337
|
338-
127 | struct IntoBytes4 {
339-
| ^^^^^^^^^^ doesn't have a size known at compile-time
338+
125 | #[derive(IntoBytes)]
339+
| ^^^^^^^^^ doesn't have a size known at compile-time
340340
|
341341
= help: within `IntoBytes4`, the trait `Sized` is not implemented for `[u8]`, which is required by `IntoBytes4: macro_util::__size_of::Sized`
342342
note: required because it appears within the type `IntoBytes4`
@@ -350,6 +350,7 @@ note: required by a bound in `macro_util::__size_of::size_of`
350350
|
351351
| pub const fn size_of<T: Sized + ?core::marker::Sized>() -> usize {
352352
| ^^^^^ required by this bound in `size_of`
353+
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
353354

354355
error[E0277]: `[u8]` is unsized
355356
--> tests/ui-nightly/struct.rs:129:8

zerocopy-derive/tests/ui-stable/enum.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ error: must have #[repr(C)] or #[repr(Int)] attribute in order to guarantee this
265265
|
266266
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
267267

268+
error: generic parameters may not be used in const operations
269+
--> tests/ui-stable/enum.rs:576:7
270+
|
271+
576 | A(T),
272+
| ^ cannot perform const operation using `T`
273+
|
274+
= note: type parameters may not be used in const expressions
275+
268276
error[E0565]: meta item in `repr` must be an identifier
269277
--> tests/ui-stable/enum.rs:19:8
270278
|
@@ -451,6 +459,19 @@ error[E0277]: `IntoBytes3` has inter-field padding
451459
= help: see issue #48214
452460
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
453461

462+
error: generic `Self` types are currently not permitted in anonymous constants
463+
--> tests/ui-stable/enum.rs:573:10
464+
|
465+
573 | #[derive(IntoBytes)]
466+
| ^^^^^^^^^
467+
|
468+
note: not a concrete type
469+
--> tests/ui-stable/enum.rs:573:10
470+
|
471+
573 | #[derive(IntoBytes)]
472+
| ^^^^^^^^^
473+
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
474+
454475
error[E0277]: the trait bound `bool: FromBytes` is not satisfied
455476
--> tests/ui-stable/enum.rs:191:10
456477
|

zerocopy-derive/tests/ui-stable/struct.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,10 @@ error[E0277]: `IntoBytes3` has inter-field padding
294294
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
295295

296296
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
297-
--> tests/ui-stable/struct.rs:127:8
297+
--> tests/ui-stable/struct.rs:125:10
298298
|
299-
127 | struct IntoBytes4 {
300-
| ^^^^^^^^^^ doesn't have a size known at compile-time
299+
125 | #[derive(IntoBytes)]
300+
| ^^^^^^^^^ doesn't have a size known at compile-time
301301
|
302302
= help: within `IntoBytes4`, the trait `Sized` is not implemented for `[u8]`, which is required by `IntoBytes4: macro_util::__size_of::Sized`
303303
note: required because it appears within the type `IntoBytes4`
@@ -311,6 +311,7 @@ note: required by a bound in `macro_util::__size_of::size_of`
311311
|
312312
| pub const fn size_of<T: Sized + ?core::marker::Sized>() -> usize {
313313
| ^^^^^ required by this bound in `size_of`
314+
= note: this error originates in the derive macro `IntoBytes` (in Nightly builds, run with -Z macro-backtrace for more info)
314315

315316
error[E0277]: `[u8]` is unsized
316317
--> tests/ui-stable/struct.rs:129:8

0 commit comments

Comments
 (0)