JelteF / derive_more

Some more derive(Trait) options
MIT License
1.73k stars 123 forks source link

Bug in `derive_more::Display` moving from `0.99.17` --> `0.99.18` with `fmt = stringify!` #373

Closed hulto closed 4 months ago

hulto commented 4 months ago

Problem

Recently ran into an issue with derive_more::Display and cargo's assumption of semver support. When building my project in CI cargo allowed the patch version for derive_more to creep forward one patch verison 0.99.17 --> 0.99.18. This resulted in the build failing when #[display(fmt = stringify!(Foo))] stopped supporting the standard libraries stringify!() macro.

Below is a minimal example and error to reproduce the error.

Working Example

cargo.toml

[package]
name = "derive_more_bug"
version = "0.1.0"
edition = "2021"

[dependencies]
derive_more = "=0.99.17"

main.rs

#[derive(derive_more::Display)]
#[display(fmt = stringify!(Foo))]
pub struct Foo;

fn main() {
    let test = Foo{};
    println!("{}", test)
}

Output:

$ cargo run
   Compiling derive_more_bug v0.1.0 (/Users/hulto/Downloads/derive_more_bug)
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
     Running `target/debug/derive_more_bug`
Foo

Failing Example

cargo.toml

[package]
name = "derive_more_bug"
version = "0.1.0"
edition = "2021"

[dependencies]
derive_more = "=0.99.18"

main.rs

#[derive(derive_more::Display)]
#[display(fmt = stringify!(Foo))]
pub struct Foo;

fn main() {
    let test = Foo{};
    println!("{}", test)
}

Error:

$ cargo run

   Compiling derive_more_bug v0.1.0 (/Users/hulto/Downloads/derive_more_bug)
error: Proper syntax: #[display(fmt = "My format", "arg1", "arg2")]
 --> src/main.rs:3:11
  |
3 | #[display(fmt = stringify!(Foo))]
  |           ^^^

error[E0277]: `Foo` doesn't implement `std::fmt::Display`
 --> src/main.rs:9:20
  |
9 |     println!("{}", test)
  |                    ^^^^ `Foo` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `Foo`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `derive_more_bug` (bin "derive_more_bug") due to 2 previous errors
JelteF commented 4 months ago

Hmm, that is kinda weird. I guess it's because of the update to syn 2.x Were you able to workaround the issue? I think probably changing the attribute to something like this would probably work:

#[display(fmt = "{}", stringify(Foo))]
hulto commented 4 months ago

I did I ended up hard pinning to the older version for now. That work around also seems to work though! Thanks 🙂

JelteF commented 4 months ago

I'm going wo be closing this. Even though it's a real issue, I don't think there's much I can do about this now. And there's multiple simple workarounds.