Skip to content

Commit 860cef1

Browse files
committed
Clarify condition for block_given? desugar
Checking the `enclosingBlockParamName` was really just a proxy for whether we were in a method or not. Even for methods with no explicit block parameter, `buildMethod()`, will add a synthetic `&<blk>`, so the if we're in a method, `enclosingBlockParamName` is always set by the time it's checked here.
1 parent a9789cf commit 860cef1

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

ast/desugar/Desugar.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ ExpressionPtr node2TreeImplBody(DesugarContext dctx, parser::Node *what) {
748748
flags.isPrivateOk = true;
749749
}
750750

751-
if (isCallToBlockGivenP(send, rec) && dctx.enclosingBlockParamName.exists()) {
751+
if (isCallToBlockGivenP(send, rec) && dctx.enclosingMethodLoc.exists()) {
752752
// Desugar:
753753
// def foo(&my_block)
754754
// x = block_given?

ast/desugar/PrismDesugar.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ ExpressionPtr node2TreeImplBody(DesugarContext dctx, parser::Node *what) {
732732
flags.isPrivateOk = true;
733733
}
734734

735-
if (isCallToBlockGivenP(send, rec) && dctx.enclosingBlockParamName.exists()) {
735+
if (isCallToBlockGivenP(send, rec) && dctx.enclosingMethodLoc.exists()) {
736736
// Desugar:
737737
// def foo(&my_block)
738738
// x = block_given?

test/testdata/desugar/block_given.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ def method_with_implicit_block_param()
5252
SomethingElse.block_given?
5353
block_given?("with parameter")
5454
end
55+
56+
# Outside of a method, none of these shuold desugar to special calls to `block_given?`
57+
block_given?
58+
self.block_given?
59+
Kernel.block_given?
60+
::Kernel.block_given?
61+
block_given? { "with a block?!" }
62+
Object.block_given? # Normally not allowed: private method `block_given?' called for Object:Class
63+
SomethingElse.block_given?
64+
block_given?("with parameter")

test/testdata/desugar/block_given.rb.desugar-tree.exp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,22 @@ class <emptyTree><<C <root>>> < (::<todo sym>)
113113
<self>.block_given?("with parameter")
114114
end
115115
end
116+
117+
<self>.block_given?()
118+
119+
<self>.block_given?()
120+
121+
<emptyTree>::<C Kernel>.block_given?()
122+
123+
::<root>::<C Kernel>.block_given?()
124+
125+
<self>.block_given?() do ||
126+
"with a block?!"
127+
end
128+
129+
<emptyTree>::<C Object>.block_given?()
130+
131+
<emptyTree>::<C SomethingElse>.block_given?()
132+
133+
<self>.block_given?("with parameter")
116134
end

0 commit comments

Comments
 (0)