@@ -929,6 +929,7 @@ private function processStmtNode(
929929 return new StatementResult ($ finalScope , $ bodyScopeResult ->hasYield (), $ alwaysTerminating , []);
930930 } elseif ($ stmt instanceof For_) {
931931 $ initScope = $ scope ;
932+ $ hasYield = false ;
932933 foreach ($ stmt ->init as $ initExpr ) {
933934 $ initScope = $ this ->processExprNode ($ initExpr , $ initScope , $ nodeCallback , ExpressionContext::createTopLevel ())->getScope ();
934935 }
@@ -955,8 +956,10 @@ private function processStmtNode(
955956 $ bodyScope = $ bodyScope ->mergeWith ($ continueExitPoint ->getScope ());
956957 }
957958 foreach ($ stmt ->loop as $ loopExpr ) {
958- $ bodyScope = $ this ->processExprNode ($ loopExpr , $ bodyScope , static function (): void {
959- }, ExpressionContext::createTopLevel ())->getScope ();
959+ $ exprResult = $ this ->processExprNode ($ loopExpr , $ bodyScope , static function (): void {
960+ }, ExpressionContext::createTopLevel ());
961+ $ bodyScope = $ exprResult ->getScope ();
962+ $ hasYield = $ hasYield || $ exprResult ->hasYield ();
960963 }
961964
962965 if ($ bodyScope ->equals ($ prevScope )) {
@@ -992,7 +995,12 @@ private function processStmtNode(
992995
993996 $ finalScope = $ finalScope ->mergeWith ($ scope );
994997
995- return new StatementResult ($ finalScope , $ finalScopeResult ->hasYield (), false /* $finalScopeResult->isAlwaysTerminating() && $isAlwaysIterable*/ , []);
998+ return new StatementResult (
999+ $ finalScope ,
1000+ $ finalScopeResult ->hasYield () || $ hasYield ,
1001+ false /* $finalScopeResult->isAlwaysTerminating() && $isAlwaysIterable*/ ,
1002+ []
1003+ );
9961004 } elseif ($ stmt instanceof Switch_) {
9971005 $ scope = $ this ->processExprNode ($ stmt ->cond , $ scope , $ nodeCallback , ExpressionContext::createDeep ())->getScope ();
9981006 $ scopeForBranches = $ scope ;
0 commit comments