tree-sitter / tree-sitter/tree-sitter-cpp
bug: Default member initializer with value of zero parsed as pure virtual function
Nobody has claimed this yet.
- 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)
No response
Describe the bug
A default member initialization is interpreted as a pure virtual clause when the value is zero. I'm unsure whether or not this is a fundamental ambiguity in C++ syntax.
Steps To Reproduce/Bad Parse Tree
The following code:
struct A {
int x = 0;
};
parses to:
translation_unit [0, 0] - [4, 0]
struct_specifier [0, 0] - [2, 1]
name: type_identifier [0, 7] - [0, 8]
body: field_declaration_list [0, 9] - [2, 1]
function_definition [1, 2] - [1, 12]
type: primitive_type [1, 2] - [1, 5]
declarator: field_identifier [1, 6] - [1, 7]
pure_virtual_clause [1, 8] - [1, 12]
i.e. the = 0 there is interpreted as x being a function that is pure virtual.
Expected Behavior/Parse Tree
A value other than zero seems to work as expected:
struct A {
int x = 1;
};
gives:
translation_unit [0, 0] - [4, 0]
struct_specifier [0, 0] - [2, 1]
name: type_identifier [0, 7] - [0, 8]
body: field_declaration_list [0, 9] - [2, 1]
field_declaration [1, 2] - [1, 12]
type: primitive_type [1, 2] - [1, 5]
declarator: field_identifier [1, 6] - [1, 7]
default_value: number_literal [1, 10] - [1, 11]
Repro
No response
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No repository file or test is named. Start by reproducing the struct A { int x = 0; }; example and inspect the grammar entry points producing field_declaration and pure_virtual_clause; done means zero-valued default member initializers produce the same field structure as the value-1 example.
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
- Mostly clear
- Newbie friendliness
- 45/100