tree-sitter / tree-sitter-rust

Rust grammar for tree-sitter
MIT License
338 stars 96 forks source link

bug: Broken parsing for `where` keyword inside trait declaration #221

Closed Areskiko closed 3 months ago

Areskiko commented 4 months ago

Did you check existing issues?

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

No response

Describe the bug

The where keyword is not highlighted properly in this minimal example:

pub trait MyTrait<'a> {
    type Output where Self: 'a;
    fn function(&'a mut self) -> Self::Output;
}

Checking with tree-sitter parse shows an error node. However, running cargo check on an otherwise empty project shows no errors, building also works fine. Meaning that the grammar defined in tree-sitter-rust is wrong.

Here is the parsed tree with the error node:

(source_file [0, 0] - [4, 0]
  (trait_item [0, 0] - [3, 1]
    (visibility_modifier [0, 0] - [0, 3])
    name: (type_identifier [0, 10] - [0, 17])
    type_parameters: (type_parameters [0, 17] - [0, 21]
      (lifetime [0, 18] - [0, 20]
        (identifier [0, 19] - [0, 20])))
    body: (declaration_list [0, 22] - [3, 1]
      (associated_type [1, 4] - [1, 31]
        name: (type_identifier [1, 9] - [1, 15])
        (ERROR [1, 16] - [1, 26]
          (identifier [1, 16] - [1, 21])
          (identifier [1, 22] - [1, 26]))
        bounds: (trait_bounds [1, 26] - [1, 30]
          (lifetime [1, 28] - [1, 30]
            (identifier [1, 29] - [1, 30]))))
      (function_signature_item [2, 4] - [2, 46]
        name: (identifier [2, 7] - [2, 15])
        parameters: (parameters [2, 15] - [2, 29]
          (self_parameter [2, 16] - [2, 28]
            (lifetime [2, 17] - [2, 19]
              (identifier [2, 18] - [2, 19]))
            (mutable_specifier [2, 20] - [2, 23])
            (self [2, 24] - [2, 28])))
        return_type: (scoped_type_identifier [2, 33] - [2, 45]
          path: (identifier [2, 33] - [2, 37])
          name: (type_identifier [2, 39] - [2, 45]))))))

Steps To Reproduce/Bad Parse Tree

  1. Clone this repository
  2. Run tree-sitter generate
  3. Paste the following into a file named example/broken.rs:
    pub trait MyTrait<'a> {
    type Output where Self: 'a;
    fn function(&'a mut self) -> Self::Output;
    }
  4. Run tree-sitter parse example/broken.rs

Expected Behavior/Parse Tree

A complete, error-free, tree.

Repro

pub trait MyTrait<'a> {
    type Output where Self: 'a;
    fn function(&'a mut self) -> Self::Output;
}
amaanq commented 3 months ago

thanks, fixed