From 898b4593ce3d519d4c3d98f9fe99d4f1cb3d6bb6 Mon Sep 17 00:00:00 2001 From: "Thomas E. Enebo" Date: Wed, 16 Apr 2025 10:29:15 -0500 Subject: [PATCH 1/2] Add spec for capture/non-capture for `it` in blocks --- spec/ruby/language/block_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/ruby/language/block_spec.rb b/spec/ruby/language/block_spec.rb index 75c1e71bc2c..45a5bd1acf0 100644 --- a/spec/ruby/language/block_spec.rb +++ b/spec/ruby/language/block_spec.rb @@ -1102,3 +1102,11 @@ def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, ** end end end + +describe "`it` is a captured variable in a block if `it` is outside" do + ruby_version_is "3.3"..."3.4" do + proc { it }.call(0).should eq(0) + it = 5 + proc { it }.call(0).should eq(5) + end +end From 7c9822a85d2702f437dd1f2f11cc97402487d33f Mon Sep 17 00:00:00 2001 From: "Thomas E. Enebo" Date: Wed, 16 Apr 2025 10:29:45 -0500 Subject: [PATCH 2/2] Blocks with no args must look for captured variable --- core/src/main/java/org/jruby/parser/RubyParserBase.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/jruby/parser/RubyParserBase.java b/core/src/main/java/org/jruby/parser/RubyParserBase.java index a4a6bd2ca13..2f931a48ce2 100644 --- a/core/src/main/java/org/jruby/parser/RubyParserBase.java +++ b/core/src/main/java/org/jruby/parser/RubyParserBase.java @@ -399,9 +399,11 @@ public Node declareIdentifier(ByteList byteName) { node = new LocalVarNode(lexer.tokline, slot, name); } else if (dyna_in_block() && id.equals("it")) { if (!hasArguments()) { - slot = currentScope.addVariable(id); + int existing = currentScope.isDefined(id); + slot = existing == -1 ? + currentScope.addVariable(id) : existing; node = new DVarNode(lexer.tokline, slot, name); - set_it_id(node); + if (existing == -1) set_it_id(node); } else { slot = currentScope.isDefined(id); // A special it cannot exist without being marked as a special it.