Skip to content

Commit c759ede

Browse files
committed
[GR-45844] Add Primitive.true? and Primitive.false?
PullRequest: truffleruby/3791
2 parents 193fc87 + 3227e39 commit c759ede

19 files changed

+156
-29
lines changed

.rubocop.yml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
require:
22
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_nil.rb
3-
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_object_class.rb
4-
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_object_equal.rb
5-
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_object_kind_of.rb
3+
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_class.rb
4+
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_equal.rb
5+
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_is_a.rb
6+
- ./tool/rubocop-truffleruby/cop/replace_with_primitive_true_and_false_predicates.rb
67

78
AllCops:
89
TargetRubyVersion: 3.1
@@ -406,21 +407,28 @@ TruffleRuby/ReplaceWithPrimitiveNil:
406407
- src/main/ruby/**/*.rb
407408

408409
# Supports --auto-correct
409-
TruffleRuby/ReplaceWithPrimitiveObjectClass:
410+
TruffleRuby/ReplaceWithPrimitiveClass:
410411
Enabled: true
411412
Include: # inspect *only* these files
412413
- lib/truffle/**/*.rb
413414
- src/main/ruby/**/*.rb
414415

415416
# Supports --auto-correct
416-
TruffleRuby/ReplaceWithPrimitiveObjectEqual:
417+
TruffleRuby/ReplaceWithPrimitiveEqual:
417418
Enabled: true
418419
Include: # inspect *only* these files
419420
- lib/truffle/**/*.rb
420421
- src/main/ruby/**/*.rb
421422

422423
# Supports --auto-correct
423-
TruffleRuby/ReplaceWithPrimitiveObjectKindOf:
424+
TruffleRuby/ReplaceWithPrimitiveIsA:
425+
Enabled: true
426+
Include: # inspect *only* these files
427+
- lib/truffle/**/*.rb
428+
- src/main/ruby/**/*.rb
429+
430+
# Supports --auto-correct
431+
TruffleRuby/ReplaceWithPrimitiveTrueAndFalsePredicates:
424432
Enabled: true
425433
Include: # inspect *only* these files
426434
- lib/truffle/**/*.rb

lib/truffle/socket/truffle.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def self.convert_reverse_lookup(socket = nil, reverse_lookup = nil)
333333
elsif reverse_lookup == :numeric
334334
reverse_lookup = false
335335

336-
elsif reverse_lookup != true and reverse_lookup != false
336+
elsif !Primitive.true?(reverse_lookup) and !Primitive.false?(reverse_lookup)
337337
raise ArgumentError,
338338
"invalid reverse_lookup flag: #{reverse_lookup.inspect}"
339339
end

lib/truffle/truffle/cext.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def rb_filesystem_encoding
633633

634634
def rb_to_encoding_index(enc)
635635
enc = Truffle::Type.coerce_to_encoding(enc)
636-
return -1 if enc == false
636+
return -1 if Primitive.false?(enc)
637637
rb_enc_to_index(enc)
638638
end
639639

@@ -1186,7 +1186,7 @@ def rb_class_new(superclass)
11861186

11871187
def rb_define_class_under(mod, name, superclass)
11881188
# nil is TypeError (checked below), false is ArgumentError
1189-
if Primitive.equal?(false, superclass)
1189+
if Primitive.false?(superclass)
11901190
raise ArgumentError, "no super class for `#{name}'"
11911191
end
11921192

src/main/java/org/truffleruby/core/support/TypeNodes.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,32 @@ protected boolean other(Object value) {
224224
}
225225
}
226226

