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

bug: Parse error with variadic template function that takes pointers as arguments

Open
#318 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)

https://tree-sitter.github.io/tree-sitter/7-playground.html

Describe the bug

Incorrect parsing tree for variadic template function with pointer arguments.

Consider this snippet of code:

template <typename... Arg>
void Method(Arg *...args)
{
    (MethodImpl(args), ...);
}

Arg *...args is parsed as binary expression and overall syntax tree for this function definition gets messed up.

Hopefully this can be parsed with less breaking error. Thank you!

Steps To Reproduce/Bad Parse Tree
translation_unit [0, 0] - [5, 0]
  template_declaration [0, 0] - [1, 25]
    parameters: template_parameter_list [0, 9] - [0, 26]
      variadic_type_parameter_declaration [0, 10] - [0, 25]
        type_identifier [0, 22] - [0, 25]
    declaration [1, 0] - [1, 25]
      type: primitive_type [1, 0] - [1, 4]
      declarator: init_declarator [1, 5] - [1, 25]
        declarator: identifier [1, 5] - [1, 11]
        value: argument_list [1, 11] - [1, 25]
          binary_expression [1, 12] - [1, 24]
            left: identifier [1, 12] - [1, 15]
            ERROR [1, 17] - [1, 20]
            right: identifier [1, 20] - [1, 24]
      MISSING ";" [1, 25] - [1, 25]
  compound_statement [2, 0] - [4, 1]
    expression_statement [3, 4] - [3, 28]
      fold_expression [3, 4] - [3, 27]
        left: call_expression [3, 5] - [3, 21]
          function: identifier [3, 5] - [3, 15]
          arguments: argument_list [3, 15] - [3, 21]
            identifier [3, 16] - [3, 20]
Expected Behavior/Parse Tree

Expected to parse to function_definition

translation_unit [0, 0] - [5, 0]
  template_declaration [0, 0] - [4, 1]
    parameters: template_parameter_list [0, 9] - [0, 26]
      variadic_type_parameter_declaration [0, 10] - [0, 25]
        type_identifier [0, 22] - [0, 25]
    function_definition [1, 0] - [4, 1]
      type: primitive_type [1, 0] - [1, 4]
      declarator: function_declarator [1, 5] - [1, 25]
        declarator: identifier [1, 5] - [1, 11]
        parameters: parameter_list [1, 11] - [1, 25]
          variadic_parameter_declaration [1, 12] - [1, 24]
            type: type_identifier [1, 12] - [1, 15]
            declarator: pointer_declarator [1, 16] - [1, 24]
              variadic_declarator [1, 17] - [1, 24]
                identifier [1, 20] - [1, 24]
      body: compound_statement [2, 0] - [4, 1]
        expression_statement [3, 4] - [3, 28]
          fold_expression [3, 4] - [3, 27]
            left: call_expression [3, 5] - [3, 21]
              function: identifier [3, 5] - [3, 15]
              arguments: argument_list [3, 15] - [3, 21]
                identifier [3, 16] - [3, 20]
Repro
template <typename Arg>
void MethodImpl(Arg *args)
{ /* ... */ }

// Code that fails to parse, or causes an error
template <typename... Arg>
void Method(Arg *...args)
{
    (MethodImpl(args), ...);
}

int main() {
	return 0;
}

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 by reproducing the example in the Tree-sitter playground and compare the current tree with the expected tree in this issue. Trace how the C++ grammar handles Arg *...args and confirm completion when the template is represented as a function_definition with a variadic_parameter_declaration, without breaking the existing fold expression.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.