diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2020-02-05 13:26:39 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-05 13:26:39 -0800 |
| commit | b42b865ac7ce144561fa17743616e550eeae7102 (patch) | |
| tree | 3c322ee542a2d496218d38aad8fdcbd58ddffe7b /tests | |
| parent | 122126c006e53bd48064f220104d425b8bf91ddf (diff) | |
Improve behavior when undefined identifier is a contextual keyword (#1200)
The HLSL language has keywords with very common names like `triangle`, and Slang doesn't want to preclude users from using such names for their variables/functions/etc.
In addition, Slang adds new keywords on top of HLSL (like `extension`) and we don't want those to prevent us from compiling existing code.
As a result, almost all keywords in Slang are contextual keywords, and they can be shadowed by user varaibles.
The down-side to making all keywords contextual is that in a case like this:
```
int test() { return triangle; }
```
The identifier `triangle` is *not* undefined as far as lookup (it is defined as a modifier keyword), so the existing "undefined identifier" logic gets bypassed, and instead we ran into an internal compiler error trying to construct an expression that refers to a modifier keyword.
Fortunately, the internal compiler error in that case was overkill, and the compiler already had defensive logic to produce an expression with an error type if it couldn't figure out what the type of a declaration reference should be.
The main fix here is thus to emit an "undefined identifier" error instead of an internal compiler error at the point where we see an attempt to reference a declaration that shouldn't be available in an expression context.
In order to improve the quality of the diagnostic, the code for constructing declaration references was updated to pass along a source location to be used in error messages.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/bugs/keyword-undefined-identifier.slang | 35 | ||||
| -rw-r--r-- | tests/bugs/keyword-undefined-identifier.slang.expected | 7 |
2 files changed, 42 insertions, 0 deletions
diff --git a/tests/bugs/keyword-undefined-identifier.slang b/tests/bugs/keyword-undefined-identifier.slang new file mode 100644 index 000000000..e1f46f517 --- /dev/null +++ b/tests/bugs/keyword-undefined-identifier.slang @@ -0,0 +1,35 @@ +// keyword-undeclared-identifier.slang + +//DIAGNOSTIC_TEST:SIMPLE: + +// Test that using a contextual keyword in +// a context where it is an underfined +// identifier produces a reasonable error +// message instead of an internal compiler error +// +// Note that HLSL has keywords with very +// common names like `triangle` and `sample`, +// so it is easy for those to collide with +// local variable names. +// +// Slang decides to make almost all keywords +// contextual, so that they are looked up +// in lexical scope and can be shadowed by +// user-defined variables or functions. +// +// The problem in this case is that code +// could easily be refactored so that it +// uses one of the contextual keywrods in +// a place where it is no longer shadowed, +// but contextually needs to be treated +// as an expression. + +int instanceTest() +{ + return instance; +} + +int triangleTest() +{ + return triangle; +} diff --git a/tests/bugs/keyword-undefined-identifier.slang.expected b/tests/bugs/keyword-undefined-identifier.slang.expected new file mode 100644 index 000000000..00c9fe8db --- /dev/null +++ b/tests/bugs/keyword-undefined-identifier.slang.expected @@ -0,0 +1,7 @@ +result code = -1 +standard error = { +tests/bugs/keyword-undefined-identifier.slang(29): error 30015: undefined identifier 'instance'. +tests/bugs/keyword-undefined-identifier.slang(34): error 30015: undefined identifier 'triangle'. +} +standard output = { +} |
