chumsky icon indicating copy to clipboard operation
chumsky copied to clipboard

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

Open yonkeltron opened this issue 1 year ago • 4 comments

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

yonkeltron avatar May 09 '24 10:05 yonkeltron

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.

zesterer avatar May 10 '24 08:05 zesterer

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.

hashedone avatar Jun 02 '24 21:06 hashedone

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.

yonkeltron avatar Jun 03 '24 00:06 yonkeltron