rust-num / num-derive

Procedural macros to derive numeric traits in Rust
Apache License 2.0
166 stars 24 forks source link

Future incompatibility with Rust RFC 3373: Avoid non-local definitions in functions #60

Closed Urgau closed 8 months ago

Urgau commented 8 months ago

Rust RFC 3373: Avoid non-local definitions in functions was accepted and it's implementation at https://github.com/rust-lang/rust/pull/120393 found that this crate would be affected by it.

To be more precise users of this crate would be affected by it, in the form of a warn-by-default lint. This is because the derive macros from this crate use impl in a local context, const _IMPL_NUM_???_FOR_???:

https://github.com/rust-num/num-derive/blob/50ecdb10ac0934eccd971c20bde9bee5fe99ed98/src/lib.rs#L102-L109

Fortunately a simple fix exist for this crate, by using a const-anon instead of named one:

 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(),
-    );
    quote! {
        #[allow(non_upper_case_globals, unused_qualifications)]
-        const #dummy_const: () = {
+        const _: () = {

I would suggest applying some form of the patch above as well as releasing a patch version of this crate, as to have a fix available for users as soon as possible.

cc @cuviper

cuviper commented 8 months ago

Ok, I'll get to this soon - thanks for the heads up!

cuviper commented 8 months ago

num-derive v0.4.2 should be fixed for this.

Urgau commented 8 months ago

Great, thks for fixing it @cuviper.