diff options
| author | Yong He <yonghe@outlook.com> | 2023-04-04 15:29:36 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-04 15:29:36 -0700 |
| commit | 492c6f2fd1a64c9d60a968b55bfd1000cc2ae8e7 (patch) | |
| tree | 2fa6849e2b0228d7cede69f5ac5445b1d543fa01 | |
| parent | 68c7d5cda2d6f2eb7bfb3a7e15860eb3ded25424 (diff) | |
Diagnose on using assignment as predicate expr. (#2774)
Co-authored-by: Yong He <yhe@nvidia.com>
| -rw-r--r-- | source/slang/slang-check-stmt.cpp | 4 | ||||
| -rw-r--r-- | source/slang/slang-diagnostic-defs.h | 1 | ||||
| -rw-r--r-- | tests/compute/loop-unroll.slang | 3 | ||||
| -rw-r--r-- | tests/diagnostics/assign-in-if.slang | 12 | ||||
| -rw-r--r-- | tests/diagnostics/assign-in-if.slang.expected | 14 |
5 files changed, 32 insertions, 2 deletions
diff --git a/source/slang/slang-check-stmt.cpp b/source/slang/slang-check-stmt.cpp index bc89dc94e..a61f48af6 100644 --- a/source/slang/slang-check-stmt.cpp +++ b/source/slang/slang-check-stmt.cpp @@ -151,6 +151,10 @@ namespace Slang Expr* SemanticsVisitor::checkPredicateExpr(Expr* expr) { + if (as<AssignExpr>(expr)) + { + getSink()->diagnose(expr, Diagnostics::assignmentInPredicateExpr); + } Expr* e = expr; e = CheckTerm(e); e = coerce(m_astBuilder->getBoolType(), e); diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h index 39ceb6678..fed4944d3 100644 --- a/source/slang/slang-diagnostic-defs.h +++ b/source/slang/slang-diagnostic-defs.h @@ -277,6 +277,7 @@ DIAGNOSTIC(30053, Error, breakLabelNotFound, "label '$0' used as break target is DIAGNOSTIC(30054, Error, targetLabelDoesNotMarkBreakableStmt, "invalid break target: statement labeled '$0' is not breakable.") DIAGNOSTIC(30055, Error, useOfNonShortCircuitingOperatorInDiffFunc, "non-short-circuiting `?:` operator is not allowed in a differentiable function, use `select` instead.") DIAGNOSTIC(30056, Warning, useOfNonShortCircuitingOperator, "non-short-circuiting `?:` operator is deprecated, use 'select' instead.") +DIAGNOSTIC(30057, Error, assignmentInPredicateExpr, "use an assignment operation as predicate expression is not allowed, wrap the assignment with '()' to clarify the intent.") DIAGNOSTIC(30043, Error, getStringHashRequiresStringLiteral, "getStringHash parameter can only accept a string literal") diff --git a/tests/compute/loop-unroll.slang b/tests/compute/loop-unroll.slang index 9a3fa3ba4..c0ea7686c 100644 --- a/tests/compute/loop-unroll.slang +++ b/tests/compute/loop-unroll.slang @@ -1,7 +1,6 @@ //TEST(compute):COMPARE_COMPUTE: -shaderobj //TEST(compute):COMPARE_COMPUTE:-dx12 -shaderobj -//TODO(JS): This test fails with a crash in CreateComputePipelineState, so disabled for now -//DISABLE_TEST(compute):COMPARE_COMPUTE:-dx12 -use-dxil -shaderobj +//TEST(compute):COMPARE_COMPUTE:-dx12 -use-dxil -shaderobj //TEST(compute):COMPARE_COMPUTE:-cpu -shaderobj //TEST(compute):COMPARE_COMPUTE:-cuda -shaderobj // Note VK output is not loop unrolled diff --git a/tests/diagnostics/assign-in-if.slang b/tests/diagnostics/assign-in-if.slang new file mode 100644 index 000000000..ef2222365 --- /dev/null +++ b/tests/diagnostics/assign-in-if.slang @@ -0,0 +1,12 @@ +//DIAGNOSTIC_TEST:REFLECTION:-stage compute -entry main -target hlsl + +[numthreads(1, 1, 1)] +void main( + uint3 dispatchThreadID : SV_DispatchThreadID) +{ + int a = 1; + if (a = 0) // error + {} + if ((a = 0)) // ok. + {} +}
\ No newline at end of file diff --git a/tests/diagnostics/assign-in-if.slang.expected b/tests/diagnostics/assign-in-if.slang.expected new file mode 100644 index 000000000..373072d66 --- /dev/null +++ b/tests/diagnostics/assign-in-if.slang.expected @@ -0,0 +1,14 @@ +result code = 1 +standard error = { +tests/diagnostics/assign-in-if.slang(8): error 30057: use an assignment operation as predicate expression is not allowed, wrap the assignment with '()' to clarify the intent. + if (a = 0) // error + ^ +tests/diagnostics/assign-in-if.slang(8): warning 30081: implicit conversion from 'int' to 'bool' is not recommended + if (a = 0) // error + ^ +tests/diagnostics/assign-in-if.slang(10): warning 30081: implicit conversion from 'int' to 'bool' is not recommended + if ((a = 0)) // ok. + ^ +} +standard output = { +} |
