@@ -60,6 +60,15 @@ public override Ast Reduce() {
6060 res
6161 ) ;
6262 }
63+
64+ internal ComprehensionScope Scope { get ; private protected set ; }
65+
66+ internal Comprehension CopyForRewrite ( ComprehensionScope scope ) {
67+ var newComprehension = ( Comprehension ) MemberwiseClone ( ) ;
68+ newComprehension . Scope = scope ;
69+ newComprehension . Parent = scope . Parent ;
70+ return newComprehension ;
71+ }
6372 }
6473
6574 public sealed class ListComprehension : Comprehension {
@@ -106,10 +115,8 @@ public override void Walk(PythonWalker walker) {
106115 }
107116 walker . PostWalk ( this ) ;
108117 }
109-
110- internal ComprehensionScope Scope { get ; }
111118 }
112-
119+
113120 public sealed class SetComprehension : Comprehension {
114121 private readonly ComprehensionIterator [ ] _iterators ;
115122
@@ -154,8 +161,6 @@ public override void Walk(PythonWalker walker) {
154161 }
155162 walker . PostWalk ( this ) ;
156163 }
157-
158- internal ComprehensionScope Scope { get ; }
159164 }
160165
161166 public sealed class DictionaryComprehension : Comprehension {
@@ -207,19 +212,17 @@ public override void Walk(PythonWalker walker) {
207212 }
208213 walker . PostWalk ( this ) ;
209214 }
210-
211- internal ComprehensionScope Scope { get ; }
212215 }
213216
214217 /// <summary>
215218 /// Scope for the comprehension. Because scopes are usually statements and comprehensions are expressions
216219 /// this doesn't actually show up in the AST hierarchy and instead hangs off the comprehension expression.
217220 /// </summary>
218221 internal class ComprehensionScope : ScopeStatement {
219- private readonly Expression _comprehension ;
222+ private readonly Comprehension _comprehension ;
220223 private static readonly MSAst . ParameterExpression _compContext = Ast . Parameter ( typeof ( CodeContext ) , "$compContext" ) ;
221224
222- public ComprehensionScope ( Expression comprehension ) {
225+ public ComprehensionScope ( Comprehension comprehension ) {
223226 _comprehension = comprehension ;
224227 }
225228
@@ -229,12 +232,12 @@ internal override bool ExposesLocalVariable(PythonVariable variable) {
229232 } else if ( variable . Scope == this ) {
230233 return false ;
231234 }
232- return _comprehension . Parent . ExposesLocalVariable ( variable ) ;
235+ return Parent . ExposesLocalVariable ( variable ) ;
233236 }
234237
235238 internal override MSAst . Expression /*!*/ GetParentClosureTuple ( ) {
236239 Debug . Assert ( NeedsLocalContext ) ;
237- return MSAst . Expression . Call ( null , typeof ( PythonOps ) . GetMethod ( nameof ( PythonOps . GetClosureTupleFromContext ) ) , _comprehension . Parent . LocalContext ) ;
240+ return MSAst . Expression . Call ( null , typeof ( PythonOps ) . GetMethod ( nameof ( PythonOps . GetClosureTupleFromContext ) ) , Parent . LocalContext ) ;
238241 }
239242
240243 internal override bool TryBindOuter ( ScopeStatement from , PythonReference reference , out PythonVariable variable ) {
@@ -269,7 +272,7 @@ internal override PythonVariable BindReference(PythonNameBinder binder, PythonRe
269272 }
270273
271274 // then bind in our parent scope
272- return _comprehension . Parent . BindReference ( binder , reference ) ;
275+ return Parent . BindReference ( binder , reference ) ;
273276 }
274277
275278 internal override Ast GetVariableExpression ( PythonVariable variable ) {
@@ -281,21 +284,23 @@ internal override Ast GetVariableExpression(PythonVariable variable) {
281284 return expr ;
282285 }
283286
284- return _comprehension . Parent . GetVariableExpression ( variable ) ;
287+ return Parent . GetVariableExpression ( variable ) ;
285288 }
286289
287290 internal override Microsoft . Scripting . Ast . LightLambdaExpression GetLambda ( )
288291 => throw new NotImplementedException ( ) ;
289292
290- public override void Walk ( PythonWalker walker ) => _comprehension . Walk ( walker ) ;
293+ public override void Walk ( PythonWalker walker ) {
294+ _comprehension . Walk ( walker ) ;
295+ }
291296
292297 internal override Ast LocalContext {
293298 get {
294299 if ( NeedsLocalContext ) {
295300 return _compContext ;
296301 }
297302
298- return _comprehension . Parent . LocalContext ;
303+ return Parent . LocalContext ;
299304 }
300305 }
301306
@@ -311,7 +316,7 @@ internal Ast AddVariables(Ast expression) {
311316 CreateVariables ( locals , body ) ;
312317
313318 if ( localContext != null ) {
314- var createLocal = CreateLocalContext ( _comprehension . Parent . LocalContext ) ;
319+ var createLocal = CreateLocalContext ( Parent . LocalContext ) ;
315320 body . Add ( Ast . Assign ( _compContext , createLocal ) ) ;
316321 }
317322
0 commit comments