Skip to content

E0277 focuses on the wrong part of the line. #100560

Closed
@Lokathor

Description

@Lokathor

So i've got some code like this:

#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct ProgramAttr(Vec<DmgToken>);
impl ProgramAttr {
  fn parser() -> impl Parser<DmgToken, ProgramAttr, Error = Simple<DmgToken>> {
    just(Punctuation('#')).ignore_then(just(Punctuation('!'))).ignore_then(
      filter(|t: &DmgToken| *t != Punctuation(']'))
        .repeated()
        .collect::<Vec<_>>()
        .delimited_by(Punctuation('['), Punctuation(']'))
        .map(ProgramAttr),
    )
  }
}

What is wrong: The delimited_by takes a parser for what occurs immediately before and immediately after the parser you call the method on. So it has a : Parser<I, O> trait bound. Except that Punctuation('[') is a token, not a parser for a token. What I should have written is just(Punctuation('[')).

What Rustc Tells You:

D:\dev\dmgrs>cargo build
   Compiling dmgrs v0.1.0 (D:\dev\dmgrs)
error[E0277]: the trait bound `DmgToken: Parser<DmgToken, _>` is not satisfied
   --> src\parser.rs:19:10
    |
19  |         .delimited_by(Punctuation('['), Punctuation(']'))
    |          ^^^^^^^^^^^^ the trait `Parser<DmgToken, _>` is not implemented for `DmgToken`
    |
    = help: the following other types implement trait `Parser<I, O>`:
              <&'a T as Parser<I, O>>
              <Arc<T> as Parser<I, O>>
              <Box<T> as Parser<I, O>>
              <BoxedParser<'a, I, O, E> as Parser<I, O>>
              <Choice<(A_, B_, C_, D_, E_, F_, G_, H_, I_, J_, K_, L_, M_, N_, O_, P_, Q_, S_, T_, U_, V_, W_, X_, Y_, Z_), E> as Parser<I, O>>
              <Choice<(B_, C_, D_, E_, F_, G_, H_, I_, J_, K_, L_, M_, N_, O_, P_, Q_, S_, T_, U_, V_, W_, X_, Y_, Z_), E> as Parser<I, O>>
              <Choice<(C_, D_, E_, F_, G_, H_, I_, J_, K_, L_, M_, N_, O_, P_, Q_, S_, T_, U_, V_, W_, X_, Y_, Z_), E> as Parser<I, O>>
              <Choice<(D_, E_, F_, G_, H_, I_, J_, K_, L_, M_, N_, O_, P_, Q_, S_, T_, U_, V_, W_, X_, Y_, Z_), E> as Parser<I, O>>
            and 60 others
note: required by a bound in `delimited_by`
   --> C:\Users\Daniel\.cargo\git\checkouts\chumsky-651fb76526ac39e3\a0a67e4\src\lib.rs:871:12
    |
871 |         L: Parser<I, U, Error = Self::Error>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `delimited_by`

The Problem: The part that's "wrong" in the line is the argument, not the method call. The way the error describes things, it makes it sound like the method doesn't exist because the thing i'm calling it on doesn't satisfy some trait bound or something like that. Which, if this were nearly any other language, would sound like a silly thing to worry about, but with rust that's a genuine concern.

How I Think Rustc Should Show The Error:
Instead of focusing on delimited_by as having the error, it should focus on the argument to delimited_by as having the error.

error[E0277]: the trait bound `DmgToken: Parser<DmgToken, _>` is not satisfied
   --> src\parser.rs:19:10
    |
19  |         .delimited_by(Punctuation('['), Punctuation(']'))
    |                       ^^^^^^^^^^^^^^^^ the trait `Parser<DmgToken, _>` is not implemented for `DmgToken`
    |

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions