Skip to content

Commit 0c3c96a

Browse files
committed
[GR-18163] Introduce primitives to warn in methods that accept a block
PullRequest: truffleruby/3652
2 parents a95249e + bb47827 commit 0c3c96a

File tree

7 files changed

+47
-14
lines changed

7 files changed

+47
-14
lines changed

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.truffleruby.builtins.NonStandard;
3838
import org.truffleruby.annotations.Primitive;
3939
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
40+
import org.truffleruby.builtins.PrimitiveNode;
4041
import org.truffleruby.core.array.ArrayUtils;
4142
import org.truffleruby.core.array.RubyArray;
4243
import org.truffleruby.core.basicobject.BasicObjectNodes.ObjectIDNode;
@@ -2024,4 +2025,36 @@ protected boolean setCategory(RubySymbol category, boolean newValue) {
20242025

20252026
}
20262027

2028+
@Primitive(name = "warn_given_block_not_used")
2029+
public abstract static class WarnGivenBlockNotUsedNode extends PrimitiveNode {
2030+
@Specialization
2031+
protected Object warn(
2032+
@Cached("new()") WarnNode warnNode) {
2033+
if (warnNode.shouldWarn()) {
2034+
warnNode.warningMessage(
2035+
getContext().getCallStack().getTopMostUserSourceSection(),
2036+
"given block not used");
2037+
}
2038+
2039+
return Nil.INSTANCE;
2040+
}
2041+
2042+
}
2043+
2044+
@Primitive(name = "warn_block_supersedes_default_value_argument")
2045+
public abstract static class WarnBlockSupersedesDefaultValueArgumentNode extends PrimitiveNode {
2046+
@Specialization
2047+
protected Object warn(
2048+
@Cached("new()") WarnNode warnNode) {
2049+
if (warnNode.shouldWarn()) {
2050+
warnNode.warningMessage(
2051+
getContext().getCallStack().getTopMostUserSourceSection(),
2052+
"block supersedes default value argument");
2053+
}
2054+
2055+
return Nil.INSTANCE;
2056+
}
2057+
2058+
}
2059+
20272060
}

src/main/ruby/truffleruby/core/array.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def combination(num)
304304
def count(item = undefined)
305305
seq = 0
306306
if !Primitive.undefined?(item)
307-
warn 'given block not used', uplevel: 1 if block_given?
307+
Primitive.warn_given_block_not_used if block_given?
308308
each { |o| seq += 1 if item == o }
309309
elsif block_given?
310310
each { |o| seq += 1 if yield(o) }
@@ -407,7 +407,7 @@ def fetch(idx, default=undefined)
407407

408408
if idx < 0 or idx >= size
409409
if block_given?
410-
warn 'block supersedes default value argument', uplevel: 1 unless Primitive.undefined?(default)
410+
Primitive.warn_block_supersedes_default_value_argument unless Primitive.undefined?(default)
411411

412412
return yield(orig)
413413
end
@@ -1012,7 +1012,7 @@ def rindex(obj=undefined)
10121012
i -= 1
10131013
end
10141014
else
1015-
warn 'given block not used', uplevel: 1 if block_given?
1015+
Primitive.warn_given_block_not_used if block_given?
10161016

10171017
i = size - 1
10181018
while i >= 0

src/main/ruby/truffleruby/core/enumerable.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def compact
128128
def count(item = undefined)
129129
seq = 0
130130
if !Primitive.undefined?(item)
131-
warn 'given block not used', uplevel: 1 if block_given?
131+
Primitive.warn_given_block_not_used if block_given?
132132
each do
133133
o = Primitive.single_block_arg
134134
seq += 1 if item == o
@@ -522,7 +522,7 @@ def inject(initial=undefined, sym=undefined, &block)
522522

523523
# inject(initial_operand, symbol) { ... }
524524
if block_given? && !Primitive.undefined?(sym)
525-
warn 'given block not used', uplevel: 1
525+
Primitive.warn_given_block_not_used
526526
end
527527

528528
if Primitive.undefined?(sym) && block_given?
@@ -563,7 +563,7 @@ def inject(initial=undefined, sym=undefined, &block)
563563

564564
def all?(pattern = undefined)
565565
if !Primitive.undefined?(pattern)
566-
warn 'given block not used', uplevel: 1 if block_given?
566+
Primitive.warn_given_block_not_used if block_given?
567567
each { return false unless pattern === Primitive.single_block_arg }
568568
elsif block_given?
569569
each { |*e| return false unless yield(*e) }
@@ -575,7 +575,7 @@ def all?(pattern = undefined)
575575

576576
def any?(pattern = undefined)
577577
if !Primitive.undefined?(pattern)
578-
warn 'given block not used', uplevel: 1 if block_given?
578+
Primitive.warn_given_block_not_used if block_given?
579579
each { return true if pattern === Primitive.single_block_arg }
580580
elsif block_given?
581581
each { |*o| return true if yield(*o) }
@@ -735,7 +735,7 @@ def find_index(value=undefined)
735735
i += 1
736736
end
737737
else
738-
warn 'given block not used', uplevel: 1 if block_given?
738+
Primitive.warn_given_block_not_used if block_given?
739739

740740
i = 0
741741
each do
@@ -926,7 +926,7 @@ def minmax_by(&block)
926926

927927
def none?(pattern = undefined)
928928
if !Primitive.undefined?(pattern)
929-
warn 'given block not used', uplevel: 1 if block_given?
929+
Primitive.warn_given_block_not_used if block_given?
930930
each { return false if pattern === Primitive.single_block_arg }
931931
elsif block_given?
932932
each { |*o| return false if yield(*o) }
@@ -941,7 +941,7 @@ def one?(pattern = undefined)
941941
found_one = false
942942

943943
if !Primitive.undefined?(pattern)
944-
warn 'given block not used', uplevel: 1 if block_given?
944+
Primitive.warn_given_block_not_used if block_given?
945945
each do
946946
if pattern === Primitive.single_block_arg
947947
return false if found_one

src/main/ruby/truffleruby/core/env.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def include?(key)
143143

144144
def fetch(key, absent=undefined)
145145
if block_given? and !Primitive.undefined?(absent)
146-
warn 'block supersedes default value argument', uplevel: 1
146+
Primitive.warn_block_supersedes_default_value_argument
147147
end
148148

149149
if value = lookup(key)

src/main/ruby/truffleruby/core/hash.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def fetch(key, default=undefined)
231231
end
232232

233233
if block_given?
234-
warn 'block supersedes default value argument', uplevel: 1 unless Primitive.undefined?(default)
234+
Primitive.warn_block_supersedes_default_value_argument unless Primitive.undefined?(default)
235235

236236
return yield(key)
237237
end

src/main/ruby/truffleruby/core/thread.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def report_on_exception=(val)
223223
end
224224

225225
def fetch(name, default = undefined)
226-
warn 'block supersedes default value argument' if !Primitive.undefined?(default) && block_given?
226+
Primitive.warn_block_supersedes_default_value_argument if !Primitive.undefined?(default) && block_given?
227227

228228
key = convert_to_local_name(name)
229229
locals = Primitive.thread_get_fiber_locals self

src/main/ruby/truffleruby/core/truffle/polyglot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def fetch(key, default = undefined)
156156
end
157157

158158
if block_given?
159-
warn 'block supersedes default value argument', uplevel: 1 unless Primitive.undefined?(default)
159+
Primitive.warn_block_supersedes_default_value_argument unless Primitive.undefined?(default)
160160

161161
return yield(key)
162162
end

0 commit comments

Comments
 (0)