summaryrefslogtreecommitdiffstats
path: root/tests/diagnostics
diff options
context:
space:
mode:
authorCopilot <198982749+Copilot@users.noreply.github.com>2025-08-07 01:01:02 -0700
committerGitHub <noreply@github.com>2025-08-07 08:01:02 +0000
commit9ed7b5264676547855e1378d2b9c9d51c8ba7162 (patch)
tree43eefcd986d1b73dc8d8a13358ce25b1764a2dac /tests/diagnostics
parent063cbeaaea2fb00a10c6058ea4a9632092772ea5 (diff)
Add warning for comma operators used outside for-loops and expand expressions in legacy mode (#7984)
This PR implements a warning system to help users identify potentially unintended comma operator usage in expressions. The comma operator can be confusing when used in contexts like variable initialization where users might have intended to use braces for initialization instead. ## Problem The following code compiles without error but is likely not written as intended: ```slang float4 vColor = (0.f, 0.f, 0.f, 1.f); // Uses comma operators, evaluates to 1.f ``` The intended code should use braces: ```slang float4 vColor = {0.f, 0.f, 0.f, 1.f}; // Proper initialization ``` ## Solution Added a new warning diagnostic (`commaOperatorUsedInExpression`, ID: 41024) that warns when comma operators are used in expressions, with exemptions for contexts where they are commonly intended: - **For-loop side effects**: `for (int i = 0; i < 10; i++, x++)` - no warning - **Expand expressions**: `expand(f(), g(each param))` - no warning - **Slang 2026+ mode**: `let m = (1,2,3)` creates tuples - no warning - **All other expressions**: `float4 v = (a, b, c, d)` and `return a, b` - warns for each comma ## Implementation Details - Added context tracking in `SemanticsContext` with `m_inForLoopSideEffect` flag - Modified `visitForStmt` to use special context when checking side effect expressions - Added comma operator detection in `visitInvokeExpr` for `InfixExpr` nodes - Added language version check using `isSlang2026OrLater()` to disable warnings in Slang 2026+ mode where parentheses create tuples - Performance optimization: language version check is hoisted to avoid unnecessary casting - Warning can be suppressed using `-Wno-41024` command line flag ## Test Coverage Added comprehensive test cases using filecheck format that verify: - Warnings are generated for comma operators in variable initialization (legacy mode only) - Warnings are generated for comma operators in return statements (legacy mode only) - Warnings are generated for comma operators in general expressions (legacy mode only) - No warnings for comma operators in for-loop side effects - No warnings in Slang 2026+ mode where parentheses create tuples - Warning suppression works correctly Example output (legacy mode): ``` warning 41024: comma operator used in expression (may be unintended) float4 vColor = (0.f, 0.f, 0.f, 1.f); ^ warning 41024: comma operator used in expression (may be unintended) return a *= 2, a + 1; ^ ``` Fixes #6732. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: aidanfnv <198290069+aidanfnv@users.noreply.github.com> Co-authored-by: slangbot <ellieh+slangbot@nvidia.com> Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: aidanfnv <aidanf@nvidia.com> Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com>
Diffstat (limited to 'tests/diagnostics')
-rw-r--r--tests/diagnostics/comma-operator-warning-slang2026.slang19
-rw-r--r--tests/diagnostics/comma-operator-warning.slang24
2 files changed, 43 insertions, 0 deletions
diff --git a/tests/diagnostics/comma-operator-warning-slang2026.slang b/tests/diagnostics/comma-operator-warning-slang2026.slang
new file mode 100644
index 000000000..e91f540d4
--- /dev/null
+++ b/tests/diagnostics/comma-operator-warning-slang2026.slang
@@ -0,0 +1,19 @@
+//DIAGNOSTIC_TEST:SIMPLE:
+
+#language 2026
+
+int testCommaOperatorInSlang2026()
+{
+ // In Slang 2026, this creates a tuple - should NOT generate a warning
+ let m = (1, 2, 3);
+
+ // Test accessing tuple elements
+ let x = m._0;
+ let y = m._1;
+ let z = m._2;
+
+ // Another tuple example - should NOT generate a warning
+ var t = (1.0f, 2.0f, 3.0f);
+
+ return x + y + z;
+} \ No newline at end of file
diff --git a/tests/diagnostics/comma-operator-warning.slang b/tests/diagnostics/comma-operator-warning.slang
new file mode 100644
index 000000000..945cc792d
--- /dev/null
+++ b/tests/diagnostics/comma-operator-warning.slang
@@ -0,0 +1,24 @@
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):
+
+int testCommaOperator()
+{
+ // This should generate a warning - comma operator in variable initialization
+ // CHECK: ([[# @LINE+1]]): warning 41024:
+ float4 vColor = (0.f, 0.f, 0.f, 1.f);
+
+ // This should NOT generate a warning - comma operator in for-loop side effect
+ for (int i = 0; i < 10; i++, vColor.x++)
+ {
+ }
+
+ // This should generate a warning - comma operator in regular expression
+ // CHECK: ([[# @LINE+1]]): warning 41024:
+ int x = (1, 2, 3);
+
+ // This should now generate a warning - comma operator in return statement
+ // CHECK: ([[# @LINE+2]]): warning 41024:
+ int a = 5;
+ return a *= 2, a + 1;
+}
+
+void someFunction(int value) {} \ No newline at end of file