Skip to content

Commit caac478

Browse files
authored
Merge pull request jruby#9204 from trinistr/fix-bigdecimal-add-sub-coercion
Fix BigDecimal#+, #-, #add, #sub failing when coercing doesn't return a BigDecimal
2 parents f9b1d3c + 47b47a1 commit caac478

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,9 +1267,9 @@ private IRubyObject addInternal(ThreadContext context, RubyBigDecimal val, IRuby
12671267
// http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17374
12681268
// return callCoerced(context, op, b, true); -- this is MRI behavior.
12691269
// We'll use ours for now, thus providing an ability to add Floats.
1270-
RubyBigDecimal ret = (RubyBigDecimal) callCoerced(context, sites(context).op_plus, b, true);
1271-
if (ret != null) {
1272-
ret.setResult(context, prec);
1270+
IRubyObject ret = callCoerced(context, sites(context).op_plus, b, true);
1271+
if (ret instanceof RubyBigDecimal bd) {
1272+
bd.setResult(context, prec);
12731273
}
12741274
return ret;
12751275
}
@@ -1333,7 +1333,13 @@ public IRubyObject sub2(ThreadContext context, IRubyObject b, IRubyObject n) {
13331333
}
13341334

13351335
private IRubyObject subInternal(ThreadContext context, RubyBigDecimal val, IRubyObject b, int prec) {
1336-
if (val == null) return ((RubyBigDecimal)callCoerced(context, sites(context).op_minus, b, true)).setResult(context, prec);
1336+
if (val == null) {
1337+
IRubyObject ret = callCoerced(context, sites(context).op_minus, b, true);
1338+
if (ret instanceof RubyBigDecimal bd) {
1339+
bd.setResult(context, prec);
1340+
}
1341+
return ret;
1342+
}
13371343
RubyBigDecimal res = subSpecialCases(context, val);
13381344
return res != null ? res : new RubyBigDecimal(context.runtime, value.subtract(val.value)).setResult(context, prec);
13391345
}

0 commit comments

Comments
 (0)