Skip to content

Commit 3777e46

Browse files
committed
[doc] Document rustc type privacy bug
See #1292 Makes progress towards #671
1 parent 73b15e5 commit 3777e46

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/lib.rs

+63
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,69 @@ use {FromZeros as FromZeroes, IntoBytes as AsBytes, Ref as LayoutVerified};
446446
///
447447
/// This derive cannot currently be applied to unsized structs without an
448448
/// 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
449512
#[cfg(any(feature = "derive", test))]
450513
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
451514
pub use zerocopy_derive::KnownLayout;

0 commit comments

Comments
 (0)