resolver: Error on use of local decl in initalizer

`var x = x;`
`let y = y;`

Should not compile.

Bug: tint:819
Bug: tint:1266
Change-Id: I6944a8a95d49329ef754aea3374f20b0dc91e513
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/70660
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-11-23 20:45:51 +00:00
parent 93fc113837
commit f83ada8b5d
2 changed files with 38 additions and 2 deletions

View File

@ -227,7 +227,6 @@ class DependencyScanner {
if (auto* l = stmt->As<ast::LoopStatement>()) {
scope_stack_.Push();
TINT_DEFER(scope_stack_.Pop());
TraverseStatements(l->body->statements);
TraverseStatement(l->continuing);
return;
@ -256,9 +255,9 @@ class DependencyScanner {
return;
}
if (auto* v = stmt->As<ast::VariableDeclStatement>()) {
Declare(v->variable->symbol, v->variable);
TraverseType(v->variable->type);
TraverseExpression(v->variable->constructor);
Declare(v->variable->symbol, v->variable);
return;
}
if (stmt->IsAnyOf<ast::BreakStatement, ast::ContinueStatement,

View File

@ -782,6 +782,43 @@ INSTANTIATE_TEST_SUITE_P(Functions,
} // namespace undeclared_tests
////////////////////////////////////////////////////////////////////////////////
// Self reference by decl
////////////////////////////////////////////////////////////////////////////////
namespace undeclared_tests {
using ResolverDependencyGraphDeclSelfUse = ResolverDependencyGraphTest;
TEST_F(ResolverDependencyGraphDeclSelfUse, GlobalVar) {
const Symbol symbol = Sym("SYMBOL");
Global(symbol, ty.i32(), Mul(Expr(Source{{12, 34}}, symbol), 123));
Build(R"(error: cyclic dependency found: 'SYMBOL' -> 'SYMBOL'
12:34 note: var 'SYMBOL' references var 'SYMBOL' here)");
}
TEST_F(ResolverDependencyGraphDeclSelfUse, GlobalLet) {
const Symbol symbol = Sym("SYMBOL");
GlobalConst(symbol, ty.i32(), Mul(Expr(Source{{12, 34}}, symbol), 123));
Build(R"(error: cyclic dependency found: 'SYMBOL' -> 'SYMBOL'
12:34 note: let 'SYMBOL' references let 'SYMBOL' here)");
}
TEST_F(ResolverDependencyGraphDeclSelfUse, LocalVar) {
const Symbol symbol = Sym("SYMBOL");
WrapInFunction(
Decl(Var(symbol, ty.i32(), Mul(Expr(Source{{12, 34}}, symbol), 123))));
Build("12:34 error: unknown identifier: 'SYMBOL'");
}
TEST_F(ResolverDependencyGraphDeclSelfUse, LocalLet) {
const Symbol symbol = Sym("SYMBOL");
WrapInFunction(
Decl(Const(symbol, ty.i32(), Mul(Expr(Source{{12, 34}}, symbol), 123))));
Build("12:34 error: unknown identifier: 'SYMBOL'");
}
} // namespace undeclared_tests
////////////////////////////////////////////////////////////////////////////////
// Recursive dependency tests
////////////////////////////////////////////////////////////////////////////////