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

Expansion error in pest-derive with tags #899

Closed
veeenu opened this issue Jul 23, 2023 · 2 comments · Fixed by #900
Closed

Expansion error in pest-derive with tags #899

veeenu opened this issue Jul 23, 2023 · 2 comments · Fixed by #900
Labels

Comments

@veeenu
Copy link
Contributor

veeenu commented Jul 23, 2023

Describe the bug
On Rust 1.70 and 1.71, when using tags, the derive macro errors out with the following:

error[E0433]: failed to resolve: use of undeclared crate or module `alloc`
 --> src/lib.rs:3:10
  |
3 | #[derive(Parser)]
  |          ^^^^^^ use of undeclared crate or module `alloc`
  |
  = help: add `extern crate alloc` to use the `alloc` crate
  = help: consider importing this enum:
          std::borrow::Cow
  = note: this error originates in the derive macro `Parser` (in Nightly builds, run with -Z macro-backtrace for more info)

To Reproduce
Here is a minimum reproducible example:

# Cargo.toml
[package]
name = "pest-issue"
version = "0.1.0"
edition = "2021"

[dependencies]
pest = "2.7.1"
pest_derive = { version = "2.7.1", features = ["std", "grammar-extras"] }
program = { SOI ~ rule* ~ EOI }
rule = !{ "{" ~ (item ~ ";")* ~ (#tail_item = item)? ~ "}" }
item = _{ "foo" | "bar" }
// lib.rs
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "grammar.pest"]
struct ExampleParser;

Expected behavior
The generated code should not emit the error.

Additional context

  • Adding extern crate alloc at the top of lib.rs makes the error go away -- it is a feasible workaround, but it should probably not be asked of the users to do that.
  • This is the only snippet that appears to reference alloc in the expanded code:
    state
        .optional(|state| {
            self::item(state)
                .and_then(|state| {
                    state.tag_node(alloc::borrow::Cow::Borrowed("tail_item"))
                })
        })
  • I found this. I haven't had time to try it myself yet, but could prepending :: so that we have ::alloc be a potential solution?

Thank you for all your work on this library!

@veeenu veeenu added the bug label Jul 23, 2023
@tomtau
Copy link
Contributor

tomtau commented Jul 23, 2023

haven't had time to try it myself yet, but could prepending :: so that we have ::alloc be a potential solution?

Possibly, that will resolve it from the crate root, so that should normally work with default features?

@veeenu
Copy link
Contributor Author

veeenu commented Jul 23, 2023

Prepending the :: did not work. However, changing the Cow path conditional on the std feature flag being active works. HTH!

tomtau pushed a commit that referenced this issue Jul 24, 2023
The generator currently wraps tags in an instance of alloc::borrow::Cow. This PR changes that to std::borrow::Cow conditional on the std feature flag being active.

Closes #899.
flyingsilverfin pushed a commit to typedb/typedb-dependencies that referenced this issue Oct 2, 2023
## What is the goal of this PR?

We update to pest and pest-derive v2.7.4, which among other things
purports to fix the error where [deriving Parser fails on "undeclared
crate or module
`alloc`"](pest-parser/pest#899)
(pest-parser/pest#900).
flyingsilverfin pushed a commit to typedb/typeql that referenced this issue Oct 2, 2023
## What is the goal of this PR?

We update to pest and pest-derive v2.7.4, which among other things purports to fix the error where [deriving Parser fails on "undeclared crate or module `alloc`"](pest-parser/pest#899) (pest-parser/pest#900).

## What are the changes implemented in this PR?

We also clean up some compile warnings (unused imports and hashes).
flyingsilverfin pushed a commit to typedb/typedb-driver that referenced this issue Oct 2, 2023
## What is the goal of this PR?

We update to pest and pest-derive v2.7.4, which among other things purports to fix the error where [deriving Parser fails on "undeclared crate or module `alloc`"](pest-parser/pest#899) (pest-parser/pest#900).

## What are the changes implemented in this PR?

We also update README URLs and project descriptions.
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.

2 participants