Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ruby 3.0 support] Add support for Array subclass methods to return Array #2510

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion spec/tags/core/array/drop_tags.txt

This file was deleted.

6 changes: 0 additions & 6 deletions spec/tags/core/array/element_reference_tags.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
fails:Array#[] with a subclass of Array returns a Array instance with [n, m]
fails:Array#[] with a subclass of Array returns a Array instance with [-n, m]
fails:Array#[] with a subclass of Array returns a Array instance with [n..m]
fails:Array#[] with a subclass of Array returns a Array instance with [n...m]
fails:Array#[] with a subclass of Array returns a Array instance with [-n..-m]
fails:Array#[] with a subclass of Array returns a Array instance with [-n...-m]
fails:Array#[] can be sliced with Enumerator::ArithmeticSequence has endless range and positive steps
fails:Array#[] can be sliced with Enumerator::ArithmeticSequence has beginless range and positive steps
fails:Array#[] can be sliced with Enumerator::ArithmeticSequence has endless range and negative steps
Expand Down
1 change: 0 additions & 1 deletion spec/tags/core/array/flatten_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/core/array/multiply_tags.txt

This file was deleted.

12 changes: 0 additions & 12 deletions spec/tags/core/array/slice_tags.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
fails:Array#slice! with a subclass of Array returns a Array instance with [n, m]
fails:Array#slice! with a subclass of Array returns a Array instance with [-n, m]
fails:Array#slice! with a subclass of Array returns a Array instance with [n..m]
fails:Array#slice! with a subclass of Array returns a Array instance with [n...m]
fails:Array#slice! with a subclass of Array returns a Array instance with [-n..-m]
fails:Array#slice! with a subclass of Array returns a Array instance with [-n...-m]
fails:Array#slice with a subclass of Array returns a Array instance with [n, m]
fails:Array#slice with a subclass of Array returns a Array instance with [-n, m]
fails:Array#slice with a subclass of Array returns a Array instance with [n..m]
fails:Array#slice with a subclass of Array returns a Array instance with [n...m]
fails:Array#slice with a subclass of Array returns a Array instance with [-n..-m]
fails:Array#slice with a subclass of Array returns a Array instance with [-n...-m]
fails:Array#slice can be sliced with Enumerator::ArithmeticSequence has endless range and positive steps
fails:Array#slice can be sliced with Enumerator::ArithmeticSequence has beginless range and positive steps
fails:Array#slice can be sliced with Enumerator::ArithmeticSequence has endless range and negative steps
Expand Down
1 change: 0 additions & 1 deletion spec/tags/core/array/uniq_tags.txt

This file was deleted.

15 changes: 1 addition & 14 deletions src/main/java/org/truffleruby/core/array/ArrayIndexNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.truffleruby.builtins.Primitive;
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
import org.truffleruby.core.array.library.ArrayStoreLibrary;
import org.truffleruby.core.klass.RubyClass;
import org.truffleruby.language.RubyContextSourceNode;
import org.truffleruby.language.RubyNode;

Expand All @@ -25,7 +24,6 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.profiles.ConditionProfile;
import org.truffleruby.language.objects.AllocationTracing;

@CoreModule(value = "Truffle::ArrayIndex", isClass = false)
public abstract class ArrayIndexNodes {
Expand Down Expand Up @@ -123,22 +121,11 @@ protected RubyArray readInBounds(RubyArray array, int index, int length,
? length
: size - index;
final Object slice = cowNode.execute(array, index, end);
return createArrayOfSameClass(array, slice, end);
return createArray(slice, end);
}

protected static boolean indexInBounds(RubyArray array, int index) {
return index >= 0 && index <= array.size;
}

protected RubyArray createArrayOfSameClass(RubyArray array, Object store, int size) {
final RubyClass logicalClass = array.getLogicalClass();
RubyArray newArray = new RubyArray(
logicalClass,
getLanguage().arrayShape,
store,
size);
AllocationTracing.trace(newArray, this);
return newArray;
}
}
}
21 changes: 3 additions & 18 deletions src/main/java/org/truffleruby/core/array/ArrayNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,7 @@ public abstract static class MulNode extends PrimitiveArrayArgumentsNode {

@Specialization(guards = "count == 0")
protected RubyArray mulZero(RubyArray array, int count) {
final RubyClass logicalClass = array.getLogicalClass();
return new RubyArray(
logicalClass,
getLanguage().arrayShape,
ArrayStoreLibrary.INITIAL_STORE,
0);
return createEmptyArray();
}

@Specialization(
Expand Down Expand Up @@ -197,12 +192,7 @@ protected RubyArray mulOther(RubyArray array, int count,
profileAndReportLoopCount(loopProfile, n);
}

final RubyClass logicalClass = array.getLogicalClass();
return new RubyArray(
logicalClass,
getLanguage().arrayShape,
newStore,
newSize);
return createArray(newStore, newSize);
}

@Specialization(guards = "count < 0")
Expand All @@ -217,12 +207,7 @@ protected RubyArray mulLong(RubyArray array, long count) {

@Specialization(guards = { "isEmptyArray(array)" })
protected RubyArray mulEmpty(RubyArray array, long count) {
final RubyClass logicalClass = array.getLogicalClass();
return new RubyArray(
logicalClass,
getLanguage().arrayShape,
ArrayStoreLibrary.INITIAL_STORE,
0);
return createEmptyArray();
}

@Specialization(guards = { "!isImplicitLong(count)" })
Expand Down
12 changes: 3 additions & 9 deletions src/main/ruby/truffleruby/core/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def <=>(other)

def *(count)
result = Primitive.array_mul(self, count)

if !Primitive.undefined?(result)
result
elsif str = Truffle::Type.rb_check_convert_type(count, String, :to_str)
Expand Down Expand Up @@ -443,9 +444,9 @@ def first(n = undefined)

def flatten(level=-1)
level = Primitive.rb_num2int level
return self.dup if level == 0
return Array.new(self) if level == 0

out = self.class.allocate # new_reserved size
Strech marked this conversation as resolved.
Show resolved Hide resolved
out = [] # new_reserved size
Primitive.array_flatten_helper(self, out, level)
out
end
Expand Down Expand Up @@ -1591,13 +1592,6 @@ def delete_range(index, del_length)
end
private :delete_range

def uniq(&block)
Strech marked this conversation as resolved.
Show resolved Hide resolved
copy_of_same_class = dup
result = super(&block)
Primitive.steal_array_storage(copy_of_same_class, result)
copy_of_same_class
end

def uniq!(&block)
Primitive.check_frozen self
result = uniq(&block)
Expand Down
1 change: 1 addition & 0 deletions src/main/ruby/truffleruby/core/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ class Array
alias_method :take, :take
alias_method :drop_while, :drop_while
alias_method :take_while, :take_while
alias_method :uniq, :uniq
alias_method :sum, :sum
alias_method :all?, :all?
alias_method :none?, :none?
Expand Down