227+
@Primitive(name = "true?")
228+
public abstract static class IsTrue extends PrimitiveArrayArgumentsNode {
229+
@Specialization
230+
protected boolean bool(boolean value) {
231+
return value;
232+
}
233+
234+
@Fallback
235+
protected boolean other(Object value) {
236+
return false;
237+
}
238+
}
239+
240+
@Primitive(name = "false?")
241+
public abstract static class IsFalse extends PrimitiveArrayArgumentsNode {
242+
@Specialization
243+
protected boolean bool(boolean value) {
244+
return !value;
245+
}
246+
247+
@Fallback
248+
protected boolean other(Object value) {
249+
return false;
250+
}
251+
}
252+
227253
@Primitive(name = "object_ivars")
228254
public abstract static class ObjectInstanceVariablesNode extends PrimitiveArrayArgumentsNode {
229255

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def glob(pattern, flags = 0, base: nil, sort: true, &block)
266266

267267
matches = []
268268
index = 0
269-
flags |= File::FNM_GLOB_NOSORT if Primitive.equal?(sort, false)
269+
flags |= File::FNM_GLOB_NOSORT if Primitive.false?(sort)
270270

271271
normalized_base = if Primitive.nil? base
272272
nil

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ def fcntl(command, arg = 0)
14611461

14621462
if !arg
14631463
arg = 0
1464-
elsif arg == true
1464+
elsif Primitive.true?(arg)
14651465
arg = 1
14661466
elsif Primitive.is_a?(arg, String)
14671467
raise NotImplementedError, 'cannot handle String'
@@ -1491,7 +1491,7 @@ def ioctl(command, arg = 0)
14911491

14921492
if !arg
14931493
real_arg = 0
1494-
elsif arg == true
1494+
elsif Primitive.true?(arg)
14951495
real_arg = 1
14961496
elsif Primitive.is_a?(arg, String)
14971497
# This could be faster.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def Complex(real, imag = undefined, exception: true)
5454
module_function :Complex
5555

5656
def Float(obj, exception: true)
57-
raise_exception = !Primitive.equal?(exception, false)
57+
raise_exception = !Primitive.false?(exception)
5858
obj = Truffle::Interop.unbox_if_needed(obj)
5959

6060
case obj
@@ -104,7 +104,7 @@ def Integer(obj, base = 0, exception: true)
104104
obj = Truffle::Interop.unbox_if_needed(obj)
105105
converted_base = Truffle::Type.rb_check_to_integer(base, :to_int)
106106
base = Primitive.nil?(converted_base) ? 0 : converted_base
107-
raise_exception = !Primitive.equal?(exception, false)
107+
raise_exception = !Primitive.false?(exception)
108108

109109
if Primitive.is_a?(obj, String)
110110
Primitive.string_to_inum(obj, base, true, raise_exception)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Numeric
3030
include Comparable
3131

3232
def clone(freeze: nil)
33-
if Primitive.equal?(freeze, false)
33+
if Primitive.false?(freeze)
3434
raise ArgumentError, "can't unfreeze #{Primitive.class(self).name}"
3535
end
3636
self

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def include(mod)
5858
result = Primitive.unbound_method_ruby2_keywords(method)
5959
if Primitive.nil?(result)
6060
warn "Skipping set of ruby2_keywords flag for #{name} (method accepts keywords or method does not accept argument splat)", uplevel: 1
61-
elsif result == false
61+
elsif Primitive.false?(result)
6262
warn "Skipping set of ruby2_keywords flag for #{name} (unknown reason)", uplevel: 1
6363
end
6464
end
@@ -71,7 +71,7 @@ def ruby2_keywords
7171
result = Primitive.proc_ruby2_keywords(self)
7272
if Primitive.nil?(result)
7373
warn 'Skipping set of ruby2_keywords flag for proc (proc accepts keywords or proc does not accept argument splat)', uplevel: 1
74-
elsif result == false
74+
elsif Primitive.false?(result)
7575
warn 'Skipping set of ruby2_keywords flag for proc (unknown reason)', uplevel: 1
7676
end
7777
self

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def bsearch(&block)
112112
return nil if max < min || max == min && self.exclude_end? #-inf.prev_float == -inf!
113113
if max == min
114114
result = yield max
115-
return result == true || result == 0 ? max : nil
115+
return Primitive.true?(result) || result == 0 ? max : nil
116116
end
117117

118118
# max == -Float::INFINITY will already be covered by the comparisons above.
@@ -136,7 +136,7 @@ def bsearch(&block)
136136
if min == -Float::INFINITY
137137
result = yield min
138138
# Find-minimum mode: It's not going to get any smaller than negative infinity.
139-
return min if result == true || result == 0
139+
return min if Primitive.true?(result) || result == 0
140140
return nil if Primitive.is_a?(result, Numeric) && result < 0
141141
min = normalized_begin = -Float::MAX # guaranteed to be <= max
142142
end
@@ -175,12 +175,12 @@ def bsearch(&block)
175175
# max is untested only if it's the end of the range.
176176
# It can only change the result of the search if we haven't found an admissible value yet (+ infinity edge case).
177177
result = yield max
178-
return max if result == true || result == 0
178+
return max if Primitive.true?(result) || result == 0
179179
elsif mid == max && min == normalized_begin
180180
# min is untested only if it's the begin of the range.
181181
result = yield min
182182
# Favor smallest value in case we're in find-minimum mode.
183-
return min if result == true || result == 0
183+
return min if Primitive.true?(result) || result == 0
184184
end
185185

186186
last_admissible

0 commit comments

Comments
 (0)