Skip to content

Commit 20b02c0

Browse files
committed
Fix: allocate yieldFromGenerator local eagerly
1 parent 273e7ab commit 20b02c0

File tree

6 files changed

+18
-13
lines changed

6 files changed

+18
-13
lines changed

graalpython/com.oracle.graal.python.pegparser.test/testData/parser/goldenFiles/YieldStatementTests/customIter01.scope

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ScopeEnvironment
2727
StopIteration: [Use, GlobalImplicit]
2828
self: [DefParam, Local]
2929
Scope gen Function
30-
Flags: [IsNested, HasFreeVars, IsGenerator]
30+
Flags: [IsNested, HasFreeVars, IsGenerator, IsGeneratorWithYieldFrom]
3131
Symbols:
3232
MyIter: [Use, Free]
33-
ret: [DefLocal, DefNonLocal, Free]
33+
ret: [DefLocal, DefNonLocal, Free]

graalpython/com.oracle.graal.python.pegparser.test/testData/parser/goldenFiles/YieldStatementTests/yield12.scope

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ ScopeEnvironment
33
Symbols:
44
f: [DefLocal, Local]
55
Scope f Function
6-
Flags: [IsGenerator]
6+
Flags: [IsGenerator, IsGeneratorWithYieldFrom]

graalpython/com.oracle.graal.python.pegparser.test/testData/parser/goldenFiles/YieldStatementTests/yield13.scope

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ ScopeEnvironment
33
Symbols:
44
f: [DefLocal, Local]
55
Scope f Function
6-
Flags: [IsGenerator]
6+
Flags: [IsGenerator, IsGeneratorWithYieldFrom]
77
Symbols:
8-
f: [Use, GlobalImplicit]
8+
f: [Use, GlobalImplicit]

graalpython/com.oracle.graal.python.pegparser/src/com/oracle/graal/python/pegparser/scope/Scope.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ enum ScopeFlags {
119119
NeedsClassClosure,
120120
NeedsClassDict,
121121
IsVisitingIterTarget,
122-
CanSeeClassScope
122+
CanSeeClassScope,
123+
IsGeneratorWithYieldFrom,
123124
}
124125

125126
EnumSet<ScopeFlags> flags = EnumSet.noneOf(ScopeFlags.class);
@@ -243,6 +244,10 @@ public boolean isGenerator() {
243244
return flags.contains(ScopeFlags.IsGenerator);
244245
}
245246

247+
public boolean isGeneratorWithYieldFrom() {
248+
return flags.contains(ScopeFlags.IsGeneratorWithYieldFrom);
249+
}
250+
246251
public boolean isCoroutine() {
247252
return flags.contains(ScopeFlags.IsCoroutine);
248253
}

graalpython/com.oracle.graal.python.pegparser/src/com/oracle/graal/python/pegparser/scope/ScopeEnvironment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ public Void visit(ExprTy.YieldFrom node) {
920920
node.value.accept(this);
921921
}
922922
currentScope.flags.add(ScopeFlags.IsGenerator);
923+
currentScope.flags.add(ScopeFlags.IsGeneratorWithYieldFrom);
923924
if (currentScope.flags.contains(ScopeFlags.IsComprehension)) {
924925
throw raiseIfComprehensionBlock(node);
925926
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ public final class RootNodeCompiler implements BaseBytecodeDSLVisitor<BytecodeDS
238238

239239
// Mutable (must be reset)
240240
private SourceRange currentLocation;
241-
boolean savedYieldFromGenerator;
242241
BytecodeLocal yieldFromGenerator;
243242
boolean inExceptStar;
244243

@@ -452,7 +451,7 @@ flags, orderedTruffleStringArray(names),
452451
null,
453452
nodes);
454453
rootNode.setMetadata(codeUnit, ctx.errorCallback);
455-
if (codeUnit.isGeneratorOrCoroutine() && savedYieldFromGenerator) {
454+
if (codeUnit.isCoroutine() || codeUnit.isAsyncGenerator() || scope.isGeneratorWithYieldFrom()) {
456455
rootNode.yieldFromGeneratorIndex = yieldFromGenerator.getLocalIndex();
457456
}
458457

@@ -595,7 +594,6 @@ public void reset() {
595594
currentLocation = null;
596595
currentSaveExceptionLocal = null;
597596
prevSaveExceptionLocal = null;
598-
this.savedYieldFromGenerator = false;
599597
this.inExceptStar = false;
600598
}
601599

@@ -1608,6 +1606,10 @@ public void setUpFrame(ArgumentsTy args, Builder b) {
16081606
if (scope.isCoroutine() || scope.isGenerator()) {
16091607
generatorExceptionStateLocal = b.createLocal();
16101608
}
1609+
1610+
if (scope.isGeneratorWithYieldFrom() || scope.isCoroutine()) {
1611+
yieldFromGenerator = b.createLocal();
1612+
}
16111613
}
16121614

16131615
private void copyArguments(ArgumentsTy args, Builder b) {
@@ -2728,10 +2730,7 @@ public void emitYieldFrom(Runnable generatorOrCoroutineProducer, BytecodeLocal r
27282730
generatorOrCoroutineProducer.run();
27292731
b.endStoreLocal();
27302732

2731-
if (!savedYieldFromGenerator) {
2732-
savedYieldFromGenerator = true;
2733-
yieldFromGenerator = b.createLocal();
2734-
}
2733+
assert yieldFromGenerator != null;
27352734
b.beginStoreLocal(yieldFromGenerator);
27362735
b.emitLoadLocal(generator);
27372736
b.endStoreLocal();

0 commit comments

Comments
 (0)