diff options
| author | Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> | 2023-08-15 15:24:18 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-15 15:24:18 -0400 |
| commit | 3d7552582bb4d4e330b1e90bcdced046cebd140c (patch) | |
| tree | fb1d3a91dbc3d423217cf4d897b033ce5700e64f | |
| parent | 00bd481e001e8c0b8008eaff5a38fa37963e6f99 (diff) | |
Fix bug with overload resolution under nested generics (#3107)
* Add test for generic param inference bug for nested generics
* Change description & simplify test
* Add expected file
* Check parent decl before unifying type parameters
| -rw-r--r-- | source/slang/slang-check-constraint.cpp | 10 | ||||
| -rw-r--r-- | tests/compute/generics-overload-2.slang | 38 | ||||
| -rw-r--r-- | tests/compute/generics-overload-2.slang.expected.txt | 4 |
3 files changed, 48 insertions, 4 deletions
diff --git a/source/slang/slang-check-constraint.cpp b/source/slang/slang-check-constraint.cpp index d66bf35cc..e7eccfaec 100644 --- a/source/slang/slang-check-constraint.cpp +++ b/source/slang/slang-check-constraint.cpp @@ -705,14 +705,16 @@ namespace Slang auto fstDeclRef = fstDeclRefType->getDeclRef(); if (auto typeParamDecl = as<GenericTypeParamDecl>(fstDeclRef.getDecl())) - return TryUnifyTypeParam(constraints, typeParamDecl, snd); + if (typeParamDecl->parentDecl == constraints.genericDecl) + return TryUnifyTypeParam(constraints, typeParamDecl, snd); if (auto sndDeclRefType = as<DeclRefType>(snd)) { auto sndDeclRef = sndDeclRefType->getDeclRef(); if (auto typeParamDecl = as<GenericTypeParamDecl>(sndDeclRef.getDecl())) - return TryUnifyTypeParam(constraints, typeParamDecl, fst); + if (typeParamDecl->parentDecl == constraints.genericDecl) + return TryUnifyTypeParam(constraints, typeParamDecl, fst); // can't be unified if they refer to different declarations. if (fstDeclRef.getDecl() != sndDeclRef.getDecl()) return false; @@ -816,7 +818,7 @@ namespace Slang if (auto typeParamDecl = as<GenericTypeParamDecl>(fstDeclRef.getDecl())) { - if(typeParamDecl->parentDecl == constraints.genericDecl ) + if(typeParamDecl->parentDecl == constraints.genericDecl) return TryUnifyTypeParam(constraints, typeParamDecl, snd); } } @@ -827,7 +829,7 @@ namespace Slang if (auto typeParamDecl = as<GenericTypeParamDecl>(sndDeclRef.getDecl())) { - if(typeParamDecl->parentDecl == constraints.genericDecl ) + if(typeParamDecl->parentDecl == constraints.genericDecl) return TryUnifyTypeParam(constraints, typeParamDecl, fst); } } diff --git a/tests/compute/generics-overload-2.slang b/tests/compute/generics-overload-2.slang new file mode 100644 index 000000000..8ca55ce7d --- /dev/null +++ b/tests/compute/generics-overload-2.slang @@ -0,0 +1,38 @@ +//TEST(smoke,compute):COMPARE_COMPUTE: -shaderobj +//TEST(smoke,compute):COMPARE_COMPUTE:-cpu -shaderobj + +// Test overload resolution for nested generic definitions + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + + +__generic<T : __BuiltinFloatingPointType> +struct Foo +{ + T test(uint index, T x) + { + return __realCast<T, float>(1.f); + } + + __generic<let N: int> + T test(vector<uint, N> index, T x) + { + return __realCast<T, float>(2.f); + } +}; + + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x + 2; + + Foo<float> obj; + + float outVal = obj.test(tid, 0.f); + outputBuffer[0] = outVal; // Expect: 1 + + float outVal2 = obj.test(uint2(tid, tid), 0.f); + outputBuffer[1] = outVal2; // Expect: 2 +}
\ No newline at end of file diff --git a/tests/compute/generics-overload-2.slang.expected.txt b/tests/compute/generics-overload-2.slang.expected.txt new file mode 100644 index 000000000..b7ad1e4a8 --- /dev/null +++ b/tests/compute/generics-overload-2.slang.expected.txt @@ -0,0 +1,4 @@ +3F800000 +40000000 +0 +0 |
