bug: braced default argument (= {}) in a parameter list is an ERROR

オープン 初心者向け
#381 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
84/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript
領域
compilers

調査の方向性

issue では、optional_parameter_declaration が default_value に対して式しか受け付けないことを指摘し、initializer_list を受け付ける近くの例として init_declarator を挙げています。それらの文法規則を読み、次に repro の3つの宣言を tree-sitter で解析して、現在のエラーを確認してください。3つのデフォルトの波括弧リストすべてが、エラーなく initializer_list として解析されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

bug
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

A default argument written as a braced initializer list, valid since C++11 ([dcl.fct.default] allows an initializer-clause, which includes a braced-init-list), makes the tree contain an error. The default value is recovered as a compound_literal_expression with a MISSING type_identifier, so has_error is true for the whole file.

This is common in modern code (Options = {}, std::vector v = {1, 2}). In one C++20 project it was the most frequent cause of parse errors: 16 of 131 files failed only because of it.

optional_parameter_declaration accepts only an expression as default_value, both in v0.23.4 and on master (c009222), while init_declarator already accepts initializer_list.

Steps To Reproduce/Bad Parse Tree

Save the repro below as repro.cpp.
Run tree-sitter parse repro.cpp, or parse it with any binding.
Parse tree from tree-sitter-cpp 0.23.4 (named nodes only), first declaration:

translation_unit
declaration
type: primitive_type
declarator: function_declarator
declarator: identifier
parameters: parameter_list
optional_parameter_declaration
type: primitive_type
declarator: identifier
default_value: compound_literal_expression
type: type_identifier (MISSING)
value: initializer_list
The other two declarations give the same compound_literal_expression with a MISSING type_identifier.

Expected Behavior/Parse Tree

No error; the braced list is the default value:

    optional_parameter_declaration
      type: primitive_type
      declarator: identifier
      default_value: initializer_list

A possible fix is field('default_value', choice($.expression, $.initializer_list)) in optional_parameter_declaration, as init_declarator does for its value.

Repro
void f(int x = {});
void g(std::vector<int> v = {1, 2});
struct Options {};
void h(Options = {});
主要言語
JavaScript
スター
463
フォーク
196
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

tree-sitter/tree-sitter-cpp のほかの issue

tree-sitter/tree-sitter-cpp の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。