Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove incorrect spanned quote usage #279

Merged
merged 1 commit into from
Oct 19, 2024
Merged
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
21 changes: 9 additions & 12 deletions derive/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{cmp, convert::TryFrom};

use proc_macro2::{Ident, Span, TokenStream, TokenTree};
use quote::{quote, quote_spanned, ToTokens};
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream, Parser},
punctuated::Punctuated,
Expand Down Expand Up @@ -553,7 +553,7 @@ fn get_struct_fields(input: &DeriveInput) -> Result<&Fields> {

/// Extract the `Fields` off a `DeriveInput`, or, in the `enum` case, off
/// those of the `enum_variant`, when provided (e.g., for `Zeroable`).
///
///
/// We purposely allow not providing an `enum_variant` for cases where
/// the caller wants to reject supporting `enum`s (e.g., `NoPadding`).
fn get_fields(
Expand Down Expand Up @@ -664,7 +664,7 @@ fn generate_checked_bit_pattern_enum_without_fields(
)?;

let check = if count == 0 {
quote_spanned!(span => false)
quote!(false)
} else if max - min == count - 1 {
// contiguous range
let min_lit = LitInt::new(&format!("{}", min), span);
Expand Down Expand Up @@ -962,22 +962,20 @@ fn generate_checked_bit_pattern_enum_with_fields(
/// is equal to the sum of the size of it's fields
fn generate_assert_no_padding(input: &DeriveInput) -> Result<TokenStream> {
let struct_type = &input.ident;
let span = input.ident.span();
let enum_variant = None; // `no padding` check is not supported for `enum`s yet.
let fields = get_fields(input, enum_variant)?;

let mut field_types = get_field_types(&fields);
let size_sum = if let Some(first) = field_types.next() {
let size_first = quote_spanned!(span => ::core::mem::size_of::<#first>());
let size_rest =
quote_spanned!(span => #( + ::core::mem::size_of::<#field_types>() )*);
let size_first = quote!(::core::mem::size_of::<#first>());
let size_rest = quote!(#( + ::core::mem::size_of::<#field_types>() )*);

quote_spanned!(span => #size_first #size_rest)
quote!(#size_first #size_rest)
} else {
quote_spanned!(span => 0)
quote!(0)
};

Ok(quote_spanned! {span => const _: fn() = || {
Ok(quote! {const _: fn() = || {
#[doc(hidden)]
struct TypeWithoutPadding([u8; #size_sum]);
let _ = ::core::mem::transmute::<#struct_type, TypeWithoutPadding>;
Expand All @@ -991,9 +989,8 @@ fn generate_fields_are_trait(
let (impl_generics, _ty_generics, where_clause) =
input.generics.split_for_impl();
let fields = get_fields(input, enum_variant)?;
let span = input.span();
let field_types = get_field_types(&fields);
Ok(quote_spanned! {span => #(const _: fn() = || {
Ok(quote! {#(const _: fn() = || {
#[allow(clippy::missing_const_for_fn)]
#[doc(hidden)]
fn check #impl_generics () #where_clause {
Expand Down
1 change: 1 addition & 0 deletions derive/tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(dead_code)]
#![deny(clippy::allow_attributes)]

use bytemuck::{
checked::CheckedCastError, AnyBitPattern, CheckedBitPattern, Contiguous,
Expand Down
Loading