From 54e1d02715747ee81585bfd23e96a1f4956dbf66 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 7 Oct 2025 08:53:36 -0700 Subject: Fix a bug that causes a struct field to be initialized twice. (#8619) We insert field initialization logic at the beginning of every ctor in `synthesizeCtorBody`, but then immediately inserts another round of initialization again for explicit ctors in `maybeInsertDefaultInitExpr`, both called from `SemanticsDeclBodyVisitor::visitAggTypeDecl` right next to each other. The fix is to remove `maybeInsertDefaultInitExpr`. This change also enhances the address aliasing analysis, so that for the following case: ``` this->member1 = 0; this->member2 = 0; this->member1 = param; ``` We can still remove the first assignment to `this->member1` despite seeing `this->member2=0`, since it is easy to know that `this->member2` cannot alias with `this->member1`. Closes #8600. --- source/slang/slang-check-decl.cpp | 47 --------------------------------------- 1 file changed, 47 deletions(-) (limited to 'source/slang/slang-check-decl.cpp') diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index fa31c54bd..4bfbde584 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -614,7 +614,6 @@ private: Decl* member, Expr* rightHandSideExpr); Expr* createCtorParamExpr(ConstructorDecl* ctor, Index paramIndex); - void maybeInsertDefaultInitExpr(StructDecl* structDecl); }; template @@ -9918,51 +9917,6 @@ void SemanticsDeclBodyVisitor::synthesizeCtorBodyForMemberVar( seqStmtChild->stmts.add(stmt); } -// This function inserts the default initialization expression for every member who -// has init expression at beginning of the user-defined ctor. We don't need to do this -// for synthesized ctor, because synthesized ctor already handle this at the function -// parameters -void SemanticsDeclBodyVisitor::maybeInsertDefaultInitExpr(StructDecl* structDecl) -{ - // If there is no explicit constructor, we don't need to do anything. - if (!_hasExplicitConstructor(structDecl, false)) - return; - - // traverse all the constructors and insert the default initialization expressions. - for (auto ctor : structDecl->getMembersOfType()) - { - ThisExpr* thisExpr = m_astBuilder->create(); - thisExpr->scope = ctor->ownedScope; - thisExpr->type = ctor->returnType.type; - auto seqStmtChild = m_astBuilder->create(); - - for (auto varDeclBase : structDecl->getDirectMemberDeclsOfType()) - { - if (varDeclBase->hasModifier() || - varDeclBase->getName() == nullptr || varDeclBase->initExpr == nullptr) - continue; - - auto assign = createMemberAssignmentExpr( - thisExpr, - ctor->ownedScope, - varDeclBase, - varDeclBase->initExpr); - if (!assign) - continue; - - auto stmt = m_astBuilder->create(); - stmt->expression = assign; - stmt->loc = varDeclBase->loc; - seqStmtChild->stmts.add(stmt); - } - if (seqStmtChild->stmts.getCount() != 0) - { - auto seqStmt = _ensureCtorBodyIsSeqStmt(m_astBuilder, ctor); - seqStmt->stmts.insert(0, seqStmtChild); - } - } -} - void SemanticsDeclBodyVisitor::synthesizeCtorBody( DeclAndCtorInfo& structDeclInfo, List& inheritanceDefaultCtorList, @@ -10063,7 +10017,6 @@ void SemanticsDeclBodyVisitor::visitAggTypeDecl(AggTypeDecl* aggTypeDecl) } synthesizeCtorBody(structDeclInfo, inheritanceDefaultCtorList, structDecl); - maybeInsertDefaultInitExpr(structDecl); if (structDeclInfo.defaultCtor) { -- cgit v1.2.3