@@ -446,6 +446,69 @@ use {FromZeros as FromZeroes, IntoBytes as AsBytes, Ref as LayoutVerified};
446
446
///
447
447
/// This derive cannot currently be applied to unsized structs without an
448
448
/// explicit `repr` attribute.
449
+ ///
450
+ /// Some invocations of this derive run afoul of a [known bug] in Rust's type
451
+ /// privacy checker. For example, this code:
452
+ ///
453
+ /// ```compile_fail,E0446
454
+ /// use zerocopy::*;
455
+ /// # use zerocopy_derive::*;
456
+ ///
457
+ /// #[derive(KnownLayout)]
458
+ /// #[repr(C)]
459
+ /// pub struct PublicType {
460
+ /// leading: Foo,
461
+ /// trailing: Bar,
462
+ /// }
463
+ ///
464
+ /// #[derive(KnownLayout)]
465
+ /// struct Foo;
466
+ ///
467
+ /// #[derive(KnownLayout)]
468
+ /// struct Bar;
469
+ /// ```
470
+ ///
471
+ /// ...results in a compilation error:
472
+ ///
473
+ /// ```text
474
+ /// error[E0446]: private type `Bar` in public interface
475
+ /// --> examples/bug.rs:3:10
476
+ /// |
477
+ /// 3 | #[derive(KnownLayout)]
478
+ /// | ^^^^^^^^^^^ can't leak private type
479
+ /// ...
480
+ /// 14 | struct Bar;
481
+ /// | ---------- `Bar` declared as private
482
+ /// |
483
+ /// = note: this error originates in the derive macro `KnownLayout` (in Nightly builds, run with -Z macro-backtrace for more info)
484
+ /// ```
485
+ ///
486
+ /// This issue arises when `#[derive(KnownLayout)]` is applied to `repr(C)`
487
+ /// structs whose trailing field type is less public than the enclosing struct.
488
+ ///
489
+ /// To work around this, mark the trailing field type `pub` and annotate it with
490
+ /// `#[doc(hidden)]`; e.g.:
491
+ ///
492
+ /// ```no_run
493
+ /// use zerocopy::*;
494
+ /// # use zerocopy_derive::*;
495
+ ///
496
+ /// #[derive(KnownLayout)]
497
+ /// #[repr(C)]
498
+ /// pub struct PublicType {
499
+ /// leading: Foo,
500
+ /// trailing: Bar,
501
+ /// }
502
+ ///
503
+ /// #[derive(KnownLayout)]
504
+ /// struct Foo;
505
+ ///
506
+ /// #[doc(hidden)]
507
+ /// #[derive(KnownLayout)]
508
+ /// pub struct Bar; // <- `Bar` is now also `pub`
509
+ /// ```
510
+ ///
511
+ /// [known bug]: https://github.com/rust-lang/rust/issues/45713
449
512
#[ cfg( any( feature = "derive" , test) ) ]
450
513
#[ cfg_attr( doc_cfg, doc( cfg( feature = "derive" ) ) ) ]
451
514
pub use zerocopy_derive:: KnownLayout ;
0 commit comments