xref: /third_party/rust/crates/nom/doc/archive/FAQ.md (revision 6855e09e)
16855e09eSopenharmony_ci# FAQ
26855e09eSopenharmony_ci
36855e09eSopenharmony_ci### Using nightly to get better error messages
46855e09eSopenharmony_ci
56855e09eSopenharmony_ci**warning**: this only applies to nom 3. nom 4 uses the
66855e09eSopenharmony_ci[compile_error](https://doc.rust-lang.org/std/macro.compile_error.html) macro
76855e09eSopenharmony_ciavailable since Rust 1.20
86855e09eSopenharmony_ci
96855e09eSopenharmony_ciIf you got the following error when compiling your nom parser:
106855e09eSopenharmony_ci
116855e09eSopenharmony_ci```
126855e09eSopenharmony_cierror[E0425]: cannot find value `INVALID_NOM_SYNTAX_PLEASE_SEE_FAQ` in this scope
136855e09eSopenharmony_ci   --> src/lib.rs:111:7
146855e09eSopenharmony_ci    |
156855e09eSopenharmony_ci111 |         INVALID_NOM_SYNTAX_PLEASE_SEE_FAQ //https://github.com/Geal/nom/blob/main/doc/archive/FAQ.md#using-nightly-to-get-better-error-messages
166855e09eSopenharmony_ci    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
176855e09eSopenharmony_ci```
186855e09eSopenharmony_ci
196855e09eSopenharmony_ciIt means that you are using Rust stable, and that one of your nom parsers has an invalid syntax.
206855e09eSopenharmony_ciIf you can switch to a nightly Rust compiler (as an example, with `rustup default nightly`),
216855e09eSopenharmony_ciand if you activate the `nightly` feature on your nom dependency like this:
226855e09eSopenharmony_ci
236855e09eSopenharmony_ci```toml
246855e09eSopenharmony_ci[dependencies.nom]
256855e09eSopenharmony_civersion = "^3"
266855e09eSopenharmony_cifeatures = ["nightly"]
276855e09eSopenharmony_ci```
286855e09eSopenharmony_ci
296855e09eSopenharmony_ciYou can get more helpful error messages, such as this one:
306855e09eSopenharmony_ci
316855e09eSopenharmony_ci```
326855e09eSopenharmony_ci$ cargo test --features nightly
336855e09eSopenharmony_ci   Compiling compiler_error v0.1.1
346855e09eSopenharmony_ci   Compiling nom v3.0.0 (file:///Users/geal/dev/rust/projects/nom)
356855e09eSopenharmony_cierror: "do_parse is missing the return value. A do_parse call must end
366855e09eSopenharmony_ci      with a return value between parenthesis, as follows:
376855e09eSopenharmony_ci
386855e09eSopenharmony_ci      do_parse!(
396855e09eSopenharmony_ci        a: tag!(\"abcd\") >>
406855e09eSopenharmony_ci        b: tag!(\"efgh\") >>
416855e09eSopenharmony_ci
426855e09eSopenharmony_ci        ( Value { a: a, b: b } )
436855e09eSopenharmony_ci    "
446855e09eSopenharmony_ci   --> src/sequence.rs:368:5
456855e09eSopenharmony_ci    |
466855e09eSopenharmony_ci368 | /     compiler_error!("do_parse is missing the return value. A do_parse call must end
476855e09eSopenharmony_ci369 | |       with a return value between parenthesis, as follows:
486855e09eSopenharmony_ci370 | |
496855e09eSopenharmony_ci371 | |       do_parse!(
506855e09eSopenharmony_ci...   |
516855e09eSopenharmony_ci375 | |         ( Value { a: a, b: b } )
526855e09eSopenharmony_ci376 | |     ");
536855e09eSopenharmony_ci    | |______^
546855e09eSopenharmony_ci...
556855e09eSopenharmony_ci851 | /         named!(no_compiler,
566855e09eSopenharmony_ci852 | |                 do_parse!(
576855e09eSopenharmony_ci853 | |                         length: be_u8         >>
586855e09eSopenharmony_ci854 | |                         bytes:  take!(length)
596855e09eSopenharmony_ci855 | |                 )
606855e09eSopenharmony_ci856 | |         );
616855e09eSopenharmony_ci    | |___- in this macro invocation
626855e09eSopenharmony_ci
636855e09eSopenharmony_cierror: aborting due to previous error(s)
646855e09eSopenharmony_ci
656855e09eSopenharmony_cierror: Could not compile `nom`.
666855e09eSopenharmony_ci```
676855e09eSopenharmony_ci
686855e09eSopenharmony_ciIf the error message is not helpful, please reach out on the [Gitter chat](https://gitter.im/Geal/nom?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) or the IRC channel (#nom on freenode), and show
696855e09eSopenharmony_ciyour code and the error message you got.
706855e09eSopenharmony_ci
716855e09eSopenharmony_ci### nom 1.0 does not compile on Rust older than 1.4
726855e09eSopenharmony_ci
736855e09eSopenharmony_ciTypically, the error would look like this:
746855e09eSopenharmony_ci
756855e09eSopenharmony_ci```ignore
766855e09eSopenharmony_cisrc/stream.rs:74:44: 74:64 error: the parameter type `E` may not live long enough [E0309]
776855e09eSopenharmony_cisrc/stream.rs:74     if let &ConsumerState::Done(_,ref o) = self.apply(consumer) {
786855e09eSopenharmony_ci                                                            ^~~~~~~~~~~~~~~~~~~~
796855e09eSopenharmony_cinote: in expansion of if let expansion
806855e09eSopenharmony_cisrc/stream.rs:74:5: 78:6 note: expansion site
816855e09eSopenharmony_cisrc/stream.rs:74:44: 74:64 help: run `rustc --explain E0309` to see a detailed explanation
826855e09eSopenharmony_cisrc/stream.rs:74:44: 74:64 help: consider adding an explicit lifetime bound `E: 'b`...
836855e09eSopenharmony_cisrc/stream.rs:74:44: 74:64 note: ...so that the reference type `&stream::ConsumerState<O, E, M>` does not outlive the data it points at
846855e09eSopenharmony_cisrc/stream.rs:74     if let &ConsumerState::Done(_,ref o) = self.apply(consumer) {
856855e09eSopenharmony_ci                                                            ^~~~~~~~~~~~~~~~~~~~
866855e09eSopenharmony_cinote: in expansion of if let expansion
876855e09eSopenharmony_cisrc/stream.rs:74:5: 78:6 note: expansion site
886855e09eSopenharmony_cisrc/stream.rs:74:44: 74:64 error: the parameter type `M` may not live long enough [E0309]
896855e09eSopenharmony_cisrc/stream.rs:74     if let &ConsumerState::Done(_,ref o) = self.apply(consumer) {
906855e09eSopenharmony_ci                                                            ^~~~~~~~~~~~~~~~~~~~
916855e09eSopenharmony_cinote: in expansion of if let expansion
926855e09eSopenharmony_cisrc/stream.rs:74:5: 78:6 note: expansion site
936855e09eSopenharmony_cisrc/stream.rs:74:44: 74:64 help: run `rustc --explain E0309` to see a detailed explanation
946855e09eSopenharmony_cisrc/stream.rs:74:44: 74:64 help: consider adding an explicit lifetime bound `M: 'b`...
956855e09eSopenharmony_cisrc/stream.rs:74:44: 74:64 note: ...so that the reference type `&stream::ConsumerState<O, E, M>` does not outlive the data it points at
966855e09eSopenharmony_cisrc/stream.rs:74     if let &ConsumerState::Done(_,ref o) = self.apply(consumer) {
976855e09eSopenharmony_ci                                                            ^~~~~~~~~~~~~~~~~~~~
986855e09eSopenharmony_cinote: in expansion of if let expansion
996855e09eSopenharmony_cisrc/stream.rs:74:5: 78:6 note: expansion site
1006855e09eSopenharmony_cierror: aborting due to 2 previous errors
1016855e09eSopenharmony_ci
1026855e09eSopenharmony_ciCould not compile `nom`.
1036855e09eSopenharmony_ci```
1046855e09eSopenharmony_ci
1056855e09eSopenharmony_ciThis is caused by some lifetime issues that may be fixed in a future version of nom. In the meantime, you can add `default-features=false` to nom's declaration in `Cargo.toml` to deactivate this part of the code:
1066855e09eSopenharmony_ci
1076855e09eSopenharmony_ci```toml
1086855e09eSopenharmony_ci[dependencies.nom]
1096855e09eSopenharmony_civersion = "~1.0.0"
1106855e09eSopenharmony_cidefault-features = false
1116855e09eSopenharmony_ci```
1126855e09eSopenharmony_ci
1136855e09eSopenharmony_ci### The compiler indicates `error: expected an item keyword` then points to the function's return type in `named!`:
1146855e09eSopenharmony_ci
1156855e09eSopenharmony_ci```ignore
1166855e09eSopenharmony_cierror: expected an item keyword
1176855e09eSopenharmony_cinamed!(multi<Vec<&str>>, many0!( map_res!(tag!( "abcd" ), str::from_utf8) ) );
1186855e09eSopenharmony_ci             ^~~
1196855e09eSopenharmony_ci```
1206855e09eSopenharmony_ci
1216855e09eSopenharmony_ciThis happens because the macro processor mistakes `>>` for an operator. It will work correctly by adding a space, like this: `named!(multi< Vec<&str> >, ...`
122