From 2f690c05304c17c98bfdb344440c5f3ce5419570 Mon Sep 17 00:00:00 2001 From: Arkadiusz Kozdra Date: Tue, 10 Sep 2024 13:17:21 +0200 Subject: [PATCH] Fix foreach colliding index names (#5444) Signed-off-by: Arkadiusz Kozdra --- src/V3Randomize.cpp | 9 ++++++--- test_regress/t/t_constraint_foreach.v | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 1e8f9d320..96f4a6953 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -685,7 +685,7 @@ class ConstraintExprVisitor final : public VNVisitor { } VL_DO_DANGLING(nodep->deleteTree(), nodep); } - void visit(AstForeach* nodep) override {} + void visit(AstBegin* nodep) override {} void visit(AstConstraintForeach* nodep) override { // Convert to plain foreach FileLine* const fl = nodep->fileline(); @@ -708,8 +708,11 @@ class ConstraintExprVisitor final : public VNVisitor { nodep->replaceWith(new AstSFormatF{fl, "%@", false, newp}); } else { iterateAndNextNull(nodep->stmtsp()); - nodep->replaceWith(new AstForeach{fl, nodep->arrayp()->unlinkFrBack(), - nodep->stmtsp()->unlinkFrBackWithNext()}); + nodep->replaceWith( + new AstBegin{fl, "", + new AstForeach{fl, nodep->arrayp()->unlinkFrBack(), + nodep->stmtsp()->unlinkFrBackWithNext()}, + false, true}); } VL_DO_DANGLING(nodep->deleteTree(), nodep); } diff --git a/test_regress/t/t_constraint_foreach.v b/test_regress/t/t_constraint_foreach.v index 35ec42c69..fd243ebed 100644 --- a/test_regress/t/t_constraint_foreach.v +++ b/test_regress/t/t_constraint_foreach.v @@ -26,6 +26,8 @@ class C; x < 7; foreach(q[i]) x > i; + foreach(q[i]) // loop again with the same index name + x > i; }; endclass