yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeSupport `where` clause and type equality constraint. (#4986)d65530246

master
1.1 KiB52 linesraw
1//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj -output-using-type
2//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj -output-using-type
3
4// Test that we can use `where` clause to constrain an associatedtype.
5
6interface IBar
7{
8    associatedtype TB;
9    TB get();
10}
11
12interface IFoo
13{
14    associatedtype TA : IBar where TA.TB == int;
15
16    TA getVal();
17}
18
19struct BarImpl : IBar
20{
21    typealias TB = int;
22    int x;
23    int get() { return x; }
24}
25
26struct FooImpl : IFoo
27{
28    typealias TA = BarImpl;
29    TA getVal() { TA a; a.x = 1; return a; }
30}
31
32int helper<T : IFoo>(T foo)
33{
34    // foo.getVal().get() has type `T.TA.TB`,
35    // because there is a type equality constraint defined on
36    // `IFoo.TA` such that `IFoo::TA.TB == int`, we should be able
37    // to conclude that `T.TA.TB` is `int` and the `return` here
38    // should type check.
39    return foo.getVal().get();
40}
41
42//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
43RWStructuredBuffer<float> outputBuffer;
44
45[numthreads(1,1,1)]
46void computeMain()
47{
48    FooImpl foo;
49
50    outputBuffer[0] = helper(foo);
51    // CHECK: 1.0
52}