summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-07-29 19:26:52 -0700
committerGitHub <noreply@github.com>2025-07-29 19:26:52 -0700
commitb8663a5d93e6e73d5e2d7e737cffd1efac055719 (patch)
tree21eedc0d19065306eb4de7e1be8d00d1045ee032
parent48efc60380aa79e8c4aba13976cc2015f38a659e (diff)
Fix ICE when immutable value is passed to a bwd_diff function. (#7973)
-rw-r--r--source/slang/slang-check-expr.cpp10
-rw-r--r--tests/diagnostics/implicit-cast-lvalue.slang.expected1
-rw-r--r--tests/diagnostics/lvalue-in-bwd-diff.slang23
-rw-r--r--tests/diagnostics/methods/mutating-method-on-rvalue.slang.expected1
4 files changed, 31 insertions, 4 deletions
diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp
index 31b08e925..f70760c5d 100644
--- a/source/slang/slang-check-expr.cpp
+++ b/source/slang/slang-check-expr.cpp
@@ -2703,10 +2703,14 @@ void SemanticsVisitor::maybeDiagnoseConstVariableAssignment(Expr* expr)
{
e = subscriptExpr->baseExpression;
}
- else
+ else if (as<ThisExpr>(e))
{
break;
}
+ else
+ {
+ return;
+ }
}
// Check if we're trying to assign to a non-l-value (const variable, immutable member, etc.)
@@ -2979,7 +2983,9 @@ Expr* SemanticsVisitor::CheckInvokeExprWithCheckedOperands(InvokeExpr* expr)
else if (!as<ErrorType>(argExpr->type))
{
// Emit additional diagnostic for invalid pointer taking operations
- auto funcDeclRef = getDeclRef(m_astBuilder, funcDeclRefExpr);
+ auto funcDeclRef = funcDeclRefExpr
+ ? getDeclRef(m_astBuilder, funcDeclRefExpr)
+ : DeclRef<Decl>();
if (funcDeclRef)
{
auto knownBuiltinAttr =
diff --git a/tests/diagnostics/implicit-cast-lvalue.slang.expected b/tests/diagnostics/implicit-cast-lvalue.slang.expected
index 93217b239..10ebfc656 100644
--- a/tests/diagnostics/implicit-cast-lvalue.slang.expected
+++ b/tests/diagnostics/implicit-cast-lvalue.slang.expected
@@ -7,7 +7,6 @@ tests/diagnostics/implicit-cast-lvalue.slang(19): error 30047: argument passed t
a(y);
^
tests/diagnostics/implicit-cast-lvalue.slang(19): note 30063: argument was implicitly cast from 'double' to 'float', and Slang does not support using an implicit cast as an l-value with this type
-tests/diagnostics/implicit-cast-lvalue.slang(19): note 30049: attempting to assign to a const variable or immutable member; use '[mutating]' attribute on the containing method to allow modification
}
standard output = {
}
diff --git a/tests/diagnostics/lvalue-in-bwd-diff.slang b/tests/diagnostics/lvalue-in-bwd-diff.slang
new file mode 100644
index 000000000..2e12bf5c0
--- /dev/null
+++ b/tests/diagnostics/lvalue-in-bwd-diff.slang
@@ -0,0 +1,23 @@
+//TEST:SIMPLE(filecheck=CHECK): -target spirv
+[Differentiable]
+float square(float x, float y)
+{
+ return x * x + y * y;
+}
+
+void main()
+{
+ // Forward mode differentiation
+ let x = diffPair(3.0, 1.0); // x = 3, 𝜕x/𝜕𝛉 = 1
+ let y = diffPair(4.0, 1.0); // y = 4, 𝜕y/𝜕𝛄 = 1
+ let result = fwd_diff(square)(x, y);
+ printf("dResult: %f\n", result.d);
+
+ // Backward mode differentiation
+ let dLdSquare = 1.0f;
+ // CHECK-NOT: error 30049
+ // CHECK: error 30047
+ // CHECK-NOT: error 30049
+ bwd_diff(square)(x, y, dLdSquare);
+ printf("dL/dx: %f, dL/dy: %f\n", x.d, y.d);
+}
diff --git a/tests/diagnostics/methods/mutating-method-on-rvalue.slang.expected b/tests/diagnostics/methods/mutating-method-on-rvalue.slang.expected
index d9694276b..066c8439d 100644
--- a/tests/diagnostics/methods/mutating-method-on-rvalue.slang.expected
+++ b/tests/diagnostics/methods/mutating-method-on-rvalue.slang.expected
@@ -7,7 +7,6 @@ tests/diagnostics/methods/mutating-method-on-rvalue.slang(13): note 30049: attem
tests/diagnostics/methods/mutating-method-on-rvalue.slang(25): error 30050: mutating method 'increment' cannot be called on an immutable value
gCounter.increment();
^
-tests/diagnostics/methods/mutating-method-on-rvalue.slang(25): note 30049: attempting to assign to a const variable or immutable member; use '[mutating]' attribute on the containing method to allow modification
}
standard output = {
}