Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added repro_direct
Binary file not shown.
Binary file added repro_macro
Binary file not shown.
20 changes: 20 additions & 0 deletions zerocopy-derive/tests/issue_2177.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This test reproduces the issue reported in https://github.com/google/zerocopy/issues/2177
// It uses a macro to wrap the derive, which triggers the private_bounds lint unless suppressed.

#![allow(missing_docs)]
use zerocopy::KnownLayout;

macro_rules! define {
($name:ident, $repr:ty) => {
#[derive(KnownLayout)]
#[repr(C)]
pub struct $name($repr);
}
}

define!(Foo, u8);

#[test]
fn test_issue_2177() {
let _ = Foo(0);
}
46 changes: 46 additions & 0 deletions zerocopy-derive/tests/issue_2177_control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This test serves as a control case for issue #2177.
// It manually expands the code that KnownLayout generates, but inside a macro_rules! macro.
// This does NOT trigger the private_bounds lint, proving that the issue is specific to
// the interaction between proc-macro generated spans and macro_rules! expansion.

#![allow(missing_docs)]
use zerocopy::KnownLayout;

// Mimic the internal Field trait
pub unsafe trait Field<T> {
type Type;
}

macro_rules! define {
($name:ident) => {
pub struct $name(u8);

const _: () = {
#[allow(non_camel_case_types)]
struct __Zerocopy_Field_0; // Private struct

unsafe impl Field<__Zerocopy_Field_0> for $name {
type Type = u8;
}

#[repr(C)]
#[doc(hidden)]
// The lint triggers on the following struct in the actual derive usage,
// claiming __Zerocopy_Field_0 is leaked.
// Here, it does not trigger.
pub struct __ZerocopyKnownLayoutMaybeUninit
where
<$name as Field<__Zerocopy_Field_0>>::Type: KnownLayout
{
_marker: (),
}
};
}
}

define!(Foo);

#[test]
fn test_issue_2177_control() {
let _ = Foo(0);
}
Loading