From 406078e46b7e5ca8328631f43f1dfd4cf2401682 Mon Sep 17 00:00:00 2001 From: IDONTUSEGH Date: Thu, 5 Mar 2026 08:05:31 +0000 Subject: [PATCH] Revert "Do semantic under nested scope when returning inside foreach body (#22632)" This reverts commit ac92d7d5c35605566c9862c905dab8e3ddf61729. --- compiler/src/dmd/frontend.h | 1 - compiler/src/dmd/funcsem.d | 2 +- compiler/src/dmd/semantic3.d | 5 ++--- compiler/src/dmd/statement.d | 1 - compiler/src/dmd/statement.h | 1 - compiler/src/dmd/statementsem.d | 4 ---- compiler/test/runnable/foreach5.d | 31 ------------------------------- compiler/test/runnable/rvalue1.d | 24 ------------------------ 8 files changed, 3 insertions(+), 66 deletions(-) diff --git a/compiler/src/dmd/frontend.h b/compiler/src/dmd/frontend.h index c4439b55b655..745306803499 100644 --- a/compiler/src/dmd/frontend.h +++ b/compiler/src/dmd/frontend.h @@ -5182,7 +5182,6 @@ class ReturnStatement final : public Statement public: Expression* exp; size_t caseDim; - FuncDeclaration* fesFunc; ReturnStatement* syntaxCopy() override; ReturnStatement* endsWithReturnStatement() override; void accept(Visitor* v) override; diff --git a/compiler/src/dmd/funcsem.d b/compiler/src/dmd/funcsem.d index 31b6397ecf5a..cc2497a8924c 100644 --- a/compiler/src/dmd/funcsem.d +++ b/compiler/src/dmd/funcsem.d @@ -2780,7 +2780,7 @@ void buildResultVar(FuncDeclaration fd, Scope* sc, Type tret) if (sc && fd.vresult.semanticRun == PASS.initial) { TypeFunction tf = fd.type.toTypeFunction(); - if (fd.isNRVO || tf.isRef) + if (tf.isRef) fd.vresult.storage_class |= STC.ref_; else if (target.isReturnOnStack(tf, fd.needThis())) fd.vresult.nrvo = true; diff --git a/compiler/src/dmd/semantic3.d b/compiler/src/dmd/semantic3.d index 234782645487..077969b9b1fc 100644 --- a/compiler/src/dmd/semantic3.d +++ b/compiler/src/dmd/semantic3.d @@ -975,12 +975,11 @@ private extern(C++) final class Semantic3Visitor : Visitor if (funcdecl.vresult) { - Scope* scret = rs.fesFunc ? rs.fesFunc._scope : sc2; - // Create: return (vresult = exp, vresult); exp = new ConstructExp(rs.loc, funcdecl.vresult, exp); - exp = exp.expressionSemantic(scret); + exp.type = funcdecl.vresult.type; exp = Expression.combine(exp, new VarExp(rs.loc, funcdecl.vresult)); + exp = exp.expressionSemantic(sc2); if (rs.caseDim) exp = Expression.combine(exp, new IntegerExp(rs.caseDim)); diff --git a/compiler/src/dmd/statement.d b/compiler/src/dmd/statement.d index ff162aa79b72..0641c0a85ce1 100644 --- a/compiler/src/dmd/statement.d +++ b/compiler/src/dmd/statement.d @@ -1308,7 +1308,6 @@ extern (C++) final class ReturnStatement : Statement { Expression exp; size_t caseDim; - FuncDeclaration fesFunc; // nested function for foreach it is in extern (D) this(Loc loc, Expression exp) @safe { diff --git a/compiler/src/dmd/statement.h b/compiler/src/dmd/statement.h index 7074052fd2e0..d49f4e47dbed 100644 --- a/compiler/src/dmd/statement.h +++ b/compiler/src/dmd/statement.h @@ -518,7 +518,6 @@ class ReturnStatement final : public Statement public: Expression *exp; size_t caseDim; - FuncDeclaration *fesFunc; // nested function for foreach it is in ReturnStatement *syntaxCopy() override; diff --git a/compiler/src/dmd/statementsem.d b/compiler/src/dmd/statementsem.d index 2f7b442d3a59..4fc8ef9ecac0 100644 --- a/compiler/src/dmd/statementsem.d +++ b/compiler/src/dmd/statementsem.d @@ -2481,12 +2481,8 @@ Statement statementSemanticVisit(Statement s, Scope* sc) //printf("ReturnStatement.dsymbolSemantic() %s\n", toChars(rs)); FuncDeclaration fd = sc.parent.isFuncDeclaration(); - if (fd.fes) - { - rs.fesFunc = fd; fd = fd.fes.func; // fd is now function enclosing foreach - } auto tf = fd.type.isTypeFunction(); diff --git a/compiler/test/runnable/foreach5.d b/compiler/test/runnable/foreach5.d index 024db2a864cd..b0befcceb62d 100644 --- a/compiler/test/runnable/foreach5.d +++ b/compiler/test/runnable/foreach5.d @@ -1208,36 +1208,6 @@ void testLDC326() } } -/***************************************/ -// https://github.com/dlang/dmd/issues/22629 - -struct Collection2 -{ - this(ref Collection2) {} - - int opApply(int delegate(Collection2)) - { - return 0; - } -} - -Collection2 testForeach2(ref Collection2 level1, ref Collection2 level2) -{ - foreach (first; level1) { - foreach (second; level2) - return second; - } - - return Collection2(); -} - -void test22629() -{ - Collection2 c1, c2; - testForeach2(c1, c2); -} - - /***************************************/ int main() @@ -1271,7 +1241,6 @@ int main() test14653(); test17041(); testLDC326(); - test22629(); printf("Success\n"); return 0; diff --git a/compiler/test/runnable/rvalue1.d b/compiler/test/runnable/rvalue1.d index 78c50919899b..e50ccd083957 100644 --- a/compiler/test/runnable/rvalue1.d +++ b/compiler/test/runnable/rvalue1.d @@ -286,7 +286,6 @@ struct V12 { S12 s; this(int) { s = S12(1); } - int opApply(int delegate(Object)) { return 0; } } S12 foo12() @@ -294,33 +293,10 @@ S12 foo12() return __rvalue(V12(1).s); } -S12 bar12() -{ - S12 s = S12(1); // NRVO - foreach (_; V12(1)) - return s; - return s; -} - -S12 baz12() -{ - S12 s1 = S12(1); - S12 s2 = S12(1); // No NRVO - foreach (_; V12(1)) - return s1; - return s2; -} - void test12() { S12 s = foo12(); assert(&s == s.ptr); - - S12 s2 = bar12(); - assert(&s2 == s2.ptr); - - S12 s3 = baz12(); - assert(&s3 == s3.ptr); } /********************************/