From a050f4a144d5ab36c93cd0a443767340301fb32e Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 26 Jan 2018 13:23:16 -0800 Subject: 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. --- source/slang/slang.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'source/slang/slang.cpp') diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 4c9ecf8a8..e4bf251fc 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -1045,6 +1045,12 @@ SLANG_API int spCompile( int anyErrors = req->executeActions(); return anyErrors; } + catch (Slang::AbortCompilationException&) + { + // This should only be thrown if we already emitted a fatal error + // message, so there is no reason to print something else. + return 1; + } catch (...) { req->mSink.diagnose(Slang::SourceLoc(), Slang::Diagnostics::compilationAborted); -- cgit v1.2.3