diff options
| author | Theresa Foley <10618364+tangent-vector@users.noreply.github.com> | 2022-09-20 12:37:33 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-20 12:37:33 -0700 |
| commit | 5ac7ba2c6d3405f1a59f4350c753ec990af8f6dc (patch) | |
| tree | 13aba4c8e57cd5cbe6e3859bea130a8091c0a13a /tests | |
| parent | 8e44968be297c0fa0ab00510a5e5922630d8c401 (diff) | |
Support partial inference of generic arguments (#2404)
A commonly requested feature is to be able to supply only some
of the arguments to a generic explicitly, while allowing the rest
to be inferred. A common example is a function that performs some
kind of conversion:
To convert<To, From>( From fromValue ) { .... }
A user would like to be able to call this operation like:
int i = convert<int>( 1.0f );
but the current Slang type checker requires all or none of the generic
arguments be supplied. Supplying all of the arguments is tedious:
int i = convert<int, float>( 1.0f );
In this case, the `float` type argument is redundant and could be
inferred from context. However, if the user tries to omit the generic
argument list:
int i = convert( 1.0f );
The current type-checker cannot infer the `int` type argument (even if
one might claim it *should* infer based on the desired result type).
This change adds support for the `convert<int>(...)` case, by allowing
a generic to be applied to a prefix of its explicit arguments, and then
inferring the remaining arguments from contextual information when that
"partially applied" generic is applied to value-level arguments.
Most of the changes are just plumbing: adding the notion of a partially
applied generic and then supporting them during overload resolution.
A single test case is included that covers the `convert`-style use case.
It is likely that more testing is needed to cover failure modes of this
feature.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/language-feature/generics/partial-generic-argument-inference.slang | 49 | ||||
| -rw-r--r-- | tests/language-feature/generics/partial-generic-argument-inference.slang.expected.txt | 5 |
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/language-feature/generics/partial-generic-argument-inference.slang b/tests/language-feature/generics/partial-generic-argument-inference.slang new file mode 100644 index 000000000..4ee50b88d --- /dev/null +++ b/tests/language-feature/generics/partial-generic-argument-inference.slang @@ -0,0 +1,49 @@ +//TEST(compute):COMPARE_COMPUTE: -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE: -vk -shaderobj -output-using-type + +// This test confirms that we can provide a subset of the required generic +// arguments to a generic function, and have the rest be inferred from the +// types of the value arguments. + +interface IConvertible +{ + __init( int value ); + + property value : int { get; } +} + +ToType convert< ToType : IConvertible, FromType : IConvertible >( FromType fromVal ) +{ + return ToType( fromVal.value*0x10 ); +} + +struct A : IConvertible +{ + int value; + __init(int v) { this.value = v; } +} + +struct B : IConvertible +{ + int value; + __init(int v) { this.value = v; } +} + + +int test( int value ) +{ + A a = A(value); + B b = convert<B>(a); + + return a.value + b.value; +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<int> outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(int3 dispatchThreadID : SV_DispatchThreadID) +{ + int tid = dispatchThreadID.x; + outputBuffer[tid] = test(tid); +} diff --git a/tests/language-feature/generics/partial-generic-argument-inference.slang.expected.txt b/tests/language-feature/generics/partial-generic-argument-inference.slang.expected.txt new file mode 100644 index 000000000..d7be6af66 --- /dev/null +++ b/tests/language-feature/generics/partial-generic-argument-inference.slang.expected.txt @@ -0,0 +1,5 @@ +type: int32_t +0 +17 +34 +51 |
