summaryrefslogtreecommitdiff
path: root/source/slang/slang-check-decl.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-10-07 08:53:36 -0700
committerGitHub <noreply@github.com>2025-10-07 15:53:36 +0000
commit54e1d02715747ee81585bfd23e96a1f4956dbf66 (patch)
tree83dfc746883b6b7aee0018bb55d4aea3095400e8 /source/slang/slang-check-decl.cpp
parent0dac20642b971191256e058282811cf9307292e7 (diff)
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.
Diffstat (limited to 'source/slang/slang-check-decl.cpp')
-rw-r--r--source/slang/slang-check-decl.cpp47
1 files changed, 0 insertions, 47 deletions
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<typename VisitorType>
@@ -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<ConstructorDecl>())
- {
- ThisExpr* thisExpr = m_astBuilder->create<ThisExpr>();
- thisExpr->scope = ctor->ownedScope;
- thisExpr->type = ctor->returnType.type;
- auto seqStmtChild = m_astBuilder->create<SeqStmt>();
-
- for (auto varDeclBase : structDecl->getDirectMemberDeclsOfType<VarDeclBase>())
- {
- if (varDeclBase->hasModifier<HLSLStaticModifier>() ||
- varDeclBase->getName() == nullptr || varDeclBase->initExpr == nullptr)
- continue;
-
- auto assign = createMemberAssignmentExpr(
- thisExpr,
- ctor->ownedScope,
- varDeclBase,
- varDeclBase->initExpr);
- if (!assign)
- continue;
-
- auto stmt = m_astBuilder->create<ExpressionStmt>();
- 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<DeclAndCtorInfo>& inheritanceDefaultCtorList,
@@ -10063,7 +10017,6 @@ void SemanticsDeclBodyVisitor::visitAggTypeDecl(AggTypeDecl* aggTypeDecl)
}
synthesizeCtorBody(structDeclInfo, inheritanceDefaultCtorList, structDecl);
- maybeInsertDefaultInitExpr(structDecl);
if (structDeclInfo.defaultCtor)
{