tree-sitter / tree-sitter-rust

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

bug: binary operator not detected as such when preceded by block comment #207

Closed Chris00 closed 7 months ago

Chris00 commented 8 months ago

Did you check existing issues?

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

No response

Describe the bug

Pasing const X: u32 = 1 /* comment */ + 1; considers the comment as the binary operator (see first parse tree below) while it should be basically parsed as const X: u32 = (1 /* comment */) + 1; (see second parse tree below).

Steps To Reproduce/Bad Parse Tree

(source_file
 (const_item const name: (identifier) : type: (primitive_type) =
  value: (binary_expression left: (integer_literal) operator: (block_comment)
                right: +
      (integer_literal))
  ;))

Expected Behavior/Parse Tree

(source_file
 (const_item const name: (identifier) : type: (primitive_type) =
  value: 
   (binary_expression
    left: (parenthesized_expression ( (integer_literal) (block_comment) ))
    operator: + right: (integer_literal))
  ;))

Repro

const X: u32 = 1 /* comment */ + 1;
amaanq commented 7 months ago

Hm not sure i see the same on my end, this is what I'm seeing:

$ ts p test.rs

(source_file [0, 0] - [1, 0]
  (const_item [0, 0] - [0, 35]
    name: (identifier [0, 6] - [0, 7])
    type: (primitive_type [0, 9] - [0, 12])
    value: (binary_expression [0, 15] - [0, 34]
      left: (integer_literal [0, 15] - [0, 16])
      (block_comment [0, 17] - [0, 30])
      right: (integer_literal [0, 33] - [0, 34]))))

Which at first glance, might look incorrect because the operator is missing. The CLI omits them when they are just literals, looking at it with some more info (on an unreleased fork or neovim's InspectTree) results in this:

image image

Which seems ok to me. Let me know if I'm wrong!