From 605204374658fc6d7f647f9a57e9e322b8c83100 Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Wed, 29 Jan 2025 12:13:13 -0600 Subject: Fix the type coerce issue (#6215) * Fix the type coerce issue When synthesize the default ctor, if there is a base type we will synthesize an InvokeExpr to call base type's default ctor as well. But we should use the type of the inheritanceDecl instead of base struct decl. --- source/slang/slang-check-decl.cpp | 26 ++++++++++++++++------ .../initializer-lists/inheritance-generic.slang | 26 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 tests/language-feature/initializer-lists/inheritance-generic.slang diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index d0c3e8d96..d12374d52 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -324,13 +324,17 @@ struct SemanticsDeclBodyVisitor : public SemanticsDeclVisitorBase, StructDecl* parent = nullptr; ConstructorDecl* defaultCtor = nullptr; List ctorList; + Type* type = nullptr; DeclAndCtorInfo() {} DeclAndCtorInfo( ASTBuilder* m_astBuilder, SemanticsVisitor* visitor, - StructDecl* parent, + StructDecl* inParent, + Type* inType, const bool getOnlyDefault) { + parent = inParent; + type = inType; if (getOnlyDefault) defaultCtor = _getDefaultCtor(parent); else @@ -9073,19 +9077,22 @@ void SemanticsDeclBodyVisitor::synthesizeCtorBodyForBases( if (!declInfo.defaultCtor) continue; + auto declRefType = as(declInfo.type); + auto ctorToInvoke = m_astBuilder->create(); - ctorToInvoke->declRef = declInfo.defaultCtor->getDefaultDeclRef(); + ctorToInvoke->declRef = declRefType->getDeclRef(); ctorToInvoke->name = declInfo.defaultCtor->getName(); ctorToInvoke->loc = declInfo.defaultCtor->loc; - ctorToInvoke->type = m_astBuilder->getFuncType(ArrayView(), ctor->returnType.type); + ctorToInvoke->type = m_astBuilder->getFuncType(ArrayView(), declRefType); auto invoke = m_astBuilder->create(); invoke->functionExpr = ctorToInvoke; auto assign = m_astBuilder->create(); - assign->left = - coerce(CoercionSite::Initializer, declInfo.defaultCtor->returnType.type, thisExpr); + + assign->left = coerce(CoercionSite::Initializer, declRefType, thisExpr); assign->right = invoke; + auto stmt = m_astBuilder->create(); stmt->expression = assign; stmt->loc = ctor->loc; @@ -9195,9 +9202,14 @@ void SemanticsDeclBodyVisitor::visitAggTypeDecl(AggTypeDecl* aggTypeDecl) if (!structOfInheritance) continue; inheritanceDefaultCtorList.add( - DeclAndCtorInfo(m_astBuilder, this, structOfInheritance, true)); + DeclAndCtorInfo(m_astBuilder, this, structOfInheritance, declRefType, true)); } - DeclAndCtorInfo structDeclInfo = DeclAndCtorInfo(m_astBuilder, this, structDecl, false); + DeclAndCtorInfo structDeclInfo = DeclAndCtorInfo( + m_astBuilder, + this, + structDecl, + calcThisType(makeDeclRef(structDecl)), + false); // ensure all varDecl members are processed up to SemanticsBodyVisitor so we can be sure that if // init expressions of members are to be synthisised, they are. diff --git a/tests/language-feature/initializer-lists/inheritance-generic.slang b/tests/language-feature/initializer-lists/inheritance-generic.slang new file mode 100644 index 000000000..c916178b8 --- /dev/null +++ b/tests/language-feature/initializer-lists/inheritance-generic.slang @@ -0,0 +1,26 @@ +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER):-shaderobj -vk +//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER):-shaderobj + +struct Base +{ + int a = 1; +} + +struct Derived: Base +{ + bool x; + bool y; +} + +//TEST_INPUT:ubuffer(data=[0], stride=4):out,name=result +RWStructuredBuffer result; + +[shader("compute")] +[numthreads(1, 1, 1)] +void computeMain() +{ + Derived<3> d; + + // BUFFER: 1 + result[0] = d.a; +} -- cgit v1.2.3