Skip to content

dispatch! unable to handle cfg attributes #609

@zeenix

Description

@zeenix

Please complete the following tasks

rust version

1.81.0

winnow version

0.6.13

Minimal reproducible code

fn main() {
    use winnow::combinator::{dispatch, empty, fail, preceded};
    use winnow::prelude::*;
    use winnow::token::any;

    fn escaped(input: &mut &str) -> PResult<char> {
        preceded('\\', escape_seq_char).parse_next(input)
    }

    fn escape_seq_char(input: &mut &str) -> PResult<char> {
        dispatch! {any;
            'b' => empty.value('\u{8}'),
            #[cfg(unix)]
            '"' => empty.value('"'),
            _ => fail::<_, char, _>,
        }
        .parse_next(input)
    }

    assert_eq!(escaped.parse_peek("\\b\""), Ok(("\"", '\u{8}')));
}

Steps to reproduce the bug with the above code

cargo run

Actual Behaviour

   Compiling tmp-unpavh v0.1.0 (/home/zeenix/.cache/cargo-temp/tmp-UNpAvH)
error: no rules expected the token `#`
  --> src/main.rs:13:13
   |
13 |             #[cfg(unix)]
   |             ^ no rules expected this token in macro call
   |
note: while trying to match meta-variable `$pat:pat`
  --> /home/zeenix/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winnow-0.6.20/src/macros/dispatch.rs:43:30
   |
43 |     ($match_parser: expr; $( $pat:pat $(if $pred:expr)? => $expr: expr ),+ $(,)? ) => {
   |                              ^^^^^^^^

warning: unused imports: `empty` and `fail`
 --> src/main.rs:2:40
  |
2 |     use winnow::combinator::{dispatch, empty, fail, preceded};
  |                                        ^^^^^  ^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `winnow::token::any`
 --> src/main.rs:4:9
  |
4 |     use winnow::token::any;
  |         ^^^^^^^^^^^^^^^^^^

warning: `tmp-unpavh` (bin "tmp-unpavh") generated 2 warnings
error: could not compile `tmp-unpavh` (bin "tmp-unpavh") due to 1 previous error; 2 warnings emitted

Expected Behaviour

Code should compile and run successfully (on unix systems).

Additional Context

@epage Hello from China. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-combinatorArea: combinatorsC-bugCategory: Things not working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions