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

ParseError when implement Display derive for enum #239

Closed
qhliang opened this issue Feb 9, 2023 · 2 comments · Fixed by #377
Closed

ParseError when implement Display derive for enum #239

qhliang opened this issue Feb 9, 2023 · 2 comments · Fixed by #377
Assignees
Labels
Milestone

Comments

@qhliang
Copy link

qhliang commented Feb 9, 2023

Awesome library, i like it!

I found some small problems during use, in some cases, it will be panic. The following is an example:

#[derive(Display)]
#[display(fmt = "{_0}")]
enum NodeType {
    Base(&'static str),
    Main(&'static str),
    N1(&'static str),
    N2(&'static str),
    N3(&'static str),
    N4(&'static str),
}

Here is the compile error result:

error: proc-macro derive panicked
  --> src/main.rs:93:10
   |
93 | #[derive(Display)]
   |          ^^^^^^^
   |
   = help: message: called `Result::unwrap()` on an `Err` value: ParseError { line: 1, column: 2, offset: 1, expected: {"}", "[0-9]", ":"} }

I can't find the cause of the problem from the message.

@qhliang qhliang changed the title ParseError when implement Display derive for struct ParseError when implement Display derive for enum Feb 9, 2023
@tyranron
Copy link
Collaborator

tyranron commented Feb 9, 2023

@ilslv I guess this is not the case anymore on latest master, but maybe you could add tests covering that, just to be sure?

@tyranron tyranron added the bug label Feb 9, 2023
@tyranron tyranron added this to the 1.0.0 milestone Feb 9, 2023
@ilslv
Copy link
Contributor

ilslv commented Feb 10, 2023

@tyranron actually this case doesn't work on master, but for a different reason: we haven't decided yet on how to implement affix #142

tyranron added a commit that referenced this issue Jul 25, 2024
…142, #239)

Resolves #142, #239

## Synopsis

This adds back support for top-level format strings of the Display
derive. It
now includes the display of the variant whenever the `_variant`
placeholder
appears be found. It also supports using the field.

## Solution

This does not include the same support for Debug, since it is considered
much less
useful there and the differences between the Debug implementation and
Display
implementation make it a non-trivial port.

Only named arguments are supported in the format string, no positional
ones.
This made the implementation easier, maybe in a future PR positional
support
can be added.

This bumps MSRV to 1.70.0, on earlier versions the derived code throws
the following error:
```rust
error: there is no argument named `_0`
    --> tests/display.rs:1373:26
     |
1373 |                 #[derive(Display)]
     |                          ^^^^^^^
     |
     = note: did you intend to capture a variable `_0` from the surrounding scope?
     = note: to avoid ambiguity, `format_args!` cannot capture variables when the format string is expanded from a macro
     = note: this error originates in the derive macro `Display` (in Nightly builds, run with -Z macro-backtrace for more info)
 ```

Co-authored-by: Kai Ren <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants