diff --git a/Cargo.toml b/Cargo.toml index c37b968..85f5497 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,3 +47,6 @@ zeroize_ = { version = "1.5", package = "zeroize", default-features = false } [package.metadata.docs.rs] all-features = true targets = [] + +[workspace.metadata.typos] +default.extend-words.wheres = "wheres" diff --git a/src/data.rs b/src/data.rs index 4b09c1c..e2f3553 100644 --- a/src/data.rs +++ b/src/data.rs @@ -4,7 +4,9 @@ mod field; mod fields; use proc_macro2::Span; -use syn::{Expr, FieldsNamed, Ident, Pat, PatPath, Path, Result, Variant}; +#[cfg(not(feature = "nightly"))] +use syn::Expr; +use syn::{FieldsNamed, Ident, Pat, PatPath, Path, Result, Variant}; pub use self::{ field::{Field, Member}, @@ -27,6 +29,7 @@ pub struct Data<'a> { pub path: Path, /// [Type](DataType) of this struct, union or variant. pub type_: DataType<'a>, + #[cfg(not(feature = "nightly"))] /// Discriminant of this variant. pub discriminant: Option<&'a Expr>, } @@ -69,7 +72,7 @@ pub enum SimpleType<'a> { /// Tuple struct or tuple variant. Tuple(&'a Fields<'a>), /// Union. - Union(&'a Fields<'a>), + Union, /// Unit variant. Unit(&'a Pat), } @@ -100,6 +103,7 @@ impl<'a> Data<'a> { ident, path, type_: DataType::Struct(fields), + #[cfg(not(feature = "nightly"))] discriminant: None, }) } @@ -117,6 +121,7 @@ impl<'a> Data<'a> { ident, path, type_: DataType::Tuple(fields), + #[cfg(not(feature = "nightly"))] discriminant: None, }) } @@ -131,6 +136,7 @@ impl<'a> Data<'a> { qself: None, path, })), + #[cfg(not(feature = "nightly"))] discriminant: None, }), syn::Fields::Unit => Err(Error::item_empty(span)), @@ -158,6 +164,7 @@ impl<'a> Data<'a> { ident, path, type_: DataType::Union(fields), + #[cfg(not(feature = "nightly"))] discriminant: None, }) } @@ -191,6 +198,7 @@ impl<'a> Data<'a> { default, type_: VariantType::Struct(fields), }, + #[cfg(not(feature = "nightly"))] discriminant: variant.discriminant.as_ref().map(|(_, expr)| expr), }) } @@ -207,6 +215,7 @@ impl<'a> Data<'a> { default, type_: VariantType::Tuple(fields), }, + #[cfg(not(feature = "nightly"))] discriminant: variant.discriminant.as_ref().map(|(_, expr)| expr), }) } @@ -226,6 +235,7 @@ impl<'a> Data<'a> { default, type_: VariantType::Unit(pattern), }, + #[cfg(not(feature = "nightly"))] discriminant: variant.discriminant.as_ref().map(|(_, expr)| expr), }) } @@ -327,15 +337,12 @@ impl<'a> Data<'a> { type_: VariantType::Unit(pattern), .. } => SimpleType::Unit(pattern), - DataType::Union(fields) => SimpleType::Union(fields), + DataType::Union(_) => SimpleType::Union, } } /// Returns an [`Iterator`] over [`Field`]s. - pub fn iter_fields( - &self, - trait_: Trait, - ) -> impl '_ + Iterator + DoubleEndedIterator { + pub fn iter_fields(&self, trait_: Trait) -> impl '_ + DoubleEndedIterator { if self.skip(trait_) { [].iter() } else { @@ -354,19 +361,13 @@ impl<'a> Data<'a> { /// Returns an [`Iterator`] over [`struct@Ident`]s used as temporary /// variables for destructuring `self`. - pub fn iter_self_ident( - &self, - trait_: Trait, - ) -> impl Iterator + DoubleEndedIterator { + pub fn iter_self_ident(&self, trait_: Trait) -> impl DoubleEndedIterator { self.iter_fields(trait_).map(|field| &field.self_ident) } /// Returns an [`Iterator`] over [`struct@Ident`]s used as temporary /// variables for destructuring `other`. - pub fn iter_other_ident( - &self, - trait_: Trait, - ) -> impl Iterator + DoubleEndedIterator { + pub fn iter_other_ident(&self, trait_: Trait) -> impl DoubleEndedIterator { self.iter_fields(trait_).map(|field| &field.other_ident) } } diff --git a/src/error.rs b/src/error.rs index a476e40..9557ac5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -213,12 +213,14 @@ impl Error { } /// Unknown `repr`. + #[cfg(not(feature = "nightly"))] pub fn repr_unknown(span: Span) -> syn::Error { syn::Error::new(span, "found unknown representation") } /// Invalid enum with non-empty variants and custom discriminants without an /// integer representation. + #[cfg(not(feature = "nightly"))] pub fn repr_discriminant_invalid(span: Span) -> syn::Error { syn::Error::new( span, diff --git a/src/input.rs b/src/input.rs index 040fc61..55a52fe 100644 --- a/src/input.rs +++ b/src/input.rs @@ -5,7 +5,9 @@ use syn::{DeriveInput, GenericParam, Generics, ImplGenerics, Result, TypeGeneric #[cfg(feature = "zeroize")] use crate::DeriveTrait; -use crate::{Data, DeriveWhere, Discriminant, Either, Error, Item, ItemAttr, Trait}; +#[cfg(not(feature = "nightly"))] +use crate::Discriminant; +use crate::{Data, DeriveWhere, Either, Error, Item, ItemAttr, Trait}; /// Parsed input. pub struct Input<'a> { @@ -51,6 +53,7 @@ impl<'a> Input<'a> { ) .map(Item::Item)?, syn::Data::Enum(data) => { + #[cfg(not(feature = "nightly"))] let discriminant = Discriminant::parse(attrs, &data.variants)?; let variants = data @@ -99,6 +102,7 @@ impl<'a> Input<'a> { } Item::Enum { + #[cfg(not(feature = "nightly"))] discriminant, ident, variants, diff --git a/src/item.rs b/src/item.rs index e8587ca..e94f82f 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,10 +1,16 @@ //! Intermediate representation of item data. -use proc_macro2::{Ident, Span, TokenStream, TokenTree}; -use quote::ToTokens; -use syn::{punctuated::Punctuated, spanned::Spanned, Attribute, Meta, Result, Token, Variant}; +use proc_macro2::Ident; +#[cfg(not(feature = "nightly"))] +use { + proc_macro2::{Span, TokenStream, TokenTree}, + quote::ToTokens, + syn::{punctuated::Punctuated, spanned::Spanned, Attribute, Meta, Result, Token, Variant}, +}; -use crate::{Data, Error, Incomparable, Trait}; +#[cfg(not(feature = "nightly"))] +use crate::Error; +use crate::{Data, Incomparable, Trait}; /// Fields or variants of an item. #[cfg_attr(test, derive(Debug))] @@ -12,6 +18,7 @@ use crate::{Data, Error, Incomparable, Trait}; pub enum Item<'a> { /// Enum. Enum { + #[cfg(not(feature = "nightly"))] /// Type of discriminant used. discriminant: Discriminant, /// [`struct@Ident`] of this enum. @@ -96,6 +103,7 @@ impl Item<'_> { /// Type of discriminant used. #[derive(Clone, Copy)] #[cfg_attr(test, derive(Debug))] +#[cfg(not(feature = "nightly"))] pub enum Discriminant { /// The enum has only a single variant. Single, @@ -109,6 +117,7 @@ pub enum Discriminant { DataRepr(Representation), } +#[cfg(not(feature = "nightly"))] impl Discriminant { /// Parse the representation of an item. pub fn parse(attrs: &[Attribute], variants: &Punctuated) -> Result { @@ -165,6 +174,7 @@ impl Discriminant { /// The type used to represent an enum. #[derive(Clone, Copy)] #[cfg_attr(test, derive(Debug))] +#[cfg(not(feature = "nightly"))] pub enum Representation { /// [`u8`]. U8, @@ -192,6 +202,7 @@ pub enum Representation { ISize, } +#[cfg(not(feature = "nightly"))] impl Representation { /// Parse an [`struct@Ident`] to a valid representation if it is. fn parse(ident: &Ident) -> Option { @@ -245,6 +256,7 @@ impl Representation { } } +#[cfg(not(feature = "nightly"))] impl ToTokens for Representation { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend(self.to_token()); diff --git a/src/lib.rs b/src/lib.rs index 3a3f744..5db30e3 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,7 +55,7 @@ //! //! ## Generic type bounds //! -//! Separated from the list of traits with a semi-colon, types to bind to can be +//! Separated from the list of traits with a semicolon, types to bind to can be //! specified. This example will restrict the implementation for `Example` to //! `T: Clone`: //! @@ -307,7 +307,7 @@ //! ## Supported items //! //! Structs, tuple structs, unions and enums are supported. Derive-where tries -//! it's best to discourage usage that could be covered by std's `derive`. For +//! its best to discourage usage that could be covered by std's `derive`. For //! example unit structs and enums only containing unit variants aren't //! supported. //! @@ -408,6 +408,8 @@ use util::MetaListExt; #[cfg(feature = "zeroize")] use self::attr::ZeroizeFqs; +#[cfg(not(feature = "nightly"))] +use self::item::Discriminant; use self::{ attr::{ Default, DeriveTrait, DeriveWhere, FieldAttr, Incomparable, ItemAttr, Skip, SkipGroup, @@ -416,7 +418,7 @@ use self::{ data::{Data, DataType, Field, SimpleType}, error::Error, input::Input, - item::{Discriminant, Item}, + item::Item, trait_::{Trait, TraitImpl}, util::Either, }; @@ -588,7 +590,7 @@ pub fn derive_where_actual(input: proc_macro::TokenStream) -> proc_macro::TokenS clean_item.span() }; - match { Input::from_input(span, &item) } { + match Input::from_input(span, &item) { Ok(Input { derive_wheres, generics, diff --git a/src/trait_/clone.rs b/src/trait_/clone.rs index 50ae6d1..dd08a33 100644 --- a/src/trait_/clone.rs +++ b/src/trait_/clone.rs @@ -123,7 +123,7 @@ impl TraitImpl for Clone { SimpleType::Unit(pattern) => { quote! { #pattern => #pattern, } } - SimpleType::Union(_) => TokenStream::new(), + SimpleType::Union => TokenStream::new(), } } } diff --git a/src/trait_/common_ord.rs b/src/trait_/common_ord.rs index 7f713a6..88ab11d 100644 --- a/src/trait_/common_ord.rs +++ b/src/trait_/common_ord.rs @@ -461,7 +461,7 @@ pub fn build_incomparable_pattern(variants: &[Data]) -> Option { .map(|variant @ Data { path, .. }| match variant.simple_type() { SimpleType::Struct(_) => quote!(#path{..}), SimpleType::Tuple(_) => quote!(#path(..)), - SimpleType::Union(_) => unreachable!("enum variants cannot be unions"), + SimpleType::Union => unreachable!("enum variants cannot be unions"), SimpleType::Unit(_) => quote!(#path), }) .peekable(); diff --git a/src/trait_/debug.rs b/src/trait_/debug.rs index b39a4dd..084637a 100644 --- a/src/trait_/debug.rs +++ b/src/trait_/debug.rs @@ -79,7 +79,7 @@ impl TraitImpl for Debug { SimpleType::Unit(_) => { quote! { #self_pattern => ::core::fmt::Formatter::write_str(__f, #debug_name), } } - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/default.rs b/src/trait_/default.rs index 09e72b7..af25617 100644 --- a/src/trait_/default.rs +++ b/src/trait_/default.rs @@ -60,7 +60,7 @@ impl TraitImpl for Default { SimpleType::Unit(_) => { quote! { #path } } - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } // Skip `Default` implementation if variant isn't marked with a `default` attribute. diff --git a/src/trait_/hash.rs b/src/trait_/hash.rs index f4d6e1c..5c130cf 100644 --- a/src/trait_/hash.rs +++ b/src/trait_/hash.rs @@ -69,7 +69,7 @@ impl TraitImpl for Hash { } } } - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/ord.rs b/src/trait_/ord.rs index e6f486b..17ae516 100644 --- a/src/trait_/ord.rs +++ b/src/trait_/ord.rs @@ -57,7 +57,7 @@ impl TraitImpl for Ord { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/partial_eq.rs b/src/trait_/partial_eq.rs index 9933146..b8917ad 100644 --- a/src/trait_/partial_eq.rs +++ b/src/trait_/partial_eq.rs @@ -127,7 +127,7 @@ impl TraitImpl for PartialEq { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/partial_ord.rs b/src/trait_/partial_ord.rs index 7314083..81e4849 100644 --- a/src/trait_/partial_ord.rs +++ b/src/trait_/partial_ord.rs @@ -69,7 +69,7 @@ impl TraitImpl for PartialOrd { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/zeroize.rs b/src/trait_/zeroize.rs index 3491847..dec02c3 100644 --- a/src/trait_/zeroize.rs +++ b/src/trait_/zeroize.rs @@ -138,7 +138,7 @@ impl TraitImpl for Zeroize { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/zeroize_on_drop.rs b/src/trait_/zeroize_on_drop.rs index 8924c4a..d31b698 100644 --- a/src/trait_/zeroize_on_drop.rs +++ b/src/trait_/zeroize_on_drop.rs @@ -169,7 +169,7 @@ impl TraitImpl for ZeroizeOnDrop { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/tests/bound.rs b/tests/bound.rs index 8139822..fccbab6 100644 --- a/tests/bound.rs +++ b/tests/bound.rs @@ -1,3 +1,4 @@ +#![allow(unused)] mod util; use std::marker::PhantomData; diff --git a/tests/cfg.rs b/tests/cfg.rs index a94eda2..3146f88 100644 --- a/tests/cfg.rs +++ b/tests/cfg.rs @@ -1,3 +1,4 @@ +#![allow(unexpected_cfgs, unused)] use std::marker::PhantomData; use derive_where::derive_where; diff --git a/tests/hygiene.rs b/tests/hygiene.rs index 2789fcc..bde728f 100644 --- a/tests/hygiene.rs +++ b/tests/hygiene.rs @@ -1,4 +1,4 @@ -#![allow(clippy::clone_on_copy)] +#![allow(clippy::clone_on_copy, unused)] mod util; diff --git a/tests/incomparable.rs b/tests/incomparable.rs index 6a7624f..ae0ff84 100644 --- a/tests/incomparable.rs +++ b/tests/incomparable.rs @@ -1,3 +1,4 @@ +#![allow(unused)] use derive_where::derive_where; macro_rules! incomparable { diff --git a/tests/misc.rs b/tests/misc.rs index 64e361d..8119902 100644 --- a/tests/misc.rs +++ b/tests/misc.rs @@ -1,3 +1,4 @@ +#![allow(unused)] use std::marker::PhantomData; use derive_where::derive_where; diff --git a/tests/raw.rs b/tests/raw.rs index 938db24..c069d15 100644 --- a/tests/raw.rs +++ b/tests/raw.rs @@ -1,3 +1,4 @@ +#![allow(unused)] mod util; use derive_where::derive_where; diff --git a/tests/safety.rs b/tests/safety.rs index a938a25..952cd87 100644 --- a/tests/safety.rs +++ b/tests/safety.rs @@ -10,7 +10,13 @@ fn discriminant() { assert_eq!(size_of::(), size_of_val(&discriminant(&Test::A))); - assert_eq!(0_isize, unsafe { transmute(discriminant(&Test::A)) }); - assert_eq!(1_isize, unsafe { transmute(discriminant(&Test::B)) }); - assert_eq!(2_isize, unsafe { transmute(discriminant(&Test::C)) }); + assert_eq!(0_isize, unsafe { + transmute::, isize>(discriminant(&Test::A)) + }); + assert_eq!(1_isize, unsafe { + transmute::, isize>(discriminant(&Test::B)) + }); + assert_eq!(2_isize, unsafe { + transmute::, isize>(discriminant(&Test::C)) + }); } diff --git a/tests/skip/field_trait.rs b/tests/skip/field_trait.rs index 3d459c8..567b4bf 100644 --- a/tests/skip/field_trait.rs +++ b/tests/skip/field_trait.rs @@ -1,3 +1,4 @@ +#![allow(unused)] use std::cmp::Ordering; use derive_where::derive_where; diff --git a/tests/skip/struct_trait.rs b/tests/skip/struct_trait.rs index 772e7ed..e9df4a7 100644 --- a/tests/skip/struct_trait.rs +++ b/tests/skip/struct_trait.rs @@ -1,3 +1,4 @@ +#![allow(unused)] use std::cmp::Ordering; use derive_where::derive_where; diff --git a/tests/skip/variant_trait.rs b/tests/skip/variant_trait.rs index 0f66da0..83ab7c3 100644 --- a/tests/skip/variant_trait.rs +++ b/tests/skip/variant_trait.rs @@ -1,3 +1,4 @@ +#![allow(unused)] use std::cmp::Ordering; use derive_where::derive_where; diff --git a/tests/zeroize.rs b/tests/zeroize.rs index 9f304aa..55bde0e 100644 --- a/tests/zeroize.rs +++ b/tests/zeroize.rs @@ -1,4 +1,5 @@ #![cfg(feature = "zeroize")] +#![allow(unused)] extern crate zeroize_ as zeroize;