Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/core/IronPython/Compiler/Ast/ForStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,26 @@ internal static MSAst.Expression TransformFor(ScopeStatement parent, MSAst.Param
)
);

// while enumerator.MoveNext():
// left = enumerator.Current
// iterator = enumerator.Key
// read once, because the interpreter calls a property getter of a struct through reflection
MSAst.ParameterExpression iterator = Ast.Variable(typeof(IEnumerator), "foreach_iterator");
MSAst.Expression initIterator = Ast.Assign(
iterator,
Ast.Property(
enumerator,
typeof(KeyValuePair<IEnumerator, IDisposable>).GetProperty("Key")
)
);

// while iterator.MoveNext():
// left = iterator.Current
// body
// else:
// else
MSAst.Expression ls = AstUtils.Loop(
parent.GlobalParent.AddDebugInfo(
Ast.Call(
Ast.Property(
enumerator,
typeof(KeyValuePair<IEnumerator, IDisposable>).GetProperty("Key")
),
iterator,
typeof(IEnumerator).GetMethod("MoveNext")
),
left.Span
Expand All @@ -95,10 +103,7 @@ internal static MSAst.Expression TransformFor(ScopeStatement parent, MSAst.Param
left.TransformSet(
SourceSpan.None,
Ast.Call(
Ast.Property(
enumerator,
typeof(KeyValuePair<IEnumerator, IDisposable>).GetProperty("Key")
),
iterator,
typeof(IEnumerator).GetProperty("Current").GetGetMethod()
),
PythonOperationKind.None
Expand All @@ -113,12 +118,15 @@ internal static MSAst.Expression TransformFor(ScopeStatement parent, MSAst.Param
);

return Ast.Block(
[iterator],
init,
initIterator,
Ast.TryFinally(
ls,
Ast.Block(
Ast.Call(AstMethods.ForLoopDispose, enumerator),
Ast.Assign(enumerator, Ast.New(typeof(KeyValuePair<IEnumerator, IDisposable>)))
Ast.Assign(enumerator, Ast.New(typeof(KeyValuePair<IEnumerator, IDisposable>))),
Ast.Assign(iterator, AstUtils.Constant(null, typeof(IEnumerator)))
)
)
);
Expand Down
Loading