-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a LocalScope
for CollectionComprehensions
and generate a Declaration
#2019
base: main
Are you sure you want to change the base?
Changes from 9 commits
3387229
209c218
1ff35b8
501d597
cb0b660
8b8fccd
c7893a6
110c288
8011422
9489ecf
091041a
d75f3f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,13 +99,25 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) : | |
} | ||
} | ||
|
||
/** | ||
* Works similar to [apply] but before executing [block], it enters the scope for this object | ||
* and afterward leaves the scope again. | ||
*/ | ||
private inline fun <T : Node> T.applyWithScope(block: T.() -> Unit): T { | ||
return this.apply { | ||
ctx?.scopeManager?.enterScope(this) | ||
block() | ||
ctx?.scopeManager?.leaveScope(this) | ||
} | ||
} | ||
|
||
/** | ||
* Translates a Python | ||
* [`GeneratorExp`](https://docs.python.org/3/library/ast.html#ast.GeneratorExp) into a | ||
* [CollectionComprehension]. | ||
*/ | ||
private fun handleGeneratorExp(node: Python.AST.GeneratorExp): CollectionComprehension { | ||
return newCollectionComprehension(rawNode = node).apply { | ||
return newCollectionComprehension(rawNode = node).applyWithScope { | ||
statement = handle(node.elt) | ||
comprehensionExpressions += node.generators.map { handleComprehension(it, node) } | ||
type = objectType("Generator") | ||
|
@@ -117,10 +129,10 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) : | |
* into a [CollectionComprehension]. | ||
*/ | ||
private fun handleListComprehension(node: Python.AST.ListComp): CollectionComprehension { | ||
return newCollectionComprehension(rawNode = node).apply { | ||
return newCollectionComprehension(rawNode = node).applyWithScope { | ||
statement = handle(node.elt) | ||
comprehensionExpressions += node.generators.map { handleComprehension(it, node) } | ||
type = objectType("list") // TODO: Replace this once we have dedicated types | ||
type = objectType("list") | ||
} | ||
} | ||
|
||
|
@@ -129,10 +141,10 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) : | |
* a [CollectionComprehension]. | ||
*/ | ||
private fun handleSetComprehension(node: Python.AST.SetComp): CollectionComprehension { | ||
return newCollectionComprehension(rawNode = node).apply { | ||
return newCollectionComprehension(rawNode = node).applyWithScope { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure, if this is really relevant in our case, but it seems that the exact definition is that only the control variable is not leaked into the outer scope, but I guess the only sane way to model this is exactly like you did by opening up a local scope for the comprehension. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, from https://peps.python.org/pep-0709/ -> "Comprehensions are currently compiled as nested functions, which provides isolation of the comprehension’s iteration variable, but is inefficient at runtime." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this sounds as if the current behavior would be appropriately modeled with the local scope but the intended/future behavior might be different. Actually, we're only generating a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, although I am not sure if this "new" bevahiour will leak the control variable or not. Maybe our PEP master @maximiliankaul understands this better than us.
I would assume so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my understanding, the variable should be available only in the |
||
this.statement = handle(node.elt) | ||
this.comprehensionExpressions += node.generators.map { handleComprehension(it, node) } | ||
this.type = objectType("set") // TODO: Replace this once we have dedicated types | ||
this.type = objectType("set") | ||
} | ||
} | ||
|
||
|
@@ -141,15 +153,15 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) : | |
* into a [CollectionComprehension]. | ||
*/ | ||
private fun handleDictComprehension(node: Python.AST.DictComp): CollectionComprehension { | ||
return newCollectionComprehension(rawNode = node).apply { | ||
return newCollectionComprehension(rawNode = node).applyWithScope { | ||
this.statement = | ||
newKeyValueExpression( | ||
key = handle(node.key), | ||
value = handle(node.value), | ||
rawNode = node, | ||
) | ||
this.comprehensionExpressions += node.generators.map { handleComprehension(it, node) } | ||
this.type = objectType("dict") // TODO: Replace this once we have dedicated types | ||
this.type = objectType("dict") | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a helpful bit, also for the future node builder API, should we move this to the scope manager? Or keep it intentionally private for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the same and wasn't sure where would be a good place. Maybe
Node
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When in doubt place it in
Extensions.kt
:D