Skip to content
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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

KuechA
Copy link
Contributor

@KuechA KuechA commented Jan 31, 2025

Fixes #2005 by

  • Entering a LocalScope for list, set, dict comprehensions and generators
  • Generating a VariableDeclaration inside this scope in the PythonAddDeclarationsPass for the variables.
  • Adds test that the local variables are not accessible outside the comprehension and that the refersTo edges are set

Copy link

codecov bot commented Jan 31, 2025

Codecov Report

Attention: Patch coverage is 84.61538% with 4 lines in your changes missing coverage. Please review.

Project coverage is 78.07%. Comparing base (bc4d52b) to head (d75f3f3).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
.../main/kotlin/de/fraunhofer/aisec/cpg/graph/Node.kt 60.00% 0 Missing and 2 partials ⚠️
...ofer/aisec/cpg/passes/PythonAddDeclarationsPass.kt 83.33% 0 Missing and 2 partials ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
...ain/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt 82.70% <100.00%> (+0.05%) ⬆️
...otlin/de/fraunhofer/aisec/cpg/graph/TypeBuilder.kt 56.41% <100.00%> (+2.56%) ⬆️
...er/aisec/cpg/frontends/python/ExpressionHandler.kt 83.04% <100.00%> (ø)
.../main/kotlin/de/fraunhofer/aisec/cpg/graph/Node.kt 86.17% <60.00%> (-1.48%) ⬇️
...ofer/aisec/cpg/passes/PythonAddDeclarationsPass.kt 89.65% <83.33%> (-1.02%) ⬇️

... and 4 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

* 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 {
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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

@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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."

Copy link
Contributor Author

@KuechA KuechA Jan 31, 2025

Choose a reason for hiding this comment

The 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 VariableDeclaration inside this local scope for the variable of the comprehension expression and this is done inside the PythonAddDeclarationsPass. Leaving the scope earlier (i.e., right after generating the ComprehensionExpression) here won't impact on resolving variables, right?

Copy link
Member

Choose a reason for hiding this comment

The 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.

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.

Actually, we're only generating a VariableDeclaration inside this local scope for the variable of the comprehension expression and this is done inside the PythonAddDeclarationsPass. Leaving the scope earlier (i.e., right after generating the ComprehensionExpression) here won't impact on resolving variables, right?

I would assume so

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my understanding, the variable should be available only in the LocalScope tied to the ComprehensionExpression. Obviously, we can always enter this scope to incorrectly resolve a variable but this shouldn't be the case. I also added a test which simulates the test case he provided in the original comment.

@oxisto oxisto added the python label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python comprehensions should open a local scope and set/infer VariableDeclarations for the variables
2 participants