From b163aaaf745939c5cee378183d05e312b3b37483 Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Mon, 14 Nov 2022 13:17:58 -0800 Subject: [PATCH] Add a ForIn node for a , move the error (#2387) This is to keep the tree consistent with the error-free state. It also more precisely locates the error. --- toolchain/parser/parser.cpp | 4 +--- .../parser/testdata/for/fail_colon_instead_of_in.carbon | 7 ++++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/toolchain/parser/parser.cpp b/toolchain/parser/parser.cpp index 838fea98a297..45a7e76a6901 100644 --- a/toolchain/parser/parser.cpp +++ b/toolchain/parser/parser.cpp @@ -1457,9 +1457,7 @@ auto Parser::HandleStatementForHeaderInState() -> void { if (auto colon = ConsumeIf(TokenKind::Colon())) { CARBON_DIAGNOSTIC(ExpectedIn, Error, "`:` should be replaced by `in`."); emitter_.Emit(*colon, ExpectedIn); - // TODO: Should probably add a ForIn node for consistency in ParseTree - // structure, but doesn't for consistency with the old implementation. - state.has_error = true; + AddLeafNode(ParseNodeKind::ForIn(), *colon, /*has_error=*/true); } else { CARBON_DIAGNOSTIC(ExpectedIn, Error, "Expected `in` after loop `var` declaration."); diff --git a/toolchain/parser/testdata/for/fail_colon_instead_of_in.carbon b/toolchain/parser/testdata/for/fail_colon_instead_of_in.carbon index d3a5472ce169..4b3178aa3d7c 100644 --- a/toolchain/parser/testdata/for/fail_colon_instead_of_in.carbon +++ b/toolchain/parser/testdata/for/fail_colon_instead_of_in.carbon @@ -14,9 +14,10 @@ // CHECK:STDOUT: {kind: 'Literal', text: 'i32'}, // CHECK:STDOUT: {kind: 'PatternBinding', text: ':', subtree_size: 3}, // CHECK:STDOUT: {kind: 'VariableDeclaration', text: 'var', subtree_size: 4}, +// CHECK:STDOUT: {kind: 'ForIn', text: ':', has_error: yes}, // CHECK:STDOUT: {kind: 'NameReference', text: 'y'}, // CHECK:STDOUT: {kind: 'ForHeaderEnd', text: ')'}, -// CHECK:STDOUT: {kind: 'ForHeader', text: '(', has_error: yes, subtree_size: 7}, +// CHECK:STDOUT: {kind: 'ForHeader', text: '(', subtree_size: 8}, // CHECK:STDOUT: {kind: 'CodeBlockStart', text: '{'}, // CHECK:STDOUT: {kind: 'NameReference', text: 'Print'}, // CHECK:STDOUT: {kind: 'NameReference', text: 'x'}, @@ -24,8 +25,8 @@ // CHECK:STDOUT: {kind: 'CallExpression', text: '(', subtree_size: 4}, // CHECK:STDOUT: {kind: 'ExpressionStatement', text: ';', subtree_size: 5}, // CHECK:STDOUT: {kind: 'CodeBlock', text: '}', subtree_size: 7}, -// CHECK:STDOUT: {kind: 'ForStatement', text: 'for', subtree_size: 15}, -// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 21}, +// CHECK:STDOUT: {kind: 'ForStatement', text: 'for', subtree_size: 16}, +// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 22}, // CHECK:STDOUT: {kind: 'FileEnd', text: ''}, // CHECK:STDOUT: ]