diff --git a/Cargo.lock b/Cargo.lock index 3a9b37aa5af0..3c27c51ac0db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4201,9 +4201,9 @@ dependencies = [ [[package]] name = "num-derive" -version = "0.4.0" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e6a0fd4f737c707bd9086cc16c925f294943eb62eb71499e9fd4cf71f8b9f4e" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" dependencies = [ "proc-macro2", "quote", diff --git a/supply-chain/audits.toml b/supply-chain/audits.toml index 2d645e99feb5..90f143292ec9 100644 --- a/supply-chain/audits.toml +++ b/supply-chain/audits.toml @@ -2916,6 +2916,11 @@ who = "Mike Hommey " criteria = "safe-to-deploy" delta = "0.3.3 -> 0.4.0" +[[audits.num-derive]] +who = "Mike Hommey " +criteria = "safe-to-deploy" +delta = "0.4.0 -> 0.4.2" + [[audits.num-integer]] who = "Josh Stone " criteria = "safe-to-deploy" diff --git a/third_party/rust/num-derive/.cargo-checksum.json b/third_party/rust/num-derive/.cargo-checksum.json index 22cbf847f39a..2defb822c159 100644 --- a/third_party/rust/num-derive/.cargo-checksum.json +++ b/third_party/rust/num-derive/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"f332f5a9fab58d78a8e2be8ec8db83fe47436ef9225e73361f4f6596cb325c6c","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"4087ceabb42281161bc074db61ad4d888343f159e60e49776559a179eda34193","RELEASES.md":"6270e75a054d3f14ae28f04abd27edb7ec41d67e131c4f1e798a3adb97aa4bc1","src/lib.rs":"724f81441d84672e684ca270cbfecddd741c8340a7e846ecf3cf6c9e9871349f","tests/empty_enum.rs":"1b2312ec2fc9866fce7172e71e0aa2efcc3cb9d7659d0b633eb352bb1e080d53","tests/issue-16.rs":"4a056b4a688805fc95f01642f016da6dadb46d1c67ceb82e5b5eaf9fe2db92cd","tests/issue-6.rs":"b7fbbe86ff82933c15ec09fabadca02d2c70e184af30b9887cae83ed2f60367e","tests/issue-9.rs":"4c87f70a63e56c24480028fafa39a845d127620141df21bc0abfa23f84ff9bb7","tests/newtype-2015.rs":"163f76f0c139c3621ba7416cec5f9fb966c86c5ebed270df77196cf75d62402f","tests/newtype-2018.rs":"ce5b005d805690b8200f6752d24a5ebe7d248703d0aa619489c6d821b3df4b9c","tests/newtype.rs":"bc1ff3fb3680935c2280cc91441c91ea104be24f525428987b49b86bc880217a","tests/num_derive_without_num.rs":"3ce528221a2cb752859e20c5423c4b474fec714b41d8c1b62f5614b165d7262b","tests/trivial-2015.rs":"1745e23dfafb8c717365e57d79323c61c7d4ceaa092d7399ed793fd4f2de8ff6","tests/trivial-2018.rs":"645f0ff3c3c0d014b6891bd310de2bc5f3ccb3019cb70d0b653061eb6571f7cf","tests/trivial.rs":"a6b0faab04527f6835f43cd72317a00065a7a6cf4c506d04e77f898134f7a59b","tests/with_custom_values.rs":"4f2ea85d1f987c6620efe5bda095477c3b669af3cb67f98935c5a7d59d28b339"},"package":"9e6a0fd4f737c707bd9086cc16c925f294943eb62eb71499e9fd4cf71f8b9f4e"} \ No newline at end of file +{"files":{"Cargo.toml":"659a3cf18a53cb7bcf017efaf2e498494e854f91ebb56c9acb067e94f076f43a","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"4087ceabb42281161bc074db61ad4d888343f159e60e49776559a179eda34193","RELEASES.md":"044558c5a745c96d6aecec2cd073a522d9919ba9873aa21b54c90c73e850accd","src/lib.rs":"dc254b4419136764ea0d5217e21cdbb8081142b6bc87a659c0d285573aebb181","tests/empty_enum.rs":"1b2312ec2fc9866fce7172e71e0aa2efcc3cb9d7659d0b633eb352bb1e080d53","tests/issue-16.rs":"4a056b4a688805fc95f01642f016da6dadb46d1c67ceb82e5b5eaf9fe2db92cd","tests/issue-6.rs":"b7fbbe86ff82933c15ec09fabadca02d2c70e184af30b9887cae83ed2f60367e","tests/issue-9.rs":"4c87f70a63e56c24480028fafa39a845d127620141df21bc0abfa23f84ff9bb7","tests/newtype-2015.rs":"163f76f0c139c3621ba7416cec5f9fb966c86c5ebed270df77196cf75d62402f","tests/newtype-2018.rs":"ce5b005d805690b8200f6752d24a5ebe7d248703d0aa619489c6d821b3df4b9c","tests/newtype.rs":"e7beacc831d911bae50424624a6d8823ef9eb6f7a0e2774b99be30c7977b927e","tests/no_implicit_prelude.rs":"7274f4fef6c35eb374eb2be2d352808f51deef06c02f819ae0e3d1521eebc610","tests/num_derive_without_num.rs":"3ce528221a2cb752859e20c5423c4b474fec714b41d8c1b62f5614b165d7262b","tests/trivial-2015.rs":"1745e23dfafb8c717365e57d79323c61c7d4ceaa092d7399ed793fd4f2de8ff6","tests/trivial-2018.rs":"645f0ff3c3c0d014b6891bd310de2bc5f3ccb3019cb70d0b653061eb6571f7cf","tests/trivial.rs":"a6b0faab04527f6835f43cd72317a00065a7a6cf4c506d04e77f898134f7a59b","tests/with_custom_values.rs":"4f2ea85d1f987c6620efe5bda095477c3b669af3cb67f98935c5a7d59d28b339"},"package":"ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202"} \ No newline at end of file diff --git a/third_party/rust/num-derive/Cargo.toml b/third_party/rust/num-derive/Cargo.toml index 6ff605a52ffc..36a8899f152c 100644 --- a/third_party/rust/num-derive/Cargo.toml +++ b/third_party/rust/num-derive/Cargo.toml @@ -13,10 +13,9 @@ edition = "2021" rust-version = "1.56.0" name = "num-derive" -version = "0.4.0" +version = "0.4.2" authors = ["The Rust Project Developers"] exclude = [ - "/bors.toml", "/ci/*", "/.github/*", ] diff --git a/third_party/rust/num-derive/RELEASES.md b/third_party/rust/num-derive/RELEASES.md index fbb34da48458..bbf6d533255d 100644 --- a/third_party/rust/num-derive/RELEASES.md +++ b/third_party/rust/num-derive/RELEASES.md @@ -1,3 +1,19 @@ +# 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! +- [Emit full paths for `Option` and `Result`.][57] +- [Add derive macro for `num_traits::Signed` and `Unsigned`][55] -- thanks @tdelabro! + +[55]: https://github.com/rust-num/num-derive/pull/55 +[56]: https://github.com/rust-num/num-derive/pull/56 +[57]: https://github.com/rust-num/num-derive/pull/57 + # Release 0.4.0 (2023-06-29) - [Update to syn-2][54] -- thanks @maurer! diff --git a/third_party/rust/num-derive/src/lib.rs b/third_party/rust/num-derive/src/lib.rs index 26b9c77c214b..207602b928fb 100644 --- a/third_party/rust/num-derive/src/lib.rs +++ b/third_party/rust/num-derive/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) } } } @@ -257,59 +249,59 @@ pub fn from_primitive(input: TokenStream) -> TokenStream { quote! { impl #import::FromPrimitive for #name { #[inline] - fn from_i64(n: i64) -> Option { + fn from_i64(n: i64) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_i64(n).map(#name) } #[inline] - fn from_u64(n: u64) -> Option { + fn from_u64(n: u64) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_u64(n).map(#name) } #[inline] - fn from_isize(n: isize) -> Option { + fn from_isize(n: isize) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_isize(n).map(#name) } #[inline] - fn from_i8(n: i8) -> Option { + fn from_i8(n: i8) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_i8(n).map(#name) } #[inline] - fn from_i16(n: i16) -> Option { + fn from_i16(n: i16) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_i16(n).map(#name) } #[inline] - fn from_i32(n: i32) -> Option { + fn from_i32(n: i32) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_i32(n).map(#name) } #[inline] - fn from_i128(n: i128) -> Option { + fn from_i128(n: i128) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_i128(n).map(#name) } #[inline] - fn from_usize(n: usize) -> Option { + fn from_usize(n: usize) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_usize(n).map(#name) } #[inline] - fn from_u8(n: u8) -> Option { + fn from_u8(n: u8) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_u8(n).map(#name) } #[inline] - fn from_u16(n: u16) -> Option { + fn from_u16(n: u16) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_u16(n).map(#name) } #[inline] - fn from_u32(n: u32) -> Option { + fn from_u32(n: u32) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_u32(n).map(#name) } #[inline] - fn from_u128(n: u128) -> Option { + fn from_u128(n: u128) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_u128(n).map(#name) } #[inline] - fn from_f32(n: f32) -> Option { + fn from_f32(n: f32) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_f32(n).map(#name) } #[inline] - fn from_f64(n: f64) -> Option { + fn from_f64(n: f64) -> ::core::option::Option { <#inner_ty as #import::FromPrimitive>::from_f64(n).map(#name) } } @@ -339,7 +331,7 @@ pub fn from_primitive(input: TokenStream) -> TokenStream { quote! { if #from_i64_var == #name::#ident as i64 { - Some(#name::#ident) + ::core::option::Option::Some(#name::#ident) } } }) @@ -355,21 +347,21 @@ pub fn from_primitive(input: TokenStream) -> TokenStream { impl #import::FromPrimitive for #name { #[allow(trivial_numeric_casts)] #[inline] - fn from_i64(#from_i64_var: i64) -> Option { + fn from_i64(#from_i64_var: i64) -> ::core::option::Option { #(#clauses else)* { - None + ::core::option::Option::None } } #[inline] - fn from_u64(n: u64) -> Option { + fn from_u64(n: u64) -> ::core::option::Option { Self::from_i64(n as i64) } } } }; - import.wrap("FromPrimitive", &name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::ToPrimitive`][to] for simple enums and newtypes. @@ -431,59 +423,59 @@ pub fn to_primitive(input: TokenStream) -> TokenStream { quote! { impl #import::ToPrimitive for #name { #[inline] - fn to_i64(&self) -> Option { + fn to_i64(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_i64(&self.0) } #[inline] - fn to_u64(&self) -> Option { + fn to_u64(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_u64(&self.0) } #[inline] - fn to_isize(&self) -> Option { + fn to_isize(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_isize(&self.0) } #[inline] - fn to_i8(&self) -> Option { + fn to_i8(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_i8(&self.0) } #[inline] - fn to_i16(&self) -> Option { + fn to_i16(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_i16(&self.0) } #[inline] - fn to_i32(&self) -> Option { + fn to_i32(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_i32(&self.0) } #[inline] - fn to_i128(&self) -> Option { + fn to_i128(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_i128(&self.0) } #[inline] - fn to_usize(&self) -> Option { + fn to_usize(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_usize(&self.0) } #[inline] - fn to_u8(&self) -> Option { + fn to_u8(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_u8(&self.0) } #[inline] - fn to_u16(&self) -> Option { + fn to_u16(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_u16(&self.0) } #[inline] - fn to_u32(&self) -> Option { + fn to_u32(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_u32(&self.0) } #[inline] - fn to_u128(&self) -> Option { + fn to_u128(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_u128(&self.0) } #[inline] - fn to_f32(&self) -> Option { + fn to_f32(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_f32(&self.0) } #[inline] - fn to_f64(&self) -> Option { + fn to_f64(&self) -> ::core::option::Option { <#inner_ty as #import::ToPrimitive>::to_f64(&self.0) } } @@ -522,7 +514,7 @@ pub fn to_primitive(input: TokenStream) -> TokenStream { } } else { quote! { - Some(match *self { + ::core::option::Option::Some(match *self { #(#variants,)* }) } @@ -532,19 +524,19 @@ pub fn to_primitive(input: TokenStream) -> TokenStream { impl #import::ToPrimitive for #name { #[inline] #[allow(trivial_numeric_casts)] - fn to_i64(&self) -> Option { + fn to_i64(&self) -> ::core::option::Option { #match_expr } #[inline] - fn to_u64(&self) -> Option { + fn to_u64(&self) -> ::core::option::Option { self.to_i64().map(|x| x as u64) } } } }; - import.wrap("ToPrimitive", &name, impl_).into() + import.wrap(impl_).into() } const NEWTYPE_ONLY: &str = "This trait can only be derived for newtypes"; @@ -617,13 +609,13 @@ pub fn num_cast(input: TokenStream) -> TokenStream { let impl_ = quote! { impl #import::NumCast for #name { #[inline] - fn from(n: T) -> Option { + fn from(n: T) -> ::core::option::Option { <#inner_ty as #import::NumCast>::from(n).map(#name) } } }; - 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`. @@ -695,13 +687,13 @@ pub fn num(input: TokenStream) -> TokenStream { impl #import::Num for #name { type FromStrRadixErr = <#inner_ty as #import::Num>::FromStrRadixErr; #[inline] - fn from_str_radix(s: &str, radix: u32) -> Result { + fn from_str_radix(s: &str, radix: u32) -> ::core::result::Result { <#inner_ty as #import::Num>::from_str_radix(s, radix).map(#name) } } }; - import.wrap("Num", &name, impl_).into() + import.wrap(impl_).into() } /// Derives [`num_traits::Float`][float] for newtypes. The inner type must already implement @@ -763,7 +755,7 @@ pub fn float(input: TokenStream) -> TokenStream { <#inner_ty as #import::Float>::is_normal(self.0) } #[inline] - fn classify(self) -> ::std::num::FpCategory { + fn classify(self) -> ::core::num::FpCategory { <#inner_ty as #import::Float>::classify(self.0) } #[inline] @@ -950,5 +942,63 @@ 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 +/// `Signed`. +/// +/// [signed]: https://docs.rs/num-traits/0.2/num_traits/sign/trait.Signed.html +#[proc_macro_derive(Signed, attributes(num_traits))] +pub fn signed(input: TokenStream) -> TokenStream { + let ast = parse!(input as syn::DeriveInput); + let name = &ast.ident; + let inner_ty = newtype_inner(&ast.data).expect(NEWTYPE_ONLY); + + let import = NumTraits::new(&ast); + + let impl_ = quote! { + impl #import::Signed for #name { + #[inline] + fn abs(&self) -> Self { + #name(<#inner_ty as #import::Signed>::abs(&self.0)) + } + #[inline] + fn abs_sub(&self, other: &Self) -> Self { + #name(<#inner_ty as #import::Signed>::abs_sub(&self.0, &other.0)) + } + #[inline] + fn signum(&self) -> Self { + #name(<#inner_ty as #import::Signed>::signum(&self.0)) + } + #[inline] + fn is_positive(&self) -> bool { + <#inner_ty as #import::Signed>::is_positive(&self.0) + } + #[inline] + fn is_negative(&self) -> bool { + <#inner_ty as #import::Signed>::is_negative(&self.0) + } + } + }; + + import.wrap(impl_).into() +} + +/// Derives [`num_traits::Unsigned`][unsigned]. The inner type must already implement +/// `Unsigned`. +/// +/// [unsigned]: https://docs.rs/num/latest/num/traits/trait.Unsigned.html +#[proc_macro_derive(Unsigned, attributes(num_traits))] +pub fn unsigned(input: TokenStream) -> TokenStream { + let ast = parse!(input as syn::DeriveInput); + let name = &ast.ident; + + let import = NumTraits::new(&ast); + + let impl_ = quote! { + impl #import::Unsigned for #name {} + }; + + import.wrap(impl_).into() } diff --git a/third_party/rust/num-derive/tests/newtype.rs b/third_party/rust/num-derive/tests/newtype.rs index 6eafd0960b3c..71b06b3932c3 100644 --- a/third_party/rust/num-derive/tests/newtype.rs +++ b/third_party/rust/num-derive/tests/newtype.rs @@ -2,9 +2,22 @@ extern crate num as num_renamed; #[macro_use] extern crate num_derive; -use crate::num_renamed::{Float, FromPrimitive, Num, NumCast, One, ToPrimitive, Zero}; +use crate::num_renamed::{ + Float, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Unsigned, Zero, +}; use std::ops::Neg; +#[derive(PartialEq, Zero, One, NumOps, Num, Unsigned)] +struct MyNum(u32); + +#[test] +fn test_derive_unsigned_works() { + fn do_nothing_on_unsigned(_input: impl Unsigned) {} + + let x = MyNum(42); + do_nothing_on_unsigned(x); +} + #[derive( Debug, Clone, @@ -19,6 +32,7 @@ use std::ops::Neg; Zero, Num, Float, + Signed, )] struct MyFloat(f64); @@ -87,3 +101,8 @@ fn test_num() { fn test_float() { assert_eq!(MyFloat(4.0).log(MyFloat(2.0)), MyFloat(2.0)); } + +#[test] +fn test_signed() { + assert!(MyFloat(-2.0).is_negative()) +} diff --git a/third_party/rust/num-derive/tests/no_implicit_prelude.rs b/third_party/rust/num-derive/tests/no_implicit_prelude.rs new file mode 100644 index 000000000000..cb82decb8e71 --- /dev/null +++ b/third_party/rust/num-derive/tests/no_implicit_prelude.rs @@ -0,0 +1,13 @@ +#![no_implicit_prelude] + +use ::num_derive::*; + +#[derive(FromPrimitive, ToPrimitive)] +enum Color { + Red, + Blue, + Green, +} + +#[derive(FromPrimitive, ToPrimitive, NumCast, PartialEq, Zero, One, NumOps, Num)] +struct NewI32(i32);