use proc_macro::TokenStream as TokenStream1;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2_diagnostic::{ToTokens, prelude::*};
use quote::{ToTokens as _, format_ident, quote};
use syn::{
Attribute, Data, DeriveInput, ExprAssign, Field, Fields, Generics, Ident, Meta, Token, Type,
parse_quote, punctuated::Punctuated, spanned::Spanned,
};
#[proc_macro_derive(DST)]
pub fn derive_dst(input: TokenStream1) -> TokenStream1 {
impl_dst(input.into()).to_tokens()
}
fn impl_dst(input: TokenStream2) -> DiagnosticStream {
let ast: DeriveInput = syn::parse2(input).expect("derive macro input");
let dst = DstStruct::parse(ast)?;
Ok(dst.generate())
}
#[derive(Debug)]
pub(crate) struct DstStruct {
name: Ident,
mirror_name: Ident,
generics: Generics,
attrs: Vec<Attribute>,
layout_head: Punctuated<ExprAssign, Token![;]>,
mirror_head: Punctuated<Field, Token![,]>,
mirror_tail: Field,
tail_elem: Type,
count_ty: Type,
}
fn is_repr_c(attrs: &[Attribute], name: &Ident) -> DiagnosticResult<()> {
let repr_c: Attribute = parse_quote!(#[repr(C)]);
let has_repr_c = attrs.iter().any(|attr| attr == &repr_c);
let other_repr = attrs
.iter()
.find(|attr| attr.path().is_ident("repr") && *attr != &repr_c);
match (has_repr_c, other_repr) {
(true, None) => attrs.iter().try_for_each(|attr| check_for_cfg_attr_repr(&attr.meta)),
(true, Some(repr)) => {
error("DST requires exactly `#[repr(C)]`: `packed`, `align(N)` and other repr hints change the layout, so the generated allocation code would be unsound")
.add_help(repr.span(), "remove this additional repr")
},
(false, None) => {
attrs.iter().try_for_each(|attr| check_for_cfg_attr_repr(&attr.meta))?;
error("DST requires #[repr(C)] so the layout can be generated for allocation")
.add_help(name.span(), "add #[repr(C)] above this")
},
(false, Some(repr)) => {
error("DST requires #[repr(C)] so the layout can be generated for allocation")
.add_help(repr.span(), "change this to `#[repr(C)]`")
},
}
}
fn check_for_cfg_attr_repr(meta: &Meta) -> DiagnosticResult<()> {
let Some(cfg_attr) = meta
.require_list()
.ok()
.filter(|meta_list| meta_list.path.is_ident("cfg_attr"))
else {
return Ok(());
};
let repr_c: Attribute = parse_quote!(#[repr(C)]);
let repr_c = repr_c.meta;
let p = cfg_attr
.parse_args_with(Punctuated::<Meta, Token![,]>::parse_separated_nonempty)
.expect("cfg_attr takes comma separated list");
let has_repr_c = p.iter().find(|meta| *meta == &repr_c);
let other_repr = p
.iter()
.find(|meta| meta.path().is_ident("repr") && *meta != &repr_c);
match (has_repr_c, other_repr) {
(Some(repr_c), _) => {
error("DST requires #[repr(C)] so the layout can be generated for allocation")
.add_help(
repr_c.span(),
"this `repr(C)` is within a `#[cfg_attr(...)]` gate",
)?
},
(None, Some(other_repr)) => {
error("DST requires exactly `#[repr(C)]`: `packed`, `align(N)` and other repr hints change the layout, so the generated allocation code would be unsound")
.add_help(other_repr.span(), "remove this additional repr")?
},
(None, None) => p.iter().try_for_each(check_for_cfg_attr_repr),
}
}
fn remove_doc_and_derive(attrs: &mut Vec<Attribute>) {
attrs.retain_mut(|attr| keep(&mut attr.meta))
}
fn keep(meta: &mut Meta) -> bool {
if let Meta::List(meta_list) = meta
&& meta_list.path.is_ident("cfg_attr")
{
let mut p = meta_list
.parse_args_with(Punctuated::<Meta, Token![,]>::parse_separated_nonempty)
.expect("cfg_attr takes comma separated list");
p.retain(keep);
if p.len() < 2 {
false
} else {
meta_list.tokens = p.into_token_stream();
true
}
} else {
!(meta.path().is_ident("derive") || meta.path().is_ident("doc"))
}
}
fn parse_struct(
data: Data,
name: &Ident,
) -> DiagnosticResult<(Punctuated<Field, Token![,]>, Field)> {
match data {
Data::Struct(struct_data) => match struct_data.fields {
Fields::Named(mut fields) => {
let tail = fields.named.pop().expect("not a unit struct");
fields.named.iter().try_for_each(is_valid_head_field)?;
has_no_default(&tail)?;
Ok((fields.named, tail))
}
Fields::Unnamed(fields) => {
error("DST can only be derived for a struct with named fields").add_help(
fields.paren_token.span.span(),
"tuple structs are not supported",
)
}
Fields::Unit => error("DST requires at least one field (an unsized slice tail)")
.add_help(name.span(), "add a tail field like `DevicePath: [u16]`"),
},
Data::Enum(enum_data) => error("DST can only be derived for a struct")
.add_help(enum_data.enum_token.span(), "not a struct"),
Data::Union(union_data) => error("DST can only be derived for a struct")
.add_help(union_data.union_token.span(), "not a struct"),
}
}
fn is_valid_head_field(field: &Field) -> DiagnosticResult<()> {
match &field.ty {
Type::Slice(slice) => error("only the last field of a DST struct may be unsized").add_help(
slice.bracket_token.span.span(),
"move this slice to the last position, or make it sized e.g. `[u16; 4]`",
),
Type::TraitObject(trait_object) => {
error("only the last field of a DST struct may be unsized").add_note(
trait_object.span(),
"deriving DSTs with `dyn Trait` as a tail is not supported",
)
}
Type::Path(path) if path.path.is_ident("str") => {
error("only the last field of a DST struct may be unsized").add_note(
path.span(),
"deriving DSTs with `str` as a tail is not supported",
)
}
_ => has_no_default(field),
}
}
fn has_no_default(field: &Field) -> DiagnosticResult<()> {
if let Some((_eq, expr)) = &field.default {
error("DST does not support default field values (`field: Type = expr`)").add_help(
expr.span(),
"write the field in your constructor (e.g. `new`) before `assume_init`",
)
} else {
Ok(())
}
}
impl DstStruct {
pub(crate) fn parse(mut ast: DeriveInput) -> DiagnosticResult<Self> {
let name = ast.ident;
let mirror_name = format_ident!("Uninit{}", name);
is_repr_c(&ast.attrs, &name)?;
remove_doc_and_derive(&mut ast.attrs);
let (fields, tail) = parse_struct(ast.data, &name)?;
let tail_elem = match &tail.ty {
Type::Slice(slice) => *slice.elem.clone(),
_ => return error(
"the last field of a DST struct must be an unsized slice, e.g. `DevicePath: [u16]`",
)
.add_help(tail.ty.span(), "this field is sized"),
};
let mut mirror_tail = tail.clone();
mirror_tail.ty = parse_quote!([::core::mem::MaybeUninit<#tail_elem>]);
let count_ty = parse_quote!(<[#tail_elem] as ::core::ptr::Pointee>::Metadata);
let mut mirror_head = fields.clone();
for field in mirror_head.iter_mut() {
let ty = field.ty.clone();
field.ty = parse_quote!(::core::mem::MaybeUninit<#ty>)
}
let layout_head: Punctuated<ExprAssign, syn::token::Semi> = fields
.iter()
.map(|field| -> ExprAssign {
let gates = field
.attrs
.iter()
.filter(|attr| attr.path().is_ident("cfg") || attr.path().is_ident("cfg_attr"));
let ty = &field.ty;
parse_quote! {
#(#gates)*
prefix = prefix.extend(::core::alloc::Layout::new::<#ty>())?.0
}
})
.collect();
let where_clause = match &mut ast.generics.where_clause {
Some(where_clause) => where_clause,
None => ast.generics.make_where_clause(),
};
where_clause
.predicates
.push(parse_quote!(#tail_elem: ::core::marker::Sized));
where_clause
.predicates
.push(parse_quote!([#tail_elem]: ::core::ptr::Pointee<Metadata = usize>));
Ok(Self {
name,
mirror_name,
generics: ast.generics,
attrs: ast.attrs,
layout_head,
mirror_head,
mirror_tail,
tail_elem,
count_ty,
})
}
pub(crate) fn generate(self) -> TokenStream2 {
let DstStruct {
name,
mirror_name,
generics,
attrs,
layout_head,
mirror_head,
mirror_tail,
tail_elem,
count_ty,
} = self;
let mirror_head_fields = mirror_head.pairs();
let header_extends = layout_head.pairs();
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
quote! {
#(#attrs)*
#[doc = concat!("`MaybeUninit` mirror of [`", stringify!(#name), "}`]: identical `repr(C)` layout, but every field is")]
#[doc = "zero-initialised and wrapped in `MaybeUninit`. Constructed by `new_uninit_for_...` methods"]
#[doc = "converted back to the initialised type with [`Self::assume_init`]."]
#[derive(Debug)]
struct #mirror_name #generics #where_clause {
#(#mirror_head_fields)*
#mirror_tail
}
impl #impl_generics #name #ty_generics #where_clause {
#[doc = "Allocate a buffer for a `Self` value of `size` bytes and return it boxed as the"]
#[doc = "`Uninit` mirror of `Self`, together with the `(Layout, tail offset, tail count)` tuple."]
#[doc = ""]
#[doc = "# SAFETY"]
#[doc = "The allocation is entirely **uninitialised**: write every field with"]
#[doc = "[`MaybeUninit::write`], then convert with `assume_init` on the returned box."]
#[doc = ""]
#[doc = "# Note"]
#[doc = "a `size` that does not match the correctly aligned and padded layout for the derived"]
#[doc = "tail count is **not** rejected — the returned `Layout` carries the valid size"]
#[doc = "(`layout.size()`), so the caller can compare it with the requested `size` and decide how to"]
#[doc = "handle a mismatch before calling `assume_init`."]
fn new_uninit_for_size(
size: usize,
) -> ::core::result::Result<
(
::std::boxed::Box<#mirror_name #ty_generics>,
(::core::alloc::Layout, usize, #count_ty),
),
::core::alloc::LayoutError,
> {
unsafe {
::core::result::Result::Ok(Self::new_uninit_for(Self::layout_for_size(size)?))
}
}
#[doc = "Allocate a buffer holding `count` tail elements and return it boxed as the `Uninit`"]
#[doc = "mirror of `Self`, together with the `(Layout, tail offset, tail count)` tuple. `count` is"]
#[doc = "typed as the tail slice's `Pointee::Metadata` (a `usize` for `[T]`)."]
#[doc = ""]
#[doc = "# SAFETY"]
#[doc = "The allocation is entirely **uninitialised**: write every field with"]
#[doc = "[`MaybeUninit::write`], then convert with `assume_init` on the returned box."]
fn new_uninit_for_count(
count: #count_ty,
) -> ::core::result::Result<
(
::std::boxed::Box<#mirror_name #ty_generics>,
(::core::alloc::Layout, usize, #count_ty),
),
::core::alloc::LayoutError,
> {
unsafe {
::core::result::Result::Ok(Self::new_uninit_for(Self::layout_for_count(count)?))
}
}
#[doc = "Allocate a buffer with the given `layout` and return it boxed as the"]
#[doc = "`Uninit` mirror of `Self`, together with the `(Layout, tail offset, tail"]
#[doc = "count)` tuple it was built for."]
#[doc = ""]
#[doc = "# SAFETY"]
#[doc = "The allocation is entirely **uninitialised**: write every field with"]
#[doc = "[`MaybeUninit::write`], then convert with `assume_init` on the returned box."]
#[doc = ""]
#[doc = "Extra care must be taken when using this function directly to ensure that"]
#[doc = "`layout` and `count` are valid in combination. No validation is performed"]
#[doc = "or possible in this function. Prefer the safe versions [`new_uninit_for_size`]"]
#[doc = "and [`new_uninit_for_count] wherever possible."]
unsafe fn new_uninit_for(
(layout, offset, count): (::core::alloc::Layout, usize, #count_ty),
) -> (
::std::boxed::Box<#mirror_name #ty_generics>,
(::core::alloc::Layout, usize, #count_ty),
) {
let raw = match <::std::alloc::Global as ::core::alloc::Allocator>::allocate_zeroed(
&::std::alloc::Global,
layout,
) {
::core::result::Result::Ok(pointer) => pointer,
::core::result::Result::Err(_) => ::std::alloc::handle_alloc_error(layout),
};
let wide = ::core::ptr::from_raw_parts_mut::<#mirror_name #ty_generics>(
raw.cast::<()>().as_ptr(),
count,
);
let boxed = unsafe { ::std::boxed::Box::from_raw(wide) };
(boxed, (layout, offset, count))
}
#[doc = "Calculate the `(layout, offset of the unsized tail, tail element count)`"]
#[doc = "for a `Self` buffer of `size` bytes."]
#[doc = ""]
#[doc = "# SAFETY"]
#[doc = "The correctness of this layout and the tail offset is relied upon by unsafe code"]
#[doc = "in [`new_uninit_for`][Self::new_uninit_for] (via [`new_uninit_for_size`][Self::new_uninit_for_size])."]
fn layout_for_size(
size: usize,
) -> ::core::result::Result<
(::core::alloc::Layout, usize, #count_ty),
::core::alloc::LayoutError,
> {
let header = Self::layout_header()?;
let elem_size = ::core::mem::size_of::<#tail_elem>();
let tail_bytes = size.saturating_sub(header.size());
let count: #count_ty = tail_bytes.checked_div(elem_size).unwrap_or(0);
Self::extend_with_tail(header, count)
}
#[doc = "Calculate the `(layout, offset of the unsized tail, tail element count)`"]
#[doc = "for a `Self` buffer holding `count` tail elements."]
#[doc = ""]
#[doc = "# SAFETY"]
#[doc = "The correctness of this layout and the tail offset is relied upon by unsafe code"]
#[doc = "in [`new_uninit_for`][Self::new_uninit_for] (via [`new_uninit_for_count`][Self::new_uninit_for_count])."]
fn layout_for_count(
count: #count_ty,
) -> ::core::result::Result<
(::core::alloc::Layout, usize, #count_ty),
::core::alloc::LayoutError,
> {
Self::extend_with_tail(Self::layout_header()?, count)
}
#[doc = "The [`Layout`] of the sized head (everything before the unsized tail)"]
fn layout_header() -> ::core::result::Result<::core::alloc::Layout, ::core::alloc::LayoutError> {
let mut prefix = ::core::alloc::Layout::new::<()>();
#(#header_extends)*;
::core::result::Result::Ok(prefix)
}
#[doc = "Extend a head [`Layout`] (from [`layout_header`][Self::layout_header]) with the tail of"]
#[doc = "`count` elements and return the resulting `(layout, tail offset, count)` tuple, padded"]
#[doc = "to the alignment."]
fn extend_with_tail(
header: ::core::alloc::Layout,
count: #count_ty,
) -> ::core::result::Result<
(::core::alloc::Layout, usize, #count_ty),
::core::alloc::LayoutError,
> {
let tail = ::core::alloc::Layout::array::<#tail_elem>(count)?;
let (extended, offset) = header.extend(tail)?;
::core::result::Result::Ok((extended.pad_to_align(), offset, count))
}
}
impl #impl_generics #mirror_name #ty_generics #where_clause {
#[doc = "Convert into the fully initialised struct, reusing the same allocation."]
#[doc = ""]
#[doc = "# SAFETY"]
#[doc = "Every head field & all tail elements must have been initialised to a valid"]
#[doc = "value for the respective types. All fields will have been initialised zeroed, but zero"]
#[doc = "is not a valid value for all types. Calling `assume_init` where some fields"]
#[doc = "are not properly initialised will lead to UB. See [`core::mem::MaybeUninit::assume_init`]"]
#[doc = "for more details."]
unsafe fn assume_init(
self: ::std::boxed::Box<Self>,
) -> ::std::boxed::Box<#name #ty_generics> {
let raw = ::std::boxed::Box::into_raw(self);
let wide = ::core::ptr::from_raw_parts_mut::<#name #ty_generics>(
raw.cast::<()>(),
::core::ptr::metadata(raw),
);
unsafe { ::std::boxed::Box::from_raw(wide) }
}
}
}
}
}
#[cfg(test)]
mod tests {
use syn::parse_quote;
use super::*;
#[test]
fn remove_derive() {
let mut foo: Attribute = parse_quote! {#[derive(Debug)]};
assert!(!keep(&mut foo.meta));
}
#[test]
fn remove_doc() {
let mut foo: Attribute = parse_quote! {#[doc(inline)]};
assert!(!keep(&mut foo.meta));
}
#[test]
fn remove_derive_from_cfg_attr() {
let mut foo: Attribute =
parse_quote! {#[cfg_attr(test, derive(Debug), deprecated, doc = "foo")]};
let expected: Attribute = parse_quote! {#[cfg_attr(test, deprecated)]};
assert!(keep(&mut foo.meta));
assert_eq!(foo, expected);
}
#[test]
fn remove_empty_cfg_attr() {
let mut foo: Attribute = parse_quote! {#[cfg_attr(test, derive(Debug), doc = "foo")]};
assert!(!keep(&mut foo.meta));
}
#[test]
fn nested_cfg_attr() {
let mut foo: Attribute = parse_quote! {#[cfg_attr(test, cfg_attr(any(), derive(Debug)), cfg_attr(any(), deprecated, doc = "foo"))]};
let expected: Attribute = parse_quote! {#[cfg_attr(test, cfg_attr(any(), deprecated))]};
assert!(keep(&mut foo.meta));
assert_eq!(foo, expected);
}
#[test]
fn repr_c_alone_is_repr_c() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_ok());
}
#[test]
fn no_repr_is_an_error() {
let ast: DeriveInput = parse_quote! {
#[derive(Debug)]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn packed_on_a_separate_repr_line_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[repr(packed)]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn packed_in_the_same_repr_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C, packed)]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn align_in_repr_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C, align(16))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_gated_repr_c_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", repr(C))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_gated_repr_c_without_plain_repr_c_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[cfg_attr(feature = "extra", repr(C))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_gated_packed_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", repr(packed))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_gated_align_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(all(), repr(align(16)))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_gated_transparent_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", repr(transparent))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_gated_bare_repr_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", repr)]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_gated_repr_c_and_packed_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", repr(C, packed))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_gating_repr_alongside_other_attrs_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", derive(Debug), repr(packed))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_with_multiple_reprs_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", repr(C), repr(packed))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_with_complex_condition_gating_repr_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(all(unix, feature = "extra"), repr(C))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn nested_cfg_attr_gated_repr_c_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "a", cfg_attr(feature = "b", repr(C)))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn nested_cfg_attr_gated_packed_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "a", cfg_attr(all(), repr(packed)))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn triple_nested_cfg_attr_gated_align_is_rejected() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(a, cfg_attr(b, cfg_attr(c, repr(align(8)))))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_error());
}
#[test]
fn cfg_attr_with_non_repr_attrs_is_ok() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", allow(dead_code), deprecated)]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_ok());
}
#[test]
fn cfg_attr_with_derive_is_ok() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(test, derive(Clone))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_ok());
}
#[test]
fn cfg_attr_doc_mentioning_repr_is_ok() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", doc = "use `repr(C)`, never `repr(packed)`")]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_ok());
}
#[test]
fn cfg_attr_macro_attr_containing_repr_tokens_is_ok() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", my_macro(repr(C)))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_ok());
}
#[test]
fn nested_cfg_attr_without_repr_is_ok() {
let ast: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "a", cfg_attr(feature = "b", allow(dead_code)))]
struct Foo { tail: [u8] }
};
assert!(is_repr_c(&ast.attrs, &ast.ident).is_ok());
}
#[test]
fn check_for_cfg_attr_repr_ignores_non_cfg_attr() {
let attr: Attribute = parse_quote!(#[allow(dead_code)]);
assert!(check_for_cfg_attr_repr(&attr.meta).is_ok());
let attr: Attribute = parse_quote!(#[repr(C)]);
assert!(check_for_cfg_attr_repr(&attr.meta).is_ok());
}
#[test]
fn check_for_cfg_attr_repr_flags_gated_repr_c() {
let attr: Attribute = parse_quote!(#[cfg_attr(feature = "extra", repr(C))]);
assert!(check_for_cfg_attr_repr(&attr.meta).is_error());
}
#[test]
fn check_for_cfg_attr_repr_flags_nested_gated_repr() {
let attr: Attribute = parse_quote!(#[cfg_attr(a, cfg_attr(b, repr(align(8))))]);
assert!(check_for_cfg_attr_repr(&attr.meta).is_error());
}
#[test]
fn check_for_cfg_attr_repr_ok_without_repr() {
let attr: Attribute =
parse_quote!(#[cfg_attr(feature = "extra", allow(dead_code), deprecated)]);
assert!(check_for_cfg_attr_repr(&attr.meta).is_ok());
}
#[test]
fn parse_rejects_cfg_attr_gated_repr() {
let input: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "extra", repr(packed))]
#[derive(DST)]
struct Foo { tail: [u8] }
};
assert!(DstStruct::parse(input).is_error());
}
#[test]
fn parse_rejects_nested_cfg_attr_gated_repr_c() {
let input: DeriveInput = parse_quote! {
#[repr(C)]
#[cfg_attr(feature = "a", cfg_attr(feature = "b", repr(C)))]
#[derive(DST)]
struct Foo { tail: [u8] }
};
assert!(DstStruct::parse(input).is_error());
}
#[test]
fn parse_cstruct() {
let input: DeriveInput = parse_quote! {
#[repr(C)]
#[doc(ignore)]
#[derive(Debug, DST)]
#[cfg_attr(test, deprecated, derive(Clone))]
struct DeviceDetails {
cbSize: u32,
DevicePath: [u16]
}
};
let device_info = DstStruct::parse(input).unwrap();
assert_eq!(device_info.name, format_ident!("DeviceDetails"));
assert_eq!(
device_info.mirror_tail,
parse_quote!(DevicePath: [::core::mem::MaybeUninit<u16>])
);
assert_eq!(device_info.tail_elem, parse_quote!(u16));
}
}