Skip to content

Commit e01a773

Browse files
committed
[GR-17457] Make ranges object literals where possible.
PullRequest: truffleruby/3418
2 parents fcc2615 + 7adeb9f commit e01a773

35 files changed

+328
-266
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Compatibility:
1414
* Fix `Array#fill` to raise `TypeError` instead of `ArgumentError` when the length argument is not numeric (#2652, @andrykonchin).
1515
* Warn when a global variable is not initialized (#2595, @andrykonchin).
1616
* Fix escaping of `/` by `Regexp#source` (#2569, @andrykonchin).
17+
* Range literals of integers are now created at parse time like in CRuby (#2622, @aardvark179).
1718

1819
Performance:
1920

spec/ruby/core/range/clone_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require_relative '../../spec_helper'
2+
3+
describe "Range#clone" do
4+
it "duplicates the range" do
5+
original = (1..3)
6+
copy = original.clone
7+
copy.begin.should == 1
8+
copy.end.should == 3
9+
copy.should_not.exclude_end?
10+
copy.should_not.equal? original
11+
12+
original = ("a"..."z")
13+
copy = original.clone
14+
copy.begin.should == "a"
15+
copy.end.should == "z"
16+
copy.should.exclude_end?
17+
copy.should_not.equal? original
18+
end
19+
20+
it "maintains the frozen state" do
21+
(1..2).clone.frozen?.should == (1..2).frozen?
22+
(1..).clone.frozen?.should == (1..).frozen?
23+
Range.new(1, 2).clone.frozen?.should == Range.new(1, 2).frozen?
24+
Class.new(Range).new(1, 2).clone.frozen?.should == Class.new(Range).new(1, 2).frozen?
25+
end
26+
end

spec/ruby/core/range/dup_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
describe "Range#dup" do
44
it "duplicates the range" do
5-
copy = (1..3).dup
5+
original = (1..3)
6+
copy = original.dup
67
copy.begin.should == 1
78
copy.end.should == 3
89
copy.should_not.exclude_end?
10+
copy.should_not.equal?(original)
911

1012
copy = ("a"..."z").dup
1113
copy.begin.should == "a"

spec/ruby/core/range/new_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,15 @@
6565

6666
range_exclude.should_not == range_include
6767
end
68+
69+
ruby_version_is "3.0" do
70+
it "creates a frozen range if the class is Range.class" do
71+
Range.new(1, 2).should.frozen?
72+
end
73+
74+
it "does not create a frozen range if the class is not Range.class" do
75+
Class.new(Range).new(1, 2).should_not.frozen?
76+
end
77+
end
6878
end
6979
end

spec/ruby/language/range_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
(1...10).should == Range.new(1, 10, true)
1111
end
1212

13+
it "creates a simple range as an object literal" do
14+
ary = []
15+
2.times do
16+
ary.push(1..3)
17+
end
18+
ary[0].should.equal?(ary[1])
19+
end
20+
1321
it "creates endless ranges" do
1422
(1..).should == Range.new(1, nil)
1523
(1...).should == Range.new(1, nil, true)

spec/tags/core/range/bsearch_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

spec/tags/core/range/minmax_tags.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
fails:Range#minmax on an inclusive range should raise RangeError on an endless range without iterating the range
2-
fails:Range#minmax on an inclusive range raises RangeError or ArgumentError on a beginless range
32
fails:Range#minmax on an exclusive range should raise RangeError on an endless range
4-
fails:Range#minmax on an exclusive range should raise RangeError on a beginless range

spec/tags/language/class_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
fails:A class definition extending an object (sclass) allows accessing the block of the original scope
2-
fails:A class definition extending an object (sclass) does not allow accessing the block of the original scope

src/main/.checkstyle_checks.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@
211211
<property name="message" value="Don't use transferToInterpreterOnException"/>
212212
</module>
213213
<module name="RegexpSinglelineJava">
214-
<property name="format" value='"isRuby(?!Value|DynamicObject|SymbolOrString|Rational|[^"]+\|\|)'/> <!-- Rational is not a builtin type -->
214+
<property name="format" value='"isRuby(?!Value|DynamicObject|SymbolOrString|Rational|Range|[^"]+\|\|)'/> <!-- Rational is not a builtin type -->
215215
<property name="message" value="Type the argument instead of using a positive isRuby* guard."/>
216216
</module>
217217
<module name="RegexpSinglelineJava">

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@
6666
import org.truffleruby.core.proc.RubyProc;
6767
import org.truffleruby.core.queue.RubyQueue;
6868
import org.truffleruby.core.queue.RubySizedQueue;
69-
import org.truffleruby.core.range.RubyIntRange;
70-
import org.truffleruby.core.range.RubyLongRange;
7169
import org.truffleruby.core.range.RubyObjectRange;
7270
import org.truffleruby.core.regexp.RegexpCacheKey;
7371
import org.truffleruby.core.regexp.RegexpTable;
@@ -260,9 +258,7 @@ public static final class ThreadLocalState {
260258
public final Shape handleShape = createShape(RubyHandle.class);
261259
public final Shape hashShape = createShape(RubyHash.class);
262260
public final Shape innerContextShape = createShape(RubyInnerContext.class);
263-
public final Shape intRangeShape = createShape(RubyIntRange.class);
264261
public final Shape ioShape = createShape(RubyIO.class);
265-
public final Shape longRangeShape = createShape(RubyLongRange.class);
266262
public final Shape matchDataShape = createShape(RubyMatchData.class);
267263
public final Shape methodShape = createShape(RubyMethod.class);
268264
public final Shape mutexShape = createShape(RubyMutex.class);

0 commit comments

Comments
 (0)