Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work correctly with new Rust inline formatting #96

Open
RReverser opened this issue May 6, 2023 · 7 comments
Open

Doesn't work correctly with new Rust inline formatting #96

RReverser opened this issue May 6, 2023 · 7 comments
Labels
C-bug Category: Something isn't working

Comments

@RReverser
Copy link

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)*) => { ... };
}
@ten3roberts ten3roberts added the C-bug Category: Something isn't working label Aug 16, 2023
@nyurik
Copy link

nyurik commented Oct 18, 2023

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

@nyurik
Copy link

nyurik commented Oct 18, 2023

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!

@RReverser
Copy link
Author

Oh, it was? Where did you see that?

@nyurik
Copy link

nyurik commented Oct 18, 2023

@nyurik
Copy link

nyurik commented Oct 18, 2023

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)+)
    };
}

@RReverser
Copy link
Author

master/eyre/build.rs#L63-L69

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

@nyurik
Copy link

nyurik commented Oct 18, 2023

They are used

eyre/eyre/src/lib.rs

Lines 1231 to 1234 in 0b24ae5

#[cfg(eyre_no_fmt_arguments_as_str)]
let fmt_arguments_as_str: Option<&str> = None;
#[cfg(not(eyre_no_fmt_arguments_as_str))]
let fmt_arguments_as_str = args.as_str();
and in a test
#[cfg(not(eyre_no_fmt_args_capture))]
fn test_capture_format_args() {
maybe_install_handler().unwrap();
let var = 42;
let err = eyre!("interpolate {var}");
assert_eq!("interpolate 42", err.to_string());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants