diff --git a/repro_direct b/repro_direct new file mode 100755 index 0000000000..6577973544 Binary files /dev/null and b/repro_direct differ diff --git a/repro_macro b/repro_macro new file mode 100755 index 0000000000..b1fd4d7538 Binary files /dev/null and b/repro_macro differ diff --git a/zerocopy-derive/tests/issue_2177.rs b/zerocopy-derive/tests/issue_2177.rs new file mode 100644 index 0000000000..7010f9d7b4 --- /dev/null +++ b/zerocopy-derive/tests/issue_2177.rs @@ -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); +} diff --git a/zerocopy-derive/tests/issue_2177_control.rs b/zerocopy-derive/tests/issue_2177_control.rs new file mode 100644 index 0000000000..8467763318 --- /dev/null +++ b/zerocopy-derive/tests/issue_2177_control.rs @@ -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 { + 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); +}