Skip to content

Commit 1f8f02d

Browse files
committed
Rewrote Range creation for 3.1.0 compatibility (frozen methods) and added a relevant spec case
1 parent dc37253 commit 1f8f02d

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

spec/ruby/language/range_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
it "is frozen" do
2020
(42..).should.frozen?
2121
end
22+
23+
it "is not frozen if it is a subclass of Range" do
24+
class ChildRange < Range; end
25+
ChildRange.new(1, 2).should_not.frozen?
26+
end
27+
28+
it "is not frozen if it is created through allocate" do
29+
Range.allocate.should_not.frozen?
30+
end
2231
end
2332

2433
ruby_version_is "2.7" do

spec/tags/language/range_tags.txt

-1
This file was deleted.

src/main/java/org/truffleruby/core/range/RangeNodes.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.truffleruby.core.range;
1111

12+
import com.oracle.truffle.api.dsl.Bind;
1213
import com.oracle.truffle.api.profiles.LoopConditionProfile;
1314
import org.truffleruby.builtins.CoreMethod;
1415
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
@@ -223,7 +224,8 @@ protected RubyObjectRange dup(RubyObjectRange range) {
223224
getLanguage().objectRangeShape,
224225
range.excludedEnd,
225226
range.begin,
226-
range.end);
227+
range.end,
228+
range.frozen);
227229
AllocationTracing.trace(copy, this);
228230
return copy;
229231
}
@@ -546,16 +548,17 @@ protected RubyLongRange longRange(RubyClass rubyClass, long begin, long end, boo
546548
return range;
547549
}
548550

549-
@Specialization(guards = { "rubyClass != getRangeClass() || (!isImplicitLong(begin) || !isImplicitLong(end))" })
551+
@Specialization(guards = { "nonStandardClass || (!isImplicitLong(begin) || !isImplicitLong(end))" })
550552
protected RubyObjectRange objectRange(RubyClass rubyClass, Object begin, Object end, boolean excludeEnd,
551-
@Cached DispatchNode compare) {
553+
@Cached DispatchNode compare, @Bind("rubyClass != getRangeClass()") boolean nonStandardClass) {
552554

553555
if (compare.call(begin, "<=>", end) == nil && end != nil && begin != nil) {
554556
throw new RaiseException(getContext(), coreExceptions().argumentError("bad value for range", this));
555557
}
556558

557559
final Shape shape = getLanguage().objectRangeShape;
558-
final RubyObjectRange range = new RubyObjectRange(rubyClass, shape, excludeEnd, begin, end);
560+
final RubyObjectRange range = new RubyObjectRange(rubyClass, shape, excludeEnd, begin, end,
561+
!nonStandardClass);
559562
AllocationTracing.trace(range, this);
560563
return range;
561564
}
@@ -571,7 +574,7 @@ public abstract static class AllocateNode extends UnaryCoreMethodNode {
571574
@Specialization
572575
protected RubyObjectRange allocate(RubyClass rubyClass) {
573576
final Shape shape = getLanguage().objectRangeShape;
574-
final RubyObjectRange range = new RubyObjectRange(rubyClass, shape, false, nil, nil);
577+
final RubyObjectRange range = new RubyObjectRange(rubyClass, shape, false, nil, nil, false);
575578
AllocationTracing.trace(range, this);
576579
return range;
577580
}

src/main/java/org/truffleruby/core/range/RubyObjectRange.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ public final class RubyObjectRange extends RubyRange implements ObjectGraphNode
2323
public Object begin;
2424
public Object end;
2525

26-
public RubyObjectRange(RubyClass rubyClass, Shape shape, boolean excludedEnd, Object begin, Object end) {
27-
super(rubyClass, shape, excludedEnd);
26+
public RubyObjectRange(
27+
RubyClass rubyClass,
28+
Shape shape,
29+
boolean excludedEnd,
30+
Object begin,
31+
Object end,
32+
boolean frozen) {
33+
super(rubyClass, shape, excludedEnd, frozen);
2834
this.begin = begin;
2935
this.end = end;
3036
}

src/main/java/org/truffleruby/core/range/RubyRange.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public abstract class RubyRange extends RubyDynamicObject {
2323
public boolean frozen;
2424

2525
public RubyRange(RubyClass rubyClass, Shape shape, boolean excludedEnd) {
26-
super(rubyClass, shape);
27-
this.excludedEnd = excludedEnd;
26+
this(rubyClass, shape, excludedEnd, true);
2827
}
2928

3029
public RubyRange(RubyClass rubyClass, Shape shape, boolean excludedEnd, boolean frozen) {

0 commit comments

Comments
 (0)