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

no method named to_slice found for opaque type impl Parser<_, <_ as Character>::Collection, Error = _> + Copy + Clone in the current scope #634

Open
yonkeltron opened this issue May 9, 2024 · 4 comments

Comments

@yonkeltron
Copy link

Hello,

When attempting to compile the following code, I get a very confusing error.

use chumsky::prelude::*;

#[derive(Clone, Debug, PartialEq)]
pub enum LiteralValue {
  Zint(isize),
  Float(f64),
  Chars(String),
  Identifier(String),
  MannaPos(String),
  MannaNeg(String),
}

pub fn literal_value_parser() -> impl Parser<char, LiteralValue, Error = Simple<char>> {
  let zint = text::int(10)
    .to_slice()
    .from_str()
    .unwrapped()
    .map(LiteralValue::Zint);

  let float = text::int(10)
    .then(just('.').then(text::digits(10)))
    .to_slice()
    .from_str()
    .unwrapped()
    .map(LiteralValue::Float);

  let string = just('"')
    .ignore_then(none_of('"').repeated())
    .then_ignore(just('"'))
    .to_slice()
    .map(LiteralValue::Chars);

  let manna_pos = just("+M").to_slice().map(LiteralValue::MannaPos);
  let manna_neg = just("-M").to_slice().map(LiteralValue::MannaNeg);

  zint.or(float).or(string).or(manna_neg).or(manna_pos)
}

The error I get is:

error[E0599]: no method named `to_slice` found for opaque type `impl Parser<_, <_ as Character>::Collection, Error = _> + Copy + Clone` in the current scope
  --> src/parser/spell.rs:18:6
   |
17 |     let zint = text::int(10)
   |  ______________-
18 | |     .to_slice()
   | |     -^^^^^^^^ method not found in `impl Parser<_, <_ as Character>::Collection, Error = _> + Copy + Clone`
   | |_____|
   |

error[E0599]: no method named `to_slice` found for struct `Then` in the current scope
  --> src/parser/spell.rs:25:6
   |
23 |     let float = text::int(10)
   |  _______________-
24 | |     .then(just('.').then(text::digits(10)))
25 | |     .to_slice()
   | |     -^^^^^^^^ method not found in `Then<impl Parser<char, ..., Error = ...> + Copy + Clone, ...>`
   | |_____|
   |
   |
   = note: the full type name has been written to '/Users/yonkeltron/.cargo/shared_target/debug/deps/splang-ebcbc78b3a0326cb.long-type-13662306929836172744.txt'
   = note: consider using `--verbose` to print the full type name to the console

error[E0599]: no method named `to_slice` found for struct `Map` in the current scope
  --> src/parser/spell.rs:33:6
   |
30 |     let string = just('"')
   |  ________________-
31 | |     .ignore_then(none_of('"').repeated())
32 | |     .then_ignore(just('"'))
33 | |     .to_slice()
   | |     -^^^^^^^^ method not found in `Map<Then<Map<Then<Just<char, char, _>, ...>, ..., ...>, ...>, ..., ...>`
   | |_____|
   |
   |
   = note: the full type name has been written to '/Users/yonkeltron/.cargo/shared_target/debug/deps/splang-ebcbc78b3a0326cb.long-type-9340547084340958696.txt'
   = note: consider using `--verbose` to print the full type name to the console

error[E0599]: no method named `to_slice` found for struct `Just` in the current scope
  --> src/parser/spell.rs:36:30
   |
36 |   let manna_pos = just("+M").to_slice().map(LiteralValue::MannaPos);
   |                              ^^^^^^^^ method not found in `Just<_, &str, _>`

error[E0599]: no method named `to_slice` found for struct `Just` in the current scope
  --> src/parser/spell.rs:37:30
   |
37 |   let manna_neg = just("-M").to_slice().map(LiteralValue::MannaNeg);
   |                              ^^^^^^^^ method not found in `Just<_, &str, _>`

Honestly, I'm not sure how to interpret the error considering that it's unclear if I am writing incorrect parsing logic, or just annotating type signatures incorrectly.

Here is some of my system info:

❯ rustc --version                                                                           at 06:37:05
rustc 1.78.0 (9b00956e5 2024-04-29)

ats/splang on  main [?] is 📦 v0.1.0 via 🦀 v1.78.0
❯ cargo --version                                                                                 at 06:41:57
cargo 1.78.0 (54d8815d0 2024-03-26)

ats/splang on  main [?] is 📦 v0.1.0 via 🦀 v1.78.0
❯ cat Cargo.toml | grep chumsky                                                                   at 06:42:00
chumsky = "0.9.3"

ats/splang on  main [?] is 📦 v0.1.0 via 🦀 v1.78.0
❯ uname -a                                                                                        at 06:42:13
Darwin Qafih.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:19:22 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T8112 arm64

Please let me know if I can provide additional information.

Thanks,
+Jonathan

@zesterer
Copy link
Owner

zesterer commented May 10, 2024

I assume you're porting code from 0.9?

In 1.0, there have been a few changes. Firstly, a lifetime parameter has been added to Parser (the lifetime of the input being parsed).

Secondly, the first type parameter of Parser has changed from the 'token type' (i.e: char) to the underlying input type (in your case, &'a str) to allow for more complex manipulation of the underlying input, such as the ability to take slices of it (as you're doing!).

Lastly, we added a few extra type parameters to Parser (to support features like stateful parsing, context-sensitivity parsing, etc.). To make this a bit easier to handle, we squashed them all into a single type parameter which you can use like so:

Parser<I, O, extra::Err<MyError>>

So in net, the changes look like this:

pub fn literal_value_parser<'a>() -> impl Parser<'a, &'a str, LiteralValue, extra::Err<Simple<char>>> {
    ...
}

Hopefully this works for you.

@hashedone
Copy link

I would say it is more trivial - you are using chumsky 0.9.3 that seems to not have to_slice utility. You need to use the 1.x line for that.

@yonkeltron
Copy link
Author

I would say it is more trivial - you are using chumsky 0.9.3 that seems to not have to_slice utility. You need to use the 1.x line for that.

Ah sorry. I tried cargo add chumsky and guess it didn't go well. Will try to fix and report back.

@Zij-IT
Copy link
Contributor

Zij-IT commented Oct 23, 2024

@yonkeltron How did the fix go :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants