tree-sitter-cpp icon indicating copy to clipboard operation
tree-sitter-cpp copied to clipboard

[Bug] incorrect assignment_expression reported inside if (x.foo < 0 || bar >= 1); if_statement

Open tr-intel opened this issue 2 years ago • 3 comments

The follwing code: if (x.foo < 0 || bar >= 1);

confuses tree-sitter-cpp to output "assignment_expression"

  if_statement [0, 0] - [0, 27]
    condition: condition_clause [0, 3] - [0, 26]
      value: assignment_expression [0, 4] - [0, 25]   <<<<<<<<< 🤨
...

Notice that if (x.foo < 0 || bar <= 1); works fine

  if_statement [0, 0] - [0, 27]
    condition: condition_clause [0, 3] - [0, 26]
      value: binary_expression [0, 4] - [0, 25] <<<<<< 👍
...

tr-intel avatar Sep 28 '23 15:09 tr-intel

Looks like #121

jdrouhard avatar Nov 15 '23 00:11 jdrouhard

Definitely related to #121, currently replicable on master and gives this parse:

if (x.foo < 0 || bar >= 1);

translation_unit [0, 0] - [1, 0]
  if_statement [0, 0] - [0, 27]
    condition: condition_clause [0, 3] - [0, 26]
      value: assignment_expression [0, 4] - [0, 25]
        left: field_expression [0, 4] - [0, 22]
          argument: identifier [0, 4] - [0, 5]
          field: template_method [0, 6] - [0, 22]
            name: field_identifier [0, 6] - [0, 9]
            arguments: template_argument_list [0, 10] - [0, 22]
              binary_expression [0, 12] - [0, 20]
                left: number_literal [0, 12] - [0, 13]
                right: identifier [0, 17] - [0, 20]
        right: number_literal [0, 24] - [0, 25]
    consequence: expression_statement [0, 26] - [0, 27]

(translation_unit 
  (if_statement 
    condition: (condition_clause 
      value: (assignment_expression 
        left: (field_expression 
          argument: (identifier) 
          field: (template_method 
            name: (field_identifier) 
            arguments: (template_argument_list 
              (binary_expression 
                left: (number_literal) 
                right: (identifier))))) 
        right: (number_literal))) 
    consequence: (expression_statement)))

#121 also parses wrong on playground but not when I pull master myself:

int a = (x->y < u && z >= w);

translation_unit [0, 0] - [1, 0]
  declaration [0, 0] - [0, 29]
    type: primitive_type [0, 0] - [0, 3]
    declarator: init_declarator [0, 4] - [0, 28]
      declarator: identifier [0, 4] - [0, 5]
      value: parenthesized_expression [0, 8] - [0, 28]
        assignment_expression [0, 9] - [0, 27]
          left: field_expression [0, 9] - [0, 24]
            argument: identifier [0, 9] - [0, 10]
            field: template_method [0, 12] - [0, 24]
              name: field_identifier [0, 12] - [0, 13]
              arguments: template_argument_list [0, 14] - [0, 24]
                binary_expression [0, 16] - [0, 22]
                  left: identifier [0, 16] - [0, 17]
                  right: identifier [0, 21] - [0, 22]
          right: identifier [0, 26] - [0, 27]

(translation_unit 
  (declaration type: (primitive_type) 
    declarator: (init_declarator 
      declarator: (identifier) 
      value: (parenthesized_expression 
        (binary_expression 
          left: (binary_expression 
            left: (field_expression 
              argument: (identifier) 
              field: (field_identifier)) 
            right: (identifier)) 
          right: (binary_expression 
            left: (identifier) 
            right: (identifier)))))))

Sean-Hastings avatar Dec 21 '23 13:12 Sean-Hastings

I found an equally strange case here:

void f() {
  if (x.y < x->y && x.y < 50)
  {
    T* x;
  }
}

The tree I get back is:

(ERROR type: (primitive_type) (function_declarator declarator: (identifier) parameters: (parameter_list)) (identifier) (identifier) (field_expression argument: (identifier) field: (field_identifier)) (identifier) (identifier) (ERROR (number_literal)) (binary_expression left: (identifier) right: (identifier)))

But on playground I get:

translation_unit [0, 0] - [6, 0]
  function_definition [0, 0] - [5, 1]
    type: primitive_type [0, 0] - [0, 4]
    declarator: function_declarator [0, 5] - [0, 8]
      declarator: identifier [0, 5] - [0, 6]
      parameters: parameter_list [0, 6] - [0, 8]
    body: compound_statement [0, 9] - [5, 1]
      if_statement [1, 2] - [4, 3]
        condition: condition_clause [1, 5] - [1, 29]
          value: binary_expression [1, 6] - [1, 28]
            left: binary_expression [1, 6] - [1, 16]
              left: field_expression [1, 6] - [1, 9]
                argument: identifier [1, 6] - [1, 7]
                field: field_identifier [1, 8] - [1, 9]
              right: field_expression [1, 12] - [1, 16]
                argument: identifier [1, 12] - [1, 13]
                field: field_identifier [1, 15] - [1, 16]
            right: binary_expression [1, 20] - [1, 28]
              left: field_expression [1, 20] - [1, 23]
                argument: identifier [1, 20] - [1, 21]
                field: field_identifier [1, 22] - [1, 23]
              right: number_literal [1, 26] - [1, 28]
        consequence: compound_statement [2, 2] - [4, 3]
          declaration [3, 4] - [3, 9]
            type: type_identifier [3, 4] - [3, 5]
            declarator: pointer_declarator [3, 5] - [3, 8]
              declarator: identifier [3, 7] - [3, 8]

Which indicates that perhaps there was a regression?

cdacamar avatar Feb 20 '24 04:02 cdacamar