Repository navigation
fix: stack overflow when a for loop's iterable references its own loop variable - #440
Open
garretfick wants to merge 1 commit into
Open
garretfick wants to merge 1 commit into
garretfick wants to merge 1 commit into
Conversation
…p variable (#1) * fix: stack overflow when a for loop's iterable references its own loop variable Fixes withered-magic#439. When collecting scopes, `for` statements declared their loop variables in the same scope in which the iterable expression had been collected, unlike assignments and comprehensions which allocate a fresh scope first. As a result, a name used in the iterable resolved to the loop variable itself. Inferring the type of the loop variable then required inferring the iterable, which required inferring the loop variable again, and so on until the stack overflowed. This is triggered by code such as: for idl in idls: for idl in idl.idl_files: ... which appears in rules_ros2's ament.bzl and crashed the server whenever that file was analyzed, e.g. after go-to-definition on `py_exec_launcher`. The iterable is evaluated before the loop variables are bound, so allocate a new scope for the targets. Names in the iterable now resolve to the enclosing definition (or report "not defined" if there is none). Additionally, add a re-entrancy guard to `infer_source_expr_assign` so that any remaining cycle through an assignment source bails out with `Unknown` instead of recursing indefinitely. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VWwYHECoSdkTj6bkAdt6xQ * test: cover for-loop iterable shadowing with code flow analysis enabled Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VWwYHECoSdkTj6bkAdt6xQ --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #439.
When collecting scopes,
forstatements declared their loop variables in the same scope in which the iterable expression had been collected, unlike assignments and comprehensions which allocate a fresh scope first. As a result, a name used in the iterable resolved to the loop variable itself. Inferring the type of the loop variable then required inferring the iterable, which required inferring the loop variable again, and so on until the stack overflowed. This is triggered by code such as:which appears in rules_ros2's ament.bzl and crashed the server whenever that file was analyzed, e.g. after go-to-definition on
py_exec_launcher.The iterable is evaluated before the loop variables are bound, so allocate a new scope for the targets. Names in the iterable now resolve to the enclosing definition (or report "not defined" if there is none).
Additionally, add a re-entrancy guard to
infer_source_expr_assignso that any remaining cycle through an assignment source bails out withUnknowninstead of recursing indefinitely.