Resolver: Validate that var/let has a type or initialzier

Fixed: chromium:1230324
Change-Id: I05f6e82696d38014fa1bd16f663c4d39e138d7d5
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/58682
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Auto-Submit: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-07-19 19:50:52 +00:00 committed by Tint LUCI CQ
parent 68596d8a55
commit b291cfced9
2 changed files with 30 additions and 3 deletions

View File

@ -556,7 +556,14 @@ Resolver::VariableInfo* Resolver::Variable(ast::Variable* var,
} }
} else if (var->is_const() && kind != VariableKind::kParameter && } else if (var->is_const() && kind != VariableKind::kParameter &&
!ast::HasDecoration<ast::OverrideDecoration>(var->decorations())) { !ast::HasDecoration<ast::OverrideDecoration>(var->decorations())) {
AddError("let declarations must have initializers", var->source()); AddError("let declaration must have an initializer", var->source());
return nullptr;
} else if (!var->type()) {
AddError(
(kind == VariableKind::kGlobal)
? "module scope var declaration requires a type and initializer"
: "function scope var declaration requires a type or initializer",
var->source());
return nullptr; return nullptr;
} }

View File

@ -31,7 +31,7 @@ TEST_F(ResolverVarLetValidationTest, LetNoInitializer) {
EXPECT_FALSE(r()->Resolve()); EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), EXPECT_EQ(r()->error(),
"12:34 error: let declarations must have initializers"); "12:34 error: let declaration must have an initializer");
} }
TEST_F(ResolverVarLetValidationTest, GlobalLetNoInitializer) { TEST_F(ResolverVarLetValidationTest, GlobalLetNoInitializer) {
@ -40,7 +40,27 @@ TEST_F(ResolverVarLetValidationTest, GlobalLetNoInitializer) {
EXPECT_FALSE(r()->Resolve()); EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), EXPECT_EQ(r()->error(),
"12:34 error: let declarations must have initializers"); "12:34 error: let declaration must have an initializer");
}
TEST_F(ResolverVarLetValidationTest, VarNoInitializerNoType) {
// var a;
WrapInFunction(Var(Source{{12, 34}}, "a", nullptr));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: function scope var declaration requires a type or "
"initializer");
}
TEST_F(ResolverVarLetValidationTest, GlobalVarNoInitializerNoType) {
// var a;
Global(Source{{12, 34}}, "a", nullptr);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: module scope var declaration requires a type and "
"initializer");
} }
TEST_F(ResolverVarLetValidationTest, VarTypeNotStorable) { TEST_F(ResolverVarLetValidationTest, VarTypeNotStorable) {