diff options
| author | Yong He <yonghe@outlook.com> | 2023-04-07 10:12:00 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-07 10:12:00 -0700 |
| commit | ea15647ba6bccb5ac48de5f4b80b8c2769d69b8f (patch) | |
| tree | 1c3568c821f1371afd15ad40507c6109377f452a | |
| parent | 0468cd0d1a8a1cff1d838a741b050ef11f6d7461 (diff) | |
Diagnose on attempt to specialize with interface type. (#2780)
* Diagnose on attempt to specialize with interface type.
Fixes ##1445.
* Enable fixed test.
* Fix test.
* Fix.
---------
Co-authored-by: Yong He <yhe@nvidia.com>
5 files changed, 110 insertions, 7 deletions
diff --git a/source/slang/slang-check-constraint.cpp b/source/slang/slang-check-constraint.cpp index f96b5a484..6e9d73b61 100644 --- a/source/slang/slang-check-constraint.cpp +++ b/source/slang/slang-check-constraint.cpp @@ -469,6 +469,16 @@ namespace Slang auto sub = getSub(m_astBuilder, constraintDeclRef); auto sup = getSup(m_astBuilder, constraintDeclRef); + if (sub->equals(sup)) + { + // We are trying to use an interface type itself to conform to the + // type constraint. We can reach this case when the user code does + // not provide an explicit type parameter to specialize a generic + // and the type parameter cannot be inferred from any arguments. + // In this case, we should fail the constraint check. + return SubstitutionSet(); + } + // Search for a witness that shows the constraint is satisfied. auto subTypeWitness = tryGetSubtypeWitness(sub, sup); if(subTypeWitness) diff --git a/tests/diagnostics/generic-type-inference-fail.slang b/tests/diagnostics/generic-type-inference-fail.slang new file mode 100644 index 000000000..803c7584c --- /dev/null +++ b/tests/diagnostics/generic-type-inference-fail.slang @@ -0,0 +1,81 @@ +//DIAGNOSTIC_TEST:SIMPLE: + +interface IAssoc +{ + int Compute(); +} + +interface IInterface +{ + associatedtype TAssoc : IAssoc; + + [mutating] + void SetVal(int inVal); + + TAssoc GetAssoc(); +}; + +T.TAssoc CreateT_Assoc_Inner<T:IInterface>(int inVal) +{ + T obj; + obj.SetVal(inVal); + return obj.GetAssoc(); +} + +T.TAssoc CreateT_Assoc<T:IInterface>(int inVal) +{ + return CreateT_Assoc_Inner<T>(inVal); +} + +T CreateT<T:IInterface>(int inVal) +{ + T obj; + obj.SetVal(inVal); + return obj; +} + +struct Impl : IInterface +{ + struct TAssoc : IAssoc + { + int base; + int Compute() + { + return base; + } + }; + + TAssoc assoc; + [mutating] + void SetVal(int inVal) + { + assoc.base = inVal; + } + + TAssoc GetAssoc() + { + return assoc; + } +}; + +int test() +{ + var obj = CreateT<Impl>(2); + var obj2 = CreateT_Assoc<Impl>(1); + + var obj3 = CreateT_Assoc_Inner(1); // ERROR. + + return obj.GetAssoc().Compute() + obj2.Compute() + obj3.Compute(); +} + + +//TEST_INPUT:ubuffer(data=[0 1 2 3], stride=4):out,name=outputBuffer +RWStructuredBuffer<int> outputBuffer : register(u0); + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + int outVal = test(); + outputBuffer[tid] = outVal; +}
\ No newline at end of file diff --git a/tests/diagnostics/generic-type-inference-fail.slang.expected b/tests/diagnostics/generic-type-inference-fail.slang.expected new file mode 100644 index 000000000..3f9753d68 --- /dev/null +++ b/tests/diagnostics/generic-type-inference-fail.slang.expected @@ -0,0 +1,9 @@ +result code = -1 +standard error = { +tests/diagnostics/generic-type-inference-fail.slang(66): error 39999: could not specialize generic for arguments of type (int) + var obj3 = CreateT_Assoc_Inner(1); // ERROR. + ^ +tests/diagnostics/generic-type-inference-fail.slang(18): note 39999: see declaration of func CreateT_Assoc_Inner<T>(int) -> T.TAssoc +} +standard output = { +} diff --git a/tests/experiments/generic/type-to-value-4.slang b/tests/experiments/generic/type-to-value-4.slang index 741b30791..8d768b54b 100644 --- a/tests/experiments/generic/type-to-value-4.slang +++ b/tests/experiments/generic/type-to-value-4.slang @@ -1,4 +1,4 @@ -//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj /* Test here is to try and associate a value with a type @@ -52,26 +52,25 @@ interface IGetE static Enum getE(); }; -public struct A : IGetE +struct A : IGetE { static Enum getE() { return Enum::A; } }; -public struct B : IGetE +struct B : IGetE { static Enum getE() { return Enum::B; } }; [numthreads(4, 1, 1)] void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) -{ - int index = dispatchThreadID.x; +{ // Err.. even though IGetE doesn't require an instanciation, not clear how to set it. So lets try with instanciation B b; IGetE g = b; let e = g.getE(); - + outputBuffer[dispatchThreadID.x] = int(e); -}
\ No newline at end of file +} diff --git a/tests/experiments/generic/type-to-value-4.slang.expected.txt b/tests/experiments/generic/type-to-value-4.slang.expected.txt new file mode 100644 index 000000000..98fb6a686 --- /dev/null +++ b/tests/experiments/generic/type-to-value-4.slang.expected.txt @@ -0,0 +1,4 @@ +1 +1 +1 +1 |
