From 5ffc2906bf58452166262bfa61f1751a179feefd Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 6 Feb 2024 12:50:12 -0800 Subject: [PATCH 1/2] Replace dummy-const with anon-const This has been supported since Rust 1.37, and the old way is about to start raising a warning from [RFC 3373][1]. [1]: https://rust-lang.github.io/rfcs/3373-avoid-nonlocal-definitions-in-fns.html --- src/lib.rs | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b6f7785..207602b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -97,16 +97,12 @@ macro_rules! parse { // we're deriving for a newtype, where the inner type is defined in the same module, but not // exported. // -// Solution: use the dummy const trick. For some reason, `extern crate` statements are allowed +// Solution: use the anonymous const trick. For some reason, `extern crate` statements are allowed // here, but everything from the surrounding module is in scope. This trick is taken from serde. -fn dummy_const_trick(trait_: &str, name: &Ident, exp: TokenStream2) -> TokenStream2 { - let dummy_const = Ident::new( - &format!("_IMPL_NUM_{}_FOR_{}", trait_, unraw(name)), - Span::call_site(), - ); +fn anon_const_trick(exp: TokenStream2) -> TokenStream2 { quote! { #[allow(non_upper_case_globals, unused_qualifications)] - const #dummy_const: () = { + const _: () = { #[allow(clippy::useless_attribute)] #[allow(rust_2018_idioms)] extern crate num_traits as _num_traits; @@ -115,10 +111,6 @@ fn dummy_const_trick(trait_: &str, name: &Ident, exp: TokenStream2) -> TokenStre } } -fn unraw(ident: &Ident) -> String { - ident.to_string().trim_start_matches("r#").to_owned() -} - // If `data` is a newtype, return the type it's wrapping. fn newtype_inner(data: &syn::Data) -> Option { match *data { @@ -189,11 +181,11 @@ impl NumTraits { } } - fn wrap(&self, trait_: &str, name: &Ident, output: TokenStream2) -> TokenStream2 { + fn wrap(&self, output: TokenStream2) -> TokenStream2 { if self.explicit { output } else { - dummy_const_trick(trait_, name, output) + anon_const_trick(output) } } } @@ -369,7 +361,7 @@ pub fn from_primitive(input: TokenStream) -> TokenStream { } }; - import.wrap("FromPrimitive", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::ToPrimitive`][to] for simple enums and newtypes. @@ -544,7 +536,7 @@ pub fn to_primitive(input: TokenStream) -> TokenStream { } }; - import.wrap("ToPrimitive", name, impl_).into() + import.wrap(impl_).into() } const NEWTYPE_ONLY: &str = "This trait can only be derived for newtypes"; @@ -623,7 +615,7 @@ pub fn num_cast(input: TokenStream) -> TokenStream { } }; - import.wrap("NumCast", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Zero`][zero] for newtypes. The inner type must already implement `Zero`. @@ -650,7 +642,7 @@ pub fn zero(input: TokenStream) -> TokenStream { } }; - import.wrap("Zero", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::One`][one] for newtypes. The inner type must already implement `One`. @@ -677,7 +669,7 @@ pub fn one(input: TokenStream) -> TokenStream { } }; - import.wrap("One", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Num`][num] for newtypes. The inner type must already implement `Num`. @@ -701,7 +693,7 @@ pub fn num(input: TokenStream) -> TokenStream { } }; - import.wrap("Num", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Float`][float] for newtypes. The inner type must already implement @@ -950,7 +942,7 @@ pub fn float(input: TokenStream) -> TokenStream { } }; - import.wrap("Float", name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Signed`][signed] for newtypes. The inner type must already implement @@ -990,7 +982,7 @@ pub fn signed(input: TokenStream) -> TokenStream { } }; - import.wrap("Signed", &name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Unsigned`][unsigned]. The inner type must already implement @@ -1008,5 +1000,5 @@ pub fn unsigned(input: TokenStream) -> TokenStream { impl #import::Unsigned for #name {} }; - import.wrap("Unsigned", &name, impl_).into() + import.wrap(impl_).into() } From b5b7ee907e23238966d2a7d8f8de93146a6c311f Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 6 Feb 2024 12:56:59 -0800 Subject: [PATCH 2/2] Release 0.4.2 --- Cargo.toml | 2 +- RELEASES.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index cfa35ab..40dcc07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ categories = [ "science" ] license = "MIT OR Apache-2.0" name = "num-derive" repository = "https://github.com/rust-num/num-derive" -version = "0.4.1" +version = "0.4.2" readme = "README.md" exclude = ["/bors.toml", "/ci/*", "/.github/*"] edition = "2021" diff --git a/RELEASES.md b/RELEASES.md index d0ae55d..bbf6d53 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,3 +1,9 @@ +# Release 0.4.2 (2024-02-06) + +- [Use anon-const to avoid RFC 3373 warnings.][62] + +[62]: https://github.com/rust-num/num-derive/pull/62 + # Release 0.4.1 (2023-10-07) - [Make `Float` work with `no_std`][56] -- thanks @vkahl!