From 9d99976703872d7608415da5a90d070bd5dec5b7 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Fri, 17 Jan 2025 17:53:02 -0500 Subject: Fix circularity issue when checking multiple generic interface constraints. (#6121) * Fix circularity issue with checking multiple generic interface constraints * Create multi-generic-interface-constraint.slang * Update multi-generic-interface-constraint.slang * Update slang-check-inheritance.cpp --------- Co-authored-by: Yong He --- source/slang/slang-check-inheritance.cpp | 15 +++++++-- .../multi-generic-interface-constraint.slang | 36 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/language-feature/generics/multi-generic-interface-constraint.slang diff --git a/source/slang/slang-check-inheritance.cpp b/source/slang/slang-check-inheritance.cpp index 7af2d2e81..b44858874 100644 --- a/source/slang/slang-check-inheritance.cpp +++ b/source/slang/slang-check-inheritance.cpp @@ -466,10 +466,14 @@ InheritanceInfo SharedSemanticsContext::_calcInheritanceInfo( if (constraintDeclRef.getDecl()->checkState.isBeingChecked()) continue; - ensureDecl(&visitor, constraintDeclRef.getDecl(), DeclCheckState::CanSpecializeGeneric); + ensureDecl(&visitor, constraintDeclRef.getDecl(), DeclCheckState::ScopesWired); - auto subType = getSub(astBuilder, constraintDeclRef); - auto superType = getSup(astBuilder, constraintDeclRef); + // Check only the sub-type. + visitor.CheckConstraintSubType(constraintDeclRef.getDecl()->sub); + auto sub = constraintDeclRef.getDecl()->sub; + if (!sub.type) + sub = visitor.TranslateTypeNodeForced(sub); + auto subType = constraintDeclRef.substitute(astBuilder, sub.type); // We only consider constraints where the type represented // by `declRef` is the subtype, since those @@ -489,6 +493,11 @@ InheritanceInfo SharedSemanticsContext::_calcInheritanceInfo( if (subDeclRefType->getDeclRef() != declRef) continue; + // Further check the constraint, since we now need the sup-type. + ensureDecl(&visitor, constraintDeclRef.getDecl(), DeclCheckState::CanSpecializeGeneric); + + auto superType = getSup(astBuilder, constraintDeclRef); + // Because the constraint is a declared inheritance relationship, // adding the base to our list of direct bases is as straightforward // as in all the preceding cases. diff --git a/tests/language-feature/generics/multi-generic-interface-constraint.slang b/tests/language-feature/generics/multi-generic-interface-constraint.slang new file mode 100644 index 000000000..9c6918ed2 --- /dev/null +++ b/tests/language-feature/generics/multi-generic-interface-constraint.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 + +interface IFoo +{ + bool requirement(T v1); +} + +struct Bar, B : IFoo> +{ + [Differentiable] + T doThing(T a) + { + return a * T(2.0); + } +} + +struct Foo : IFoo +{ + bool requirement(float v) + { + return v > 0.5; + } +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +[numthreads(1, 1, 1)] +void computeMain(int3 dispatchThreadID : SV_DispatchThreadID) +{ + int tid = dispatchThreadID.x; + Bar obj; + outputBuffer[tid] = obj.doThing(2.0f); + // CHECK: 4 +} -- cgit v1.2.3