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

未关闭 适合新手
#381 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
84/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
javascript
领域
compilers

调研方向

Issue 指出 optional_parameter_declaration 对 default_value 只接受表达式,并以附近接受 initializer_list 的 init_declarator 为例。阅读这些语法规则,然后用 tree-sitter 解析 repro 中的三个声明,以确认当前的错误。当三个默认花括号列表都能作为 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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tree-sitter/tree-sitter-cpp 的其他 Issue

查看 tree-sitter/tree-sitter-cpp 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。