tint: Prevent function calls at module-scope

Bug: chromium:1339723
Bug: tint:1606
Change-Id: Ia2e10d6ac38fde0a9c851d279fe07f50f108546e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/95081
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-06-29 19:19:31 +00:00 committed by Dawn LUCI CQ
parent aa037ac489
commit c7d6ab6c5a
2 changed files with 15 additions and 1 deletions

View File

@ -354,10 +354,19 @@ TEST_F(ResolverFunctionValidationTest, CannotCallEntryPoint) {
EXPECT_FALSE(r()->Resolve()); EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), EXPECT_EQ(r()->error(),
R"(12:34 error: entry point functions cannot be the target of a function call)"); R"(12:34 error: entry point functions cannot be the target of a function call)");
} }
TEST_F(ResolverFunctionValidationTest, CannotCallFunctionAtModuleScope) {
// fn F() -> i32 { return 1; }
// var x = F();
Func("F", {}, ty.i32(), {Return(1_i)});
GlobalVar("x", nullptr, Call(Source{{12, 34}}, "F"), ast::StorageClass::kPrivate);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: functions cannot be called at module-scope)");
}
TEST_F(ResolverFunctionValidationTest, PipelineStage_MustBeUnique_Fail) { TEST_F(ResolverFunctionValidationTest, PipelineStage_MustBeUnique_Fail) {
// @fragment // @fragment
// @vertex // @vertex

View File

@ -1695,6 +1695,11 @@ bool Validator::FunctionCall(const sem::Call* call, sem::Statement* current_stat
auto sym = decl->target.name->symbol; auto sym = decl->target.name->symbol;
auto name = symbols_.NameFor(sym); auto name = symbols_.NameFor(sym);
if (!current_statement) { // Function call at module-scope.
AddError("functions cannot be called at module-scope", decl->source);
return false;
}
if (target->Declaration()->IsEntryPoint()) { if (target->Declaration()->IsEntryPoint()) {
// https://www.w3.org/TR/WGSL/#function-restriction // https://www.w3.org/TR/WGSL/#function-restriction
// An entry point must never be the target of a function call. // An entry point must never be the target of a function call.