const-field-offset: Fix warnings on nightly rust

```
warning: non-local impl definition, they should be avoided as they go against expectation
```

Part of  #4706
This commit is contained in:
Olivier Goffart 2024-02-28 09:14:40 +01:00
parent 345da48a7e
commit aa7360a91a
2 changed files with 30 additions and 3 deletions

View file

@ -217,13 +217,14 @@ pub fn const_field_offset(input: TokenStream) -> TokenStream {
})
},
Some(quote! {
const _ : () = {
/// Make sure that Unpin is not implemented
#[allow(dead_code)]
pub struct __MustNotImplUnpin<'__dummy_lifetime> (
#(#types, )*
struct __MustNotImplUnpin<'__dummy_lifetime> (
::core::marker::PhantomData<&'__dummy_lifetime ()>
);
impl<'__dummy_lifetime> Unpin for #struct_name where __MustNotImplUnpin<'__dummy_lifetime> : Unpin {};
};
}),
quote!(#crate_::AllowPin),
quote!(new_from_offset_pinned),
@ -256,7 +257,6 @@ pub fn const_field_offset(input: TokenStream) -> TokenStream {
/// Return a struct containing the offset of for the fields of this struct
pub const FIELD_OFFSETS : #field_struct_name = {
#ensure_pin_safe;
#ensure_no_unpin;
let mut len = 0usize;
#field_struct_name {
#( #fields : {
@ -272,6 +272,7 @@ pub fn const_field_offset(input: TokenStream) -> TokenStream {
}
#pinned_drop_impl
#ensure_no_unpin
};
#[cfg(feature = "field-offset-trait")]

View file

@ -117,6 +117,32 @@ mod tests {
}
}
/**
Test that one can't implement Unpin for pinned struct
This should work:
```
#[derive(const_field_offset::FieldOffsets)]
#[repr(C)]
#[pin]
struct MyStructPin { a: u32 }
```
But this not:
```compile_fail
#[derive(const_field_offset::FieldOffsets)]
#[repr(C)]
#[pin]
struct MyStructPin { a: u32 }
impl Unpin for MyStructPin {};
```
*/
#[cfg(doctest)]
const NO_IMPL_UNPIN: u32 = 0;
#[doc(hidden)]
#[cfg(feature = "field-offset-trait")]
mod internal {