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

bug: Error parsing designated aggregate initialization without =

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

tree-sitter 0.25.4 (726dcd1e872149d95de581589fc408fb8ea9cb0b)

Describe the bug

There is a parsing error in case of aggregate initialization with designated initializers without using the equals sign.

This would be a 1-line change in tree-sitter-c, but as far as I can tell it's not valid C syntax.

     initializer_pair: $ => choice(
       seq(
         field('designator', repeat1(choice(
           $.subscript_designator,
           $.field_designator,
           $.subscript_range_designator,
         ))),
-        '=',
+        optional('='),
         field('value', choice($.expression, $.initializer_list)),
       ),
       seq(

I can't find this in the C++ spec, but it compiles in gcc, clang, and msvc.

Some background: I want to write a query to detect this, but since there's an error, I can't. I tried making a query that includes the ERROR node, but because of this bug, that's not working either.

Steps To Reproduce/Bad Parse Tree

Code:

SomeStruct
{
   .foo{"bar"},
};

Parse tree:

(translation_unit ; [0, 0] - [4, 0]
  (expression_statement ; [0, 0] - [3, 2]
    (compound_literal_expression ; [0, 0] - [3, 1]
      type: (type_identifier) ; [0, 0] - [0, 10]
      value: (initializer_list ; [1, 0] - [3, 1]
        (ERROR ; [2, 3] - [2, 7]
          (field_designator ; [2, 3] - [2, 7]
            (field_identifier))) ; [2, 4] - [2, 7]
        (initializer_list ; [2, 7] - [2, 14]
          (string_literal ; [2, 8] - [2, 13]
            (string_content))))))) ; [2, 9] - [2, 12]
Expected Behavior/Parse Tree

This is the parse tree if I add the = sign like

SomeStruct
{
   .foo = {"bar"},
};
(translation_unit ; [0, 0] - [4, 0]
  (expression_statement ; [0, 0] - [3, 2]
    (compound_literal_expression ; [0, 0] - [3, 1]
      type: (type_identifier) ; [0, 0] - [0, 10]
      value: (initializer_list ; [1, 0] - [3, 1]
        (initializer_pair ; [2, 3] - [2, 17]
          designator: (field_designator ; [2, 3] - [2, 7]
            (field_identifier)) ; [2, 4] - [2, 7]
          value: (initializer_list ; [2, 10] - [2, 17]
            (string_literal ; [2, 11] - [2, 16]
              (string_content)))))))) ; [2, 12] - [2, 15]
Repro
SomeStruct
{
   .foo{"bar"},
};

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 initializer_pair rule and the designated-initializer reproduction shown in the issue; compare the parse trees with and without the equals sign. Done means the no-equals example parses as an initializer_pair without an ERROR node, while preserving the existing equals-sign behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.