Skip to content

Commit 510c945

Browse files
committed
Fix Array#sort_by! and raise FrozenError lazily only during iteration when method called without a block
1 parent bcea091 commit 510c945

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

spec/ruby/core/array/reject_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@
116116
-> { ArraySpecs.empty_frozen_array.reject! {} }.should raise_error(FrozenError)
117117
end
118118

119+
it "raises a FrozenError on a frozen array only during iteration if called without a block" do
120+
enum = ArraySpecs.frozen_array.reject!
121+
-> { enum.each {} }.should raise_error(FrozenError)
122+
end
123+
119124
it "does not truncate the array is the block raises an exception" do
120125
a = [1, 2, 3]
121126
begin

spec/ruby/core/array/shared/keep_if.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
-> { @frozen.send(@method) { false } }.should raise_error(FrozenError)
5858
end
5959
end
60+
61+
it "raises a FrozenError on a frozen array only during iteration if called without a block" do
62+
enum = @frozen.send(@method)
63+
-> { enum.each {} }.should raise_error(FrozenError)
64+
end
6065
end
6166

6267
it "does not truncate the array is the block raises an exception" do

spec/ruby/core/array/sort_by_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
-> { ArraySpecs.empty_frozen_array.sort_by! {}}.should raise_error(FrozenError)
3333
end
3434

35+
it "raises a FrozenError on a frozen array only during iteration if called without a block" do
36+
enum = ArraySpecs.frozen_array.sort_by!
37+
-> { enum.each {} }.should raise_error(FrozenError)
38+
end
39+
3540
it "returns the specified value when it would break in the given block" do
3641
[1, 2, 3].sort_by!{ break :a }.should == :a
3742
end

src/main/ruby/truffleruby/core/array.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,10 +1259,10 @@ def drop(n)
12591259
end
12601260

12611261
def sort_by!(&block)
1262-
Primitive.check_frozen self
1263-
12641262
return to_enum(:sort_by!) { size } unless block_given?
12651263

1264+
Primitive.check_frozen self
1265+
12661266
Primitive.steal_array_storage(self, sort_by(&block))
12671267
end
12681268

0 commit comments

Comments
 (0)