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

bug: Conversion operator definition not recognized as such

Open
#330 1 comment 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)

the latest used at https://tree-sitter.github.io/tree-sitter/7-playground.html

Describe the bug

A conversion operator is sometimes misinterpreted as a regular function with a weird (broken) return type, as opposed to operator_cast.

Steps To Reproduce/Bad Parse Tree
  function_definition [0, 0] - [2, 1]
    storage_class_specifier [0, 0] - [0, 6]
    type: qualified_identifier [0, 7] - [0, 18]
      scope: namespace_identifier [0, 7] - [0, 8]
      name: type_identifier [0, 10] - [0, 18]
    declarator: function_declarator [0, 19] - [0, 24]
      declarator: identifier [0, 19] - [0, 22]
      parameters: parameter_list [0, 22] - [0, 24]
    body: compound_statement [0, 25] - [2, 1]
      return_statement [1, 2] - [1, 12]
        number_literal [1, 9] - [1, 11]

Which is incorrect since it treats this as a function called int with the A::operator return type.

Expected Behavior/Parse Tree

Removing inline in this example results in a correct parsing:

  function_definition [0, 0] - [2, 1]
    declarator: qualified_identifier [0, 0] - [0, 17]
      scope: namespace_identifier [0, 0] - [0, 1]
      name: operator_cast [0, 3] - [0, 17]
        type: primitive_type [0, 12] - [0, 15]
        declarator: abstract_function_declarator [0, 15] - [0, 17]
          parameters: parameter_list [0, 15] - [0, 17]
    body: compound_statement [0, 18] - [2, 1]
      return_statement [1, 2] - [1, 12]
        number_literal [1, 9] - [1, 11]
Repro
inline A::operator int() {
  return 42;
}

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 provided C++ reproduction, inline A::operator int(), and compare its parse tree with the same definition without inline. Trace the grammar rules that classify conversion operators and verify that the result recognizes operator_cast rather than a function with a broken return type; done means the inline form produces the expected tree.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.