eyre icon indicating copy to clipboard operation
eyre copied to clipboard

Doesn't work correctly with new Rust inline formatting

Open RReverser opened this issue 2 years ago • 7 comments

Nowadays Rust supports inline formatting for variables like format!("Expected {a} to be {b}").

This syntax works correctly in format!, format_args!, anyhow! and a lot of other contexts.

However, in eyre, somewhat surprisingly, it treats the string as a literal and prints without interpolation, even though eyre itself has support for formatting when arguments are passed explicitly. It seems this stems from all macros (ensure!, bail!, eyre!) having a separate branch that catches string literal with no following arguments in a special way:

macro_rules! eyre {
    ($msg:literal $(,)?) => { ... };
    ($err:expr $(,)?) => { ... };
    ($fmt:expr, $($arg:tt)*) => { ... };
}

RReverser avatar May 06 '23 15:05 RReverser

Seems like an easy fix - should i submit a PR?

nyurik avatar Oct 18 '23 04:10 nyurik

Ah, turns out its already been fixed, but has not been released yet. Thx for all the hard work updating it. Can't wait for the new release!

nyurik avatar Oct 18 '23 04:10 nyurik

Oh, it was? Where did you see that?

RReverser avatar Oct 18 '23 17:10 RReverser

https://github.com/eyre-rs/eyre/blob/master/eyre/build.rs#L63-L69

nyurik avatar Oct 18 '23 18:10 nyurik

TBH, the implementation seems a bit convoluted, but I might simply not know enough about it. Most implementations I have seen simply rely on format_args! like so:

macro_rules! foo {
    ($($arg:tt)+) => {
        format_args!($($arg)+)
    };
}

nyurik avatar Oct 18 '23 18:10 nyurik

master/eyre/build.rs#L63-L69

I don't understand how the referenced lines are related to feature in question.

RReverser avatar Oct 18 '23 21:10 RReverser

They are used https://github.com/eyre-rs/eyre/blob/0b24ae558f4779afccb1dfc4640c57d9922ff70e/eyre/src/lib.rs#L1231-L1234 and in a test https://github.com/eyre-rs/eyre/blob/0b24ae558f4779afccb1dfc4640c57d9922ff70e/eyre/tests/test_macros.rs#L78-L85

nyurik avatar Oct 18 '23 21:10 nyurik

Looks like this was released, so closing.

RReverser avatar Aug 26 '24 22:08 RReverser