diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2018-01-26 13:23:16 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-01-26 13:23:16 -0800 |
| commit | a050f4a144d5ab36c93cd0a443767340301fb32e (patch) | |
| tree | 2c0326de410cf550b16635baf4767f544d925da4 /source/slang/check.cpp | |
| parent | 4cd18ec9de72dfaa0c550f10a39e53d31cc08899 (diff) | |
Fix some crashing bugs around local variable declarations. (#385)
The basic problem here arises when a local variable is used either before its own declaration:
```hlsl
int a = b;
...
int b = 0;
```
or when a local variable is used *in* its own decalration:
```hlsl
int b = b;
```
In each case, Slang considers the scope of the `{}`-enclosed function body (or nested statement) as a whole, and so the lookup can "see" the declaration even if it is later in the same function.
This behavior isn't really correct for HLSL semantics, so the right long-term fix is to change our scoping rules, but for now users really just want the compiler to not crash on code like this, and give an error message that points at the issue.
This change makes both of the above examples print an error message saying that variable `b` was used before its declaration, which is accurate to the way that Slang is interpreting those code examples.
This is currently treated as a fatal error, so that compilation aborts right away, to avoid all of the downstream crashes that these cases were causing.
Diffstat (limited to 'source/slang/check.cpp')
| -rw-r--r-- | source/slang/check.cpp | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/source/slang/check.cpp b/source/slang/check.cpp index e2d519a4c..ea7f1be2a 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -481,7 +481,33 @@ namespace Slang if (decl->checkState == DeclCheckState::CheckingHeader) { // We tried to reference the same declaration while checking it! + // + // TODO: we should ideally be tracking a "chain" of declarations + // being checked on the stack, so that we can report the full + // chain that leads from this declaration back to itself. + // sink->diagnose(decl, Diagnostics::cyclicReference, decl); + return; + } + + // Hack: if we are somehow referencing a local variable declaration + // before the line of code that defines it, then we need to diagnose + // an error. + // + // TODO: The right answer is that lookup should have been performed in + // the scope that was in place *before* the variable was declared, but + // this is a quick fix that at least alerts the user to how we are + // interpreting their code. + if (auto varDecl = decl.As<Variable>()) + { + if (auto parenScope = varDecl->ParentDecl->As<ScopeDecl>()) + { + // TODO: This diagnostic should be emitted on the line that is referencing + // the declaration. That requires `EnsureDecl` to take the requesting + // location as a parameter. + sink->diagnose(decl, Diagnostics::localVariableUsedBeforeDeclared, decl); + return; + } } if (DeclCheckState::CheckingHeader > decl->checkState) @@ -2151,7 +2177,7 @@ namespace Slang { for (auto decl : declGroup->decls) { - checkDecl(decl); + DeclVisitor::dispatch(decl); } } @@ -2808,6 +2834,7 @@ namespace Slang stmt->varDecl->type.type = getSession()->getIntType(); addModifier(stmt->varDecl, new ConstModifier()); + stmt->varDecl->SetCheckState(DeclCheckState::Checked); RefPtr<IntVal> rangeBeginVal; RefPtr<IntVal> rangeEndVal; |
