Skip to content

Commit 4f2e278

Browse files
committed
[GR-19220] Fix Float#<=> for immutable objects (#2432)
PullRequest: truffleruby/2891
2 parents 513d5e2 + d34a690 commit 4f2e278

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Bug fixes:
2121
* Fix issue with `strspn` used in the `date` C extension compiled as a macro on older glibc and then missing the `__strspn_c1` symbol on newer glibc (#2406).
2222
* Fix constant lookup when loading the same file multiple times (#2408).
2323
* Fix handling of `break`, `next` and `redo` in `define_method(name, &block)` methods (#2418).
24+
* Fix handling of incompatible types in `Float#<=>` (#2432, @chrisseaton).
2425

2526
Compatibility:
2627

spec/ruby/core/float/comparison_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
it "returns nil when the given argument is not a Float" do
1616
(1.0 <=> "1").should be_nil
17+
(1.0 <=> "1".freeze).should be_nil
18+
(1.0 <=> :one).should be_nil
19+
(1.0 <=> true).should be_nil
1720
end
1821

1922
it "compares using #coerce when argument is not a Float" do

src/main/java/org/truffleruby/core/numeric/FloatNodes.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.truffleruby.core.string.RubyString;
3333
import org.truffleruby.core.string.StringNodes;
3434
import org.truffleruby.core.string.StringUtils;
35-
import org.truffleruby.language.Nil;
3635
import org.truffleruby.language.RubyDynamicObject;
3736
import org.truffleruby.language.Visibility;
3837
import org.truffleruby.language.control.RaiseException;
@@ -414,14 +413,8 @@ protected int compare(double a, double b) {
414413
return Double.compare(a, b);
415414
}
416415

417-
@Specialization(guards = { "!isNaN(a)", "!isRubyBignum(b)" })
418-
protected Object compare(double a, RubyDynamicObject b,
419-
@Cached DispatchNode redoCompare) {
420-
return redoCompare.call(a, "redo_compare_bad_coerce_return_error", b);
421-
}
422-
423-
@Specialization(guards = { "!isNaN(a)" })
424-
protected Object compare(double a, Nil b,
416+
@Specialization(guards = { "!isNaN(a)", "!isRubyNumber(b)" })
417+
protected Object compare(double a, Object b,
425418
@Cached DispatchNode redoCompare) {
426419
return redoCompare.call(a, "redo_compare_bad_coerce_return_error", b);
427420
}

0 commit comments

Comments
 (0)