diff options
| author | Yong He <yonghe@outlook.com> | 2024-09-05 11:24:19 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-05 11:24:19 -0700 |
| commit | d655302465457c5d3285ae5339201a0769cc38dc (patch) | |
| tree | 4c0946ba4ea4879831133370d2203f569c135c35 /tests/language-feature | |
| parent | a88055c6f5190ca62bb4aa853b4f0fa11546278f (diff) | |
Support `where` clause and type equality constraint. (#4986)
* Support `where` clause.
* Fix.
* Fix parser.
* Enhance test to cover traditional __generic syntax.
* Update user-guide.
* Support `where` clause on associatedtype.
* Fix.
* Put in more comments.
Diffstat (limited to 'tests/language-feature')
| -rw-r--r-- | tests/language-feature/generics/where-1.slang | 20 | ||||
| -rw-r--r-- | tests/language-feature/generics/where-2.slang | 33 | ||||
| -rw-r--r-- | tests/language-feature/generics/where-3.slang | 36 | ||||
| -rw-r--r-- | tests/language-feature/generics/where-4.slang | 52 |
4 files changed, 141 insertions, 0 deletions
diff --git a/tests/language-feature/generics/where-1.slang b/tests/language-feature/generics/where-1.slang new file mode 100644 index 000000000..904e03d52 --- /dev/null +++ b/tests/language-feature/generics/where-1.slang @@ -0,0 +1,20 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj -output-using-type + +// Test that we can use `where` clause to constrain generic type parameters. + +T process<T, int N>(vector<T, N> v) where T : IFloat +{ + return v[0]; +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +[numthreads(1,1,1)] +void computeMain() +{ + float a = 1.0; + outputBuffer[0] = process(a); + // CHECK: 1.0 +} diff --git a/tests/language-feature/generics/where-2.slang b/tests/language-feature/generics/where-2.slang new file mode 100644 index 000000000..b3e9bb86a --- /dev/null +++ b/tests/language-feature/generics/where-2.slang @@ -0,0 +1,33 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj -output-using-type + +// Test that we can use `where` clause to constrain associatedtype of generic type parameters. + +interface IFoo +{ + associatedtype TA; +} + +struct FooImpl : IFoo +{ + typealias TA = int; +} + +__generic<typename T> +T.TA process(T v) + where T : IFoo + where T.TA == int +{ + return 1; +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +[numthreads(1,1,1)] +void computeMain() +{ + FooImpl fooImpl; + outputBuffer[0] = process(fooImpl); + // CHECK: 1.0 +} diff --git a/tests/language-feature/generics/where-3.slang b/tests/language-feature/generics/where-3.slang new file mode 100644 index 000000000..730373b76 --- /dev/null +++ b/tests/language-feature/generics/where-3.slang @@ -0,0 +1,36 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj -output-using-type + +// Test that we can use `where` clause to constrain the type of a type pack. + +interface IFoo +{ + associatedtype TA; +} + +struct FooImpl : IFoo +{ + typealias TA = int; +} + +void add(inout int a, int b) +{ + a += b; +} + +int process<each T>(T v) where T == int +{ + int result = 0; + expand add(result, each v); + return result; +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +[numthreads(1,1,1)] +void computeMain() +{ + outputBuffer[0] = process(1,2,3); + // CHECK: 6.0 +} diff --git a/tests/language-feature/generics/where-4.slang b/tests/language-feature/generics/where-4.slang new file mode 100644 index 000000000..9f86ca7dc --- /dev/null +++ b/tests/language-feature/generics/where-4.slang @@ -0,0 +1,52 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj -output-using-type + +// Test that we can use `where` clause to constrain an associatedtype. + +interface IBar +{ + associatedtype TB; + TB get(); +} + +interface IFoo +{ + associatedtype TA : IBar where TA.TB == int; + + TA getVal(); +} + +struct BarImpl : IBar +{ + typealias TB = int; + int x; + int get() { return x; } +} + +struct FooImpl : IFoo +{ + typealias TA = BarImpl; + TA getVal() { TA a; a.x = 1; return a; } +} + +int helper<T : IFoo>(T foo) +{ + // foo.getVal().get() has type `T.TA.TB`, + // because there is a type equality constraint defined on + // `IFoo.TA` such that `IFoo::TA.TB == int`, we should be able + // to conclude that `T.TA.TB` is `int` and the `return` here + // should type check. + return foo.getVal().get(); +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +[numthreads(1,1,1)] +void computeMain() +{ + FooImpl foo; + + outputBuffer[0] = helper(foo); + // CHECK: 1.0 +} |
