tree-sitter / tree-sitter-rust

Rust grammar for tree-sitter
MIT License
350 stars 98 forks source link

bug: Error in (seemingly?) correct Rust file #230

Closed HiPhish closed 3 months ago

HiPhish commented 3 months ago

Did you check existing issues?

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

No response

Describe the bug

Neovim user and maintainer of HiPhish/rainbow-delimiters.nvim here. I have a Rust file and Neovim reports four errors.

I don't know much about Rust, so maybe the code is invalid? The code itself does not make much sense, it's just a test file for trying out the rainbow delimiter highlighting. See also this PR and this issue:

Steps To Reproduce/Bad Parse Tree

The offending source code is

fn main() {
    let nested_macro = vec![vec![vec![vec![vec![0]]]]];
}

The resulting tree is

(source_file ; [0, 0] - [3, 0]
  (function_item ; [0, 0] - [2, 1]
    name: (identifier) ; [0, 3] - [0, 7]
    parameters: (parameters) ; [0, 7] - [0, 9]
    body: (block ; [0, 10] - [2, 1]
      (let_declaration ; [1, 4] - [1, 55]
        pattern: (identifier) ; [1, 8] - [1, 20]
        value: (macro_invocation ; [1, 23] - [1, 54]
          macro: (identifier) ; [1, 23] - [1, 26]
          (token_tree ; [1, 27] - [1, 54]
            (source_file ; [1, 27] - [1, 54]
              (expression_statement ; [1, 27] - [1, 54]
                (array_expression ; [1, 27] - [1, 54]
                  (macro_invocation ; [1, 28] - [1, 53]
                    macro: (identifier) ; [1, 28] - [1, 31]
                    (token_tree ; [1, 32] - [1, 53]
                      (source_file ; [1, 32] - [1, 53]
                        (expression_statement ; [1, 32] - [1, 53]
                          (array_expression ; [1, 32] - [1, 53]
                            (macro_invocation ; [1, 33] - [1, 52]
                              macro: (identifier) ; [1, 33] - [1, 36]
                              (token_tree ; [1, 37] - [1, 52]
                                (source_file ; [1, 37] - [1, 52]
                                  (expression_statement ; [1, 37] - [1, 52]
                                    (array_expression ; [1, 37] - [1, 52]
                                      (macro_invocation ; [1, 38] - [1, 51]
                                        macro: (identifier) ; [1, 38] - [1, 41]
                                        (token_tree ; [1, 42] - [1, 51]
                                          (source_file ; [1, 42] - [1, 51]
                                            (ERROR ; [1, 42] - [1, 51]
                                              (array_expression ; [1, 42] - [1, 51]
                                                (macro_invocation ; [1, 43] - [1, 50]
                                                  macro: (identifier) ; [1, 43] - [1, 46]
                                                  (token_tree ; [1, 47] - [1, 50]
                                                    (source_file ; [1, 47] - [1, 50]
                                                      (ERROR ; [1, 47] - [1, 50]
                                                        (array_expression ; [1, 47] - [1, 50]
                                                          (integer_literal)))) ; [1, 48] - [1, 49]
                                                    (integer_literal)))))) ; [1, 48] - [1, 49]
                                          (identifier) ; [1, 43] - [1, 46]
                                          (token_tree ; [1, 47] - [1, 50]
                                            (integer_literal))))))) ; [1, 48] - [1, 49]
                                (identifier) ; [1, 38] - [1, 41]
                                (token_tree ; [1, 42] - [1, 51]
                                  (identifier) ; [1, 43] - [1, 46]
                                  (token_tree ; [1, 47] - [1, 50]
                                    (integer_literal)))))))) ; [1, 48] - [1, 49]
                      (identifier) ; [1, 33] - [1, 36]
                      (token_tree ; [1, 37] - [1, 52]
                        (identifier) ; [1, 38] - [1, 41]
                        (token_tree ; [1, 42] - [1, 51]
                          (identifier) ; [1, 43] - [1, 46]
                          (token_tree ; [1, 47] - [1, 50]
                            (integer_literal))))))))) ; [1, 48] - [1, 49]
            (identifier) ; [1, 28] - [1, 31]
            (token_tree ; [1, 32] - [1, 53]
              (identifier) ; [1, 33] - [1, 36]
              (token_tree ; [1, 37] - [1, 52]
                (identifier) ; [1, 38] - [1, 41]
                (token_tree ; [1, 42] - [1, 51]
                  (identifier) ; [1, 43] - [1, 46]
                  (token_tree ; [1, 47] - [1, 50]
                    (integer_literal))))))))))) ; [1, 48] - [1, 49]

Expected Behavior/Parse Tree

There should be no errors in the tree.

Repro

No response

amaanq commented 3 months ago

parsing works correctly using the tree-sitter cli. this is an issue with injections (rust inside rust), and does not belong in this repo.

HiPhish commented 3 months ago

Fair enough, but where does it belong then? Neovim?

amaanq commented 3 months ago

probably nvim-treesitter, though it's likely unsolvable you either lose macro injections or deal with spurious parse errors