-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix variable shadowing bug in interpreter #16335
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
Open
cyangle
wants to merge
16
commits into
crystal-lang:master
Choose a base branch
from
cyangle:fix_variable_shadowing_bug_in_interpreter_v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix variable shadowing bug in interpreter #16335
cyangle
wants to merge
16
commits into
crystal-lang:master
from
cyangle:fix_variable_shadowing_bug_in_interpreter_v2
+32
−11
Conversation
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
Contributor
|
We don't need to search in a range. We need to:
Which can be simplified into a couple method: lookup from and lookup at, with the from variant calling the at variant. For example: diff --git a/src/compiler/crystal/interpreter/compiler.cr b/src/compiler/crystal/interpreter/compiler.cr
index c40aa4ded..5e0521ce5 100644
--- a/src/compiler/crystal/interpreter/compiler.cr
+++ b/src/compiler/crystal/interpreter/compiler.cr
@@ -791,8 +791,9 @@ class Crystal::Repl::Compiler < Crystal::Visitor
end
def lookup_local_var_or_closured_var(name : String) : LocalVar | ClosuredVar
- lookup_local_var?(name) ||
+ lookup_local_var?(name, at: @block_level) ||
lookup_closured_var?(name) ||
+ lookup_local_var?(name, from: @block_level - 1) ||
raise("BUG: can't find closured var or local var #{name}")
end
@@ -800,19 +801,18 @@ class Crystal::Repl::Compiler < Crystal::Visitor
lookup_local_var?(name) || raise("BUG: can't find local var #{name}")
end
- def lookup_local_var?(name : String) : LocalVar?
- block_level = @block_level
- while block_level >= 0
- index = @local_vars.name_to_index?(name, block_level)
- if index
- type = @local_vars.type(name, block_level)
- return LocalVar.new(index, type)
+ def lookup_local_var?(name : String, *, from = @block_level) : LocalVar?
+ from.downto(0) do |block_level|
+ if local_var = lookup_local_var?(name, at: block_level)
+ return local_var
end
-
- block_level -= 1
end
+ end
- nil
+ def lookup_local_var?(name : String, *, at block_level : Int32) : LocalVar?
+ if index = @local_vars.name_to_index?(name, block_level)
+ LocalVar.new(index, @local_vars.type(name, block_level))
+ end
end
def lookup_closured_var(name : String) : ClosuredVar |
….com:cyangle/crystal into fix_variable_shadowing_bug_in_interpreter_v2
ysbaddaden
approved these changes
Nov 9, 2025
straight-shoota
approved these changes
Nov 21, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
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 #15489
Root Cause
The bug was caused by an incorrect variable lookup order in the Crystal interpreter. When a block argument, like parser, was used in a nested block, the compiler correctly identified it as a closure variable. However, the interpreter's lookup_local_var_or_closured_var method would search for a local variable in all scopes (from current to global) before searching the closure scope. This caused it to find the global parser variable first, leading to a type error, instead of finding the correct parser block argument from the closure.
The Fix
To resolve this, the variable lookup logic was reordered to correctly prioritize scopes. The lookup_local_var_or_closured_var method was modified to search in this order:
This ensures that block arguments and other local variables are found before any variables from outer or global scopes, correctly implementing variable shadowing and fixing the bug.
@HertzDevil Could you have a look at this?