Skip to content

bug: ‍Don't parse (foo) && bar as cast expression. #291

@HKalbasi

Description

@HKalbasi

Did you check existing issues?

  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues of tree-sitter-c

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

No response

Describe the bug

int main() {
	 if( (pVal) && pVal->pSrc->nSrc>0 ){
     }
}

This code is parsed as a pointer of pointer casted to pVal, but it should be parsed as pVal and less than expression. I know that tree sitter inherently can't decide between type alias and variables, but in this case & & expr is never a valid C code since & expr is not an l value so (foo) && bar should never become a cast.

Steps To Reproduce/Bad Parse Tree

translation_unit [0, 0] - [4, 0]
  function_definition [0, 0] - [3, 1]
    type: primitive_type [0, 0] - [0, 3]
    declarator: function_declarator [0, 4] - [0, 10]
      declarator: identifier [0, 4] - [0, 8]
      parameters: parameter_list [0, 8] - [0, 10]
    body: compound_statement [0, 11] - [3, 1]
      if_statement [1, 2] - [2, 6]
        condition: parenthesized_expression [1, 4] - [1, 36]
          binary_expression [1, 6] - [1, 34]
            left: cast_expression [1, 6] - [1, 32]
              type: type_descriptor [1, 7] - [1, 11]
                type: type_identifier [1, 7] - [1, 11]
              value: pointer_expression [1, 13] - [1, 32]
                argument: pointer_expression [1, 14] - [1, 32]
                  argument: field_expression [1, 16] - [1, 32]
                    argument: field_expression [1, 16] - [1, 26]
                      argument: identifier [1, 16] - [1, 20]
                      field: field_identifier [1, 22] - [1, 26]
                    field: field_identifier [1, 28] - [1, 32]
            right: number_literal [1, 33] - [1, 34]
        consequence: compound_statement [1, 36] - [2, 6]

Expected Behavior/Parse Tree

translation_unit [0, 0] - [4, 0]
  function_definition [0, 0] - [3, 1]
    type: primitive_type [0, 0] - [0, 3]
    declarator: function_declarator [0, 4] - [0, 10]
      declarator: identifier [0, 4] - [0, 8]
      parameters: parameter_list [0, 8] - [0, 10]
    body: compound_statement [0, 11] - [3, 1]
      if_statement [1, 2] - [2, 6]
        condition: parenthesized_expression [1, 4] - [1, 34]
          binary_expression [1, 6] - [1, 32]
            left: identifier [1, 6] - [1, 10]
            right: binary_expression [1, 14] - [1, 32]
              left: field_expression [1, 14] - [1, 30]
                argument: field_expression [1, 14] - [1, 24]
                  argument: identifier [1, 14] - [1, 18]
                  field: field_identifier [1, 20] - [1, 24]
                field: field_identifier [1, 26] - [1, 30]
              right: number_literal [1, 31] - [1, 32]
        consequence: compound_statement [1, 34] - [2, 6]

Repro

int main() {
	 if( (pVal) && pVal->pSrc->nSrc>0 ){
     }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions