paste icon indicating copy to clipboard operation
paste copied to clipboard

Cannot generate f32/f64 values

Open mamekoro opened this issue 2 years ago • 0 comments

paste: 1.0.12 rustc: 1.68.2

When I try to concatenate a minus sign (-), a float literal (e.g. 0.0), and a type suffix (e.g. f32 or f64), rustc reports errors. Here is a reproduction code:

use paste::paste;

fn main() {
    paste! {
        let _ = [< 0.0 f32 >]; // rustc:Error:unsupported literal
        let _ = [< -0.0 f32 >]; // rustc:Error:unexpected punct
    }
}

In my actual project, I used the paste! macro with the duplicate! macro for unit tests using f32/f64 values like this:

use duplicate::duplicate;
use paste::paste;

duplicate! {
    [ float; [f32]; [f64]; ]
    // In this code block, the word `float` is replaced with `f32` and `f64`.
    paste! {
        #[test]
        fn [< ulp_should_return_EPSILON_when_self_is_1_ float >]() {
            // Here is the problematic code `[< 1.0 float >]`.
            assert_eq!([< 1.0 float >].ulp(), float::EPSILON);
        }

        // Many other test functions using f32 and f64 are omitted.
        // ...
    }
}

I expected the following code to be generated:

#[test]
fn ulp_should_return_EPSILON_when_self_is_1_f32() {
    assert_eq!(1.0f32.ulp(), f32::EPSILON);
}

#[test]
fn ulp_should_return_EPSILON_when_self_is_1_f64() {
    assert_eq!(1.0f64.ulp(), f64::EPSILON);
}

The duplicate! macro is not the cause of the compilation error, as shown in the reproduction code at the top of this post.

Please make the paste! macro able to concatenate signs, floating point numbers, and type suffixes 🙏

mamekoro avatar Apr 17 '23 14:04 mamekoro