summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2023-02-11 13:57:35 +0800
committerGitHub <noreply@github.com>2023-02-11 13:57:35 +0800
commit94b2c676662ef581b5de3baa6cde076fe07ef26b (patch)
tree8ee9bda481e9786ed05322deb72e9f65df11b249
parentc7f486c57cec2aae901dec6123fc57dfb5133f46 (diff)
Take into account existing initializer list type when performing coercions (#2641)
Fixes https://github.com/shader-slang/slang/issues/2189
-rw-r--r--source/slang/slang-check-conversion.cpp11
-rw-r--r--source/slang/slang-check-expr.cpp3
-rw-r--r--tests/bugs/c-style-cast-coerce.slang17
-rw-r--r--tests/bugs/c-style-cast-coerce.slang.expected8
-rw-r--r--tests/bugs/c-style-cast-overload.slang30
-rw-r--r--tests/bugs/c-style-cast-overload.slang.expected.txt4
6 files changed, 72 insertions, 1 deletions
diff --git a/source/slang/slang-check-conversion.cpp b/source/slang/slang-check-conversion.cpp
index 71231b9e5..abfe7afa0 100644
--- a/source/slang/slang-check-conversion.cpp
+++ b/source/slang/slang-check-conversion.cpp
@@ -502,6 +502,17 @@ namespace Slang
// TODO: we should handle the special case of `{0}` as an initializer
// for arbitrary `struct` types here.
+ // If this initializer list has a more specific type than just
+ // InitializerListType (i.e. it's already undergone a coercion) we
+ // should ensure that we're allowed to coerce from that type to our
+ // desired type.
+ // If this isn't prohibited, then we can proceed to try and coerce from
+ // the initializer list itself; assuming that coercion is closed under
+ // composition this shouldn't fail.
+ if(!as<InitializerListType>(fromInitializerListExpr->type) &&
+ !canCoerce(toType, fromInitializerListExpr->type, nullptr))
+ return _failedCoercion(toType, outToExpr, fromInitializerListExpr);
+
if(!_readAggregateValueFromInitializerList(toType, outToExpr, fromInitializerListExpr, argIndex))
return false;
diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp
index 7d546e60b..75a3c2ff1 100644
--- a/source/slang/slang-check-expr.cpp
+++ b/source/slang/slang-check-expr.cpp
@@ -2474,7 +2474,7 @@ namespace Slang
auto funcExpr = expr->functionExpr;
funcExpr = CheckTerm(funcExpr);
- // Now ensure that the term represnets a (proper) type.
+ // Now ensure that the term represents a (proper) type.
TypeExp typeExp;
typeExp.exp = funcExpr;
typeExp = CheckProperType(typeExp);
@@ -2544,6 +2544,7 @@ namespace Slang
// make this a major concern (since they aren't supported in HLSL).
//
InitializerListExpr* initListExpr = m_astBuilder->create<InitializerListExpr>();
+ initListExpr->loc = expr->loc;
auto checkedInitListExpr = visitInitializerListExpr(initListExpr);
return coerce(typeExp.type, checkedInitListExpr);
diff --git a/tests/bugs/c-style-cast-coerce.slang b/tests/bugs/c-style-cast-coerce.slang
new file mode 100644
index 000000000..e01903e2c
--- /dev/null
+++ b/tests/bugs/c-style-cast-coerce.slang
@@ -0,0 +1,17 @@
+//TEST:SIMPLE:
+
+// It used to be the case that coercions of already-coerced initializer lists
+// didn't take into account the more specific type. One way of triggering this
+// was to create an already-coerced initializer list via the HLSL `(S)0`
+// syntax which creates the equivalent of `{} : S`. In the example below this
+// then proceeds to be coerced to type T.
+
+struct S {};
+struct T {};
+
+void main()
+{
+ // Should generate a type error.
+ T t = (S)0;
+}
+
diff --git a/tests/bugs/c-style-cast-coerce.slang.expected b/tests/bugs/c-style-cast-coerce.slang.expected
new file mode 100644
index 000000000..a09c6842f
--- /dev/null
+++ b/tests/bugs/c-style-cast-coerce.slang.expected
@@ -0,0 +1,8 @@
+result code = -1
+standard error = {
+tests/bugs/c-style-cast-coerce.slang(15): error 30019: expected an expression of type 'T', got 'S'
+ T t = (S)0;
+ ^
+}
+standard output = {
+}
diff --git a/tests/bugs/c-style-cast-overload.slang b/tests/bugs/c-style-cast-overload.slang
new file mode 100644
index 000000000..058394271
--- /dev/null
+++ b/tests/bugs/c-style-cast-overload.slang
@@ -0,0 +1,30 @@
+// https://github.com/shader-slang/slang/issues/2189
+// See also ./c-style-cast-coerce.slang which represents the essence of this bug
+
+//TEST(compute):COMPARE_COMPUTE: -shaderobj
+//TEST(compute):COMPARE_COMPUTE: -shaderobj -vulkan
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+struct S {};
+
+int f(S)
+{
+ return 1;
+}
+
+int f(float)
+{
+ return 2;
+}
+
+[shader("compute")]
+[numthreads(1,1,1)]
+void computeMain()
+{
+ outputBuffer[0] = f(float(0));
+ outputBuffer[1] = f((float)1);
+ outputBuffer[2] = f((float)0);
+ outputBuffer[3] = f((S)0);
+}
diff --git a/tests/bugs/c-style-cast-overload.slang.expected.txt b/tests/bugs/c-style-cast-overload.slang.expected.txt
new file mode 100644
index 000000000..5b3c1b7c0
--- /dev/null
+++ b/tests/bugs/c-style-cast-overload.slang.expected.txt
@@ -0,0 +1,4 @@
+2
+2
+2
+1