Skip to content

Commit 9efd959

Browse files
committed
Fix substitution for associatedtype.
fixes shader-slang#341 When a typedef definition is used to satisfy an associated type, we must also substitute the resulting typedef type using parent substitution, in the case that the typedef is a generic application.
1 parent 6924239 commit 9efd959

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

source/slang/syntax.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ void Type::accept(IValVisitor* visitor, void* extra)
503503
if (aggTypeDeclRef.getDecl()->memberDictionary.TryGetValue(assocTypeDecl->getName(), targetType))
504504
{
505505
if (auto typeDefDecl = dynamic_cast<TypeDefDecl*>(targetType))
506-
return typeDefDecl->type.type;
506+
return typeDefDecl->type.type->Substitute(aggTypeDeclRef.substitutions);
507507
else
508508
return DeclRefType::Create(getSession(), DeclRef<Decl>(targetType, aggTypeDeclRef.substitutions));
509509
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//TEST(compute):COMPARE_COMPUTE:-xslang -use-ir
2+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out
3+
4+
// Test type checking of associatedtype and typedef
5+
6+
RWStructuredBuffer<float> outputBuffer;
7+
8+
interface IBase
9+
{
10+
associatedtype SubTypeT;
11+
associatedtype RetT;
12+
RetT getVal(SubTypeT t);
13+
SubTypeT setVal(RetT v);
14+
}
15+
16+
struct SubType<T>
17+
{
18+
T x;
19+
};
20+
21+
struct GenStruct<T> : IBase
22+
{
23+
typedef T RetT;
24+
typedef SubType<RetT> SubTypeT;
25+
SubTypeT setVal(T val)
26+
{
27+
SubTypeT rs;
28+
rs.x = val;
29+
return rs;
30+
}
31+
T getVal(SubTypeT v)
32+
{
33+
return v.x;
34+
}
35+
};
36+
37+
U.RetT test<U:IBase>(U.RetT val)
38+
{
39+
U obj;
40+
U.SubTypeT sb = obj.setVal(val);
41+
return obj.getVal(sb);
42+
}
43+
44+
45+
[numthreads(4, 1, 1)]
46+
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
47+
{
48+
uint tid = dispatchThreadID.x;
49+
float inVal = float(tid);
50+
/* wrong error message for the following line */
51+
float outVal = test<GenStruct<float> >(inVal);
52+
outputBuffer[tid] = outVal.x;
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
3F800000
3+
40000000
4+
40400000

0 commit comments

Comments
 (0)