tree-sitter / tree-sitter/tree-sitter-cpp

bug: defaulted `friend` operators get incorrectly parsed as declarations

Open
#351 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
453
Forks
185
PR merge metrics
No merged PRs in 30d

Description

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)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the C++ example and bad parse tree in this issue, then inspect the grammar rules handling friend declarations, operator names, and default method clauses. Confirm the existing parse and identify the grammar entry point responsible; done means the friend operator with = default is represented as a defaulted function rather than a declaration whose value is default.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.