Skip to content

bug: defaulted friend operators get incorrectly parsed as declarations #351

@xhrnca00

Description

@xhrnca00

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-cpp

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

No response

Describe the bug

In C++20, operator== and operator<=> can get marked as default. These operators do not have to be methods and can be friends instead. This does not get parsed correctly.

Steps To Reproduce/Bad Parse Tree

Example (adapted from Default comparisons (cppreference))

struct X
{
    bool operator==(const X&) const = default;
    friend bool operator==(X, X) = default;
};

(Bad) tree:

(translation_unit ; [0, 0] - [5, 0] cpp
  (struct_specifier ; [0, 0] - [4, 1] cpp
    name: (type_identifier) ; [0, 7] - [0, 8] cpp
    body: (field_declaration_list ; [1, 0] - [4, 1] cpp
      (function_definition ; [2, 4] - [2, 46] cpp
        type: (primitive_type) ; [2, 4] - [2, 8] cpp
        declarator: (function_declarator ; [2, 9] - [2, 35] cpp
          declarator: (operator_name) ; [2, 9] - [2, 19] cpp
          parameters: (parameter_list ; [2, 19] - [2, 29] cpp
            (parameter_declaration ; [2, 20] - [2, 28] cpp
              (type_qualifier) ; [2, 20] - [2, 25] cpp
              type: (type_identifier) ; [2, 26] - [2, 27] cpp
              declarator: (abstract_reference_declarator))) ; [2, 27] - [2, 28] cpp
          (type_qualifier)) ; [2, 30] - [2, 35] cpp
        (default_method_clause)) ; [2, 36] - [2, 46] cpp
      (friend_declaration ; [3, 4] - [3, 43] cpp
        (declaration ; [3, 11] - [3, 43] cpp
          type: (primitive_type) ; [3, 11] - [3, 15] cpp
          declarator: (init_declarator ; [3, 16] - [3, 42] cpp
            declarator: (function_declarator ; [3, 16] - [3, 32] cpp
              declarator: (operator_name) ; [3, 16] - [3, 26] cpp
              parameters: (parameter_list ; [3, 26] - [3, 32] cpp
                (parameter_declaration ; [3, 27] - [3, 28] cpp
                  type: (type_identifier)) ; [3, 27] - [3, 28] cpp
                (parameter_declaration ; [3, 30] - [3, 31] cpp
                  type: (type_identifier)))) ; [3, 30] - [3, 31] cpp
            value: (identifier))))))) ; [3, 35] - [3, 42] cpp

See that the friend function with = default got parsed as a declaration, and the default got parsed as a value, not a keyword.

Expected Behavior/Parse Tree

Defaulted friend functions (currently probably only operator== and operator<=>) should get parsed as such.

Repro

(see example above bad tree)

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