diff --git a/src/tint/reader/wgsl/parser_impl.cc b/src/tint/reader/wgsl/parser_impl.cc index cbfbc461f0..c9221f1a8b 100644 --- a/src/tint/reader/wgsl/parser_impl.cc +++ b/src/tint/reader/wgsl/parser_impl.cc @@ -2258,20 +2258,30 @@ Maybe ParserImpl::loop_statement() { return Failure::kNoMatch; } - return expect_brace_block("loop", [&]() -> Maybe { + Maybe continuing(Failure::kErrored); + auto body_start = peek().source(); + auto body = expect_brace_block("loop", [&]() -> Maybe { auto stmts = expect_statements(); if (stmts.errored) { return Failure::kErrored; } - auto continuing = continuing_statement(); + continuing = continuing_statement(); if (continuing.errored) { return Failure::kErrored; } - - auto* body = create(source, stmts.value, utils::Empty); - return create(source, body, continuing.value); + return stmts; }); + if (body.errored) { + return Failure::kErrored; + } + auto body_end = last_source(); + + return create( + source, + create(Source::Combine(body_start, body_end), body.value, + utils::Empty), + continuing.value); } ForHeader::ForHeader(const ast::Statement* init, @@ -2482,7 +2492,8 @@ Maybe ParserImpl::break_if_statement() { // continuing_compound_statement: // brace_left statement* break_if_statement? brace_right Maybe ParserImpl::continuing_compound_statement() { - return expect_brace_block("", [&]() -> Expect { + auto source_start = peek().source(); + auto body = expect_brace_block("", [&]() -> Expect { StatementList stmts; while (continue_parsing()) { @@ -2506,8 +2517,15 @@ Maybe ParserImpl::continuing_compound_statement() { stmts.Push(stmt.value); } - return create(Source{}, stmts, utils::Empty); + return stmts; }); + if (body.errored) { + return Failure::kErrored; + } + auto source_end = last_source(); + + return create(Source::Combine(source_start, source_end), body.value, + utils::Empty); } // continuing_statement diff --git a/src/tint/reader/wgsl/parser_impl_loop_stmt_test.cc b/src/tint/reader/wgsl/parser_impl_loop_stmt_test.cc index 5122332481..43cf2196be 100644 --- a/src/tint/reader/wgsl/parser_impl_loop_stmt_test.cc +++ b/src/tint/reader/wgsl/parser_impl_loop_stmt_test.cc @@ -26,6 +26,11 @@ TEST_F(ParserImplTest, LoopStmt_BodyNoContinuing) { EXPECT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e.value, nullptr); + EXPECT_EQ(e->body->source.range.begin.line, 1u); + EXPECT_EQ(e->body->source.range.begin.column, 6u); + EXPECT_EQ(e->body->source.range.end.line, 1u); + EXPECT_EQ(e->body->source.range.end.column, 18u); + ASSERT_EQ(e->body->statements.Length(), 1u); EXPECT_TRUE(e->body->statements[0]->Is()); @@ -40,11 +45,21 @@ TEST_F(ParserImplTest, LoopStmt_BodyWithContinuing) { EXPECT_FALSE(p->has_error()) << p->error(); ASSERT_NE(e.value, nullptr); + EXPECT_EQ(e->body->source.range.begin.line, 1u); + EXPECT_EQ(e->body->source.range.begin.column, 6u); + EXPECT_EQ(e->body->source.range.end.line, 1u); + EXPECT_EQ(e->body->source.range.end.column, 41u); + ASSERT_EQ(e->body->statements.Length(), 1u); EXPECT_TRUE(e->body->statements[0]->Is()); EXPECT_EQ(e->continuing->statements.Length(), 1u); EXPECT_TRUE(e->continuing->statements[0]->Is()); + + EXPECT_EQ(e->continuing->source.range.begin.line, 1u); + EXPECT_EQ(e->continuing->source.range.begin.column, 28u); + EXPECT_EQ(e->continuing->source.range.end.line, 1u); + EXPECT_EQ(e->continuing->source.range.end.column, 40u); } TEST_F(ParserImplTest, LoopStmt_NoBodyNoContinuing) {