tree-sitter / tree-sitter-rust

Rust grammar for tree-sitter
MIT License
340 stars 97 forks source link

Possible regression when `default` or `union` are used as identifiers #132

Closed ninevra closed 1 year ago

ninevra commented 2 years ago

I noticed when testing #93 that npm test reports errors in parsing several of the example files:

examples/regex/regex-syntax/src/ast/parse.rs                                                    17 ms   (ERROR [1737, 22] - [1737, 27])
examples/regex/regex-syntax/src/hir/interval.rs                                                 1 ms    (ERROR [288, 28] - [288, 33])
examples/serde/serde_derive/src/de.rs                                                           9 ms    (ERROR [636, 16] - [636, 25])
examples/serde/serde_derive/src/internals/attr.rs                                               5 ms    (ERROR [186, 24] - [186, 30])
examples/serde/serde_derive_internals/src/attr.rs                                               5 ms    (ERROR [186, 24] - [186, 30])

git bisect identifies a360da0a29a19c281d08295a35ecd0544d2da211 as the first bad commit, and it looks like many of the errors occur when default or union are used as variable names. For example, examples/serde/serde_derive/src/internals/attr.rs contains

        let mut default = Attr::none(cx, "default");

which parses as

(let_declaration [186, 8] - [186, 52]
  (mutable_specifier [186, 12] - [186, 15])
  pattern: (tuple_struct_pattern [186, 16] - [186, 51]
    type: (scoped_identifier [186, 16] - [186, 36]
      path: (identifier [186, 16] - [186, 23])
      (ERROR [186, 24] - [186, 30]
        (identifier [186, 26] - [186, 30]))
      name: (identifier [186, 32] - [186, 36]))
    (identifier [186, 37] - [186, 39])
    (string_literal [186, 41] - [186, 50])))

Before a360da0, that line parsed as

(let_declaration [186, 8] - [186, 52]
  (mutable_specifier [186, 12] - [186, 15])
  pattern: (identifier [186, 16] - [186, 23])
  value: (call_expression [186, 26] - [186, 51]
    function: (scoped_identifier [186, 26] - [186, 36]
      path: (identifier [186, 26] - [186, 30])
      name: (identifier [186, 32] - [186, 36]))
    arguments: (arguments [186, 36] - [186, 51]
      (identifier [186, 37] - [186, 39])
      (string_literal [186, 41] - [186, 50]))))