Peternator7 / strum

A small rust library for adding custom derives to enums
https://crates.io/crates/strum
MIT License
1.78k stars 150 forks source link

strum-macros 0.26.3 breaks `no_std` build #359

Closed tronical closed 5 months ago

tronical commented 5 months ago

With 0.26.3, the emitted code when using derive(strum::Display) uses format! unconditionally. This requires the surrounding code to use format::alloc;, which seems like an unfortunate breaking change to me.

Example main.rs:

#![no_std]

#[derive(strum::Display)]
enum Foo {
    Bar
}

fn main() {
}

Sample Cargo.toml:

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

[dependencies]
strum = { version = "0.26.2", features = ["derive"] }
strum_macros = { version = "0.26.3" }

If you downgrade to =0.26.2 the build works, otherwise it aborts with:

error: cannot find macro `format` in this scope
 --> src/main.rs:3:10
  |
3 | #[derive(strum::Display)]
  |          ^^^^^^^^^^^^^^
  |
  = note: this error originates in the derive macro `strum::Display` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `strumtest` (bin "strumtest") due to 1 previous error

I think it's okay to require the surrounding no_std code to use alloc::format; when using strum, but it would be great if this requirement could come with a new major version (0.27) instead.

Peternator7 commented 5 months ago

This was unintentional. Thank you for pointing it out, I've put out a candidate fix that I'll merge later today if everything seems correct.

tronical commented 5 months ago

Thank you!

gmryuuko commented 5 months ago

This was unintentional. Thank you for pointing it out, I've put out a candidate fix that I'll merge later today if everything seems correct.

Another breaking change is that format! will affect serialize. I have an enum like this:

#[derive(Display)]
enum SyntaxKind {
    #[strum(serialize = "`{`")]
    LCurly,
}

Any serialize containing {} characters will be broken.

Peternator7 commented 5 months ago

@tronical, 0.26.4 is available that should fix your issue. @gmryuuko, in your case, you can mitigate by escaping the curly brace.

#[derive(Display)]
enum SyntaxKind {
    #[strum(serialize = "`{{`")]
    LCurly,
}
tronical commented 5 months ago

Thanks a lot!