Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
diff --git a/kernel/alpha.rb b/kernel/alpha.rb
index 915b1f6..932cb7f 100644
--- a/kernel/alpha.rb
+++ b/kernel/alpha.rb
@@ -38,6 +38,12 @@ class Rubinius::VM
   end
 end

+module Rubinius
+  def self.coerce_into_array(obj)
+    obj
+  end
+end
+
 class Object
   def __show__
     Ruby.primitive :object_show
diff --git a/kernel/compiler/bytecode.rb b/kernel/compiler/bytecode.rb
index 77b2a75..c030670 100644
--- a/kernel/compiler/bytecode.rb
+++ b/kernel/compiler/bytecode.rb
@@ -1574,14 +1574,14 @@ class Compiler
         if @splat_rhs then
           g.make_array @rhs.body.size
           @splat_rhs.bytecode(g)
-          g.cast_array
+          g.coerce_into_array
           g.send :+, 1
         else
           g.rotate @rhs.body.size if @rhs.respond_to? :body # HACK
         end

         if @lhs && !@lhs.body.empty?
-          g.cast_array
+          g.coerce_into_array
           @lhs.body.each do |x|
             g.shift_array
             if x.is? AttrAssign
@@ -2207,7 +2207,7 @@ class Compiler
       # If the array has 1 or 0 elements, grab the 0th element.
       # otherwise, leave the array on the stack.
       def unwrap_array(g)
-        g.cast_array
+        g.coerce_into_array
         g.dup
         g.send :size, 0
         g.push 1
@@ -2282,7 +2282,7 @@ class Compiler
       def call_bytecode(g)
         if @child
           @child.bytecode(g)
-          g.cast_array
+          g.coerce_into_array
         end

         return 0
@@ -2293,7 +2293,7 @@ class Compiler
       def bytecode_in_array_literal(g)
         if @child
           @child.bytecode(g)
-          g.cast_array
+          g.coerce_into_array
           g.send :+, 1
         end
       end
diff --git a/kernel/compiler/generator.rb b/kernel/compiler/generator.rb
index 9e79d43..eaf5dc8 100644
--- a/kernel/compiler/generator.rb
+++ b/kernel/compiler/generator.rb
@@ -575,6 +575,15 @@ class Compiler
       add :make_array, count
     end

+    def coerce_into_array
+      unless @last and [:cast_array, :make_array].include? @last.first
+        push_const(:Rubinius)
+        swap_stack
+        send(:coerce_into_array, 1)
+        cast_array
+      end
+    end
+
     def cast_array
       unless @last and [:cast_array, :make_array].include? @last.first
         add :cast_array
diff --git a/kernel/delta/rubinius.rb b/kernel/delta/rubinius.rb
index 45dd890..c09aff9 100644
--- a/kernel/delta/rubinius.rb
+++ b/kernel/delta/rubinius.rb
@@ -114,4 +114,11 @@ module Rubinius
     writer_name = "#{normalized}=".to_sym
     add_method writer_name, AccessVariable.set_ivar(normalized), mod, vis
   end
+
+  def self.coerce_into_array(obj)
+    return obj if obj.kind_of?(Tuple)
+    return obj if obj.kind_of?(Array)
+    return obj.to_ary if obj.respond_to?(:to_ary)
+    [obj]
+  end
 end
diff --git a/spec/compiler/array_spec.rb b/spec/compiler/array_spec.rb
index 8e7c345..236e936 100644
--- a/spec/compiler/array_spec.rb
+++ b/spec/compiler/array_spec.rb
@@ -108,7 +108,7 @@ describe "An Array node" do
     compile do |g|
       g.make_array 0
       g.push 1
-      g.cast_array
+      g.coerce_into_array
       g.send :+, 1
     end
   end
@@ -121,7 +121,7 @@ describe "An Array node" do
     compile do |g|
       g.make_array 0
       g.push 1
-      g.cast_array
+      g.coerce_into_array
       g.send :+, 1
       g.make_array 1
     end
@@ -137,7 +137,7 @@ describe "An Array node" do
       g.make_array 1

       g.push 2
-      g.cast_array
+      g.coerce_into_array

       g.send :+, 1
     end
diff --git a/spec/compiler/attrasgn_spec.rb b/spec/compiler/attrasgn_spec.rb
index 4a7aaad..810d5bc 100644
--- a/spec/compiler/attrasgn_spec.rb
+++ b/spec/compiler/attrasgn_spec.rb
@@ -51,7 +51,7 @@ describe "An Attrasgn node" do
       g.send :a, 0, true
       g.push :self
       g.send :b, 0, true
-      g.cast_array
+      g.coerce_into_array
       g.push :self
       g.send :c, 0, true
       g.dup
diff --git a/spec/compiler/call_spec.rb b/spec/compiler/call_spec.rb
index 9e289b4..4987d1f 100644
--- a/spec/compiler/call_spec.rb
+++ b/spec/compiler/call_spec.rb
@@ -39,7 +39,7 @@ describe "A Call node" do
       g.push 2
       g.push :self
       g.send :a, 0, true
-      g.cast_array
+      g.coerce_into_array

       g.push :nil
       g.send_with_splat :h, 2, true, false
@@ -73,7 +73,7 @@ describe "A Call node" do
       g.push :self
       g.push :self
       g.send :a, 0, true
-      g.cast_array
+      g.coerce_into_array
       g.push :nil
       g.send_with_splat :blah, 0, true, false
     end
@@ -427,7 +427,7 @@ describe "A Call node" do

       g.push :self
       g.send :c, 0, true
-      g.cast_array
+      g.coerce_into_array
       g.push :nil
       g.send_with_splat :m, 2, false, false
     end
@@ -593,7 +593,7 @@ describe "A Call node" do

       g.push :self
       g.send :c, 0, true
-      g.cast_array
+      g.coerce_into_array
       g.push :nil

       g.send_with_splat :m, 2, true, false
diff --git a/spec/compiler/defn_spec.rb b/spec/compiler/defn_spec.rb
index 4da415a..5bc3fbe 100644
--- a/spec/compiler/defn_spec.rb
+++ b/spec/compiler/defn_spec.rb
@@ -62,7 +62,7 @@ describe "A Defn node" do
         d.push :self
         d.push 42          # only line different from block_pass_splat
         d.push_local 0
-        d.cast_array
+        d.coerce_into_array
         d.push_local 1
         d.dup
         d.is_nil
@@ -121,7 +121,7 @@ describe "A Defn node" do

         d.push :self
         d.push_local 0
-        d.cast_array
+        d.coerce_into_array
         d.push_local 1
         d.dup
         d.is_nil
@@ -904,7 +904,7 @@ describe "A Defn node" do
       in_method :x do |d|
         d.push :self
         d.push_local 0
-        d.cast_array
+        d.coerce_into_array
         d.push :nil
         d.send_with_splat :a, 0, true, false
       end
diff --git a/spec/compiler/hash_spec.rb b/spec/compiler/hash_spec.rb
index f778350..5c9a1bd 100644
--- a/spec/compiler/hash_spec.rb
+++ b/spec/compiler/hash_spec.rb
@@ -52,7 +52,7 @@ describe "A Hash node" do

       g.make_array 0
       g.push 1
-      g.cast_array
+      g.coerce_into_array
       g.send :+, 1

       g.send :[], 2
diff --git a/spec/compiler/lasgn_spec.rb b/spec/compiler/lasgn_spec.rb
index 5389ab5..9f875a8 100644
--- a/spec/compiler/lasgn_spec.rb
+++ b/spec/compiler/lasgn_spec.rb
@@ -20,7 +20,7 @@ describe "An Lasgn node" do
       g.make_array 2
       g.push :self
       g.send :d, 0, true
-      g.cast_array
+      g.coerce_into_array
       g.send :+, 1
       g.set_local 0
     end
@@ -134,7 +134,7 @@ describe "An Lasgn node" do

       g.push :self
       g.send :b, 0, true
-      g.cast_array
+      g.coerce_into_array
       g.dup

       g.send :size, 0
diff --git a/spec/compiler/masgn_spec.rb b/spec/compiler/masgn_spec.rb
index 0838e8d..c1740b0 100644
--- a/spec/compiler/masgn_spec.rb
+++ b/spec/compiler/masgn_spec.rb
@@ -94,9 +94,9 @@ describe "A Masgn node" do
       g.push 3
       g.push 4
       g.make_array 2
-      g.cast_array
+      g.coerce_into_array
       g.send :+, 1
-      g.cast_array
+      g.coerce_into_array

       g.shift_array
       g.set_local 0
@@ -127,7 +127,7 @@ describe "A Masgn node" do
     compile do |g|
       g.push :self
       g.send :q, 0, true
-      g.cast_array
+      g.coerce_into_array

       g.shift_array
       g.push :self
@@ -490,7 +490,7 @@ describe "A Masgn node" do
       g.push :self
       g.send :d, 0, true

-      g.cast_array
+      g.coerce_into_array

       g.lvar_set 0
       g.lvar_set 1
@@ -514,10 +514,10 @@ describe "A Masgn node" do

       g.push :self
       g.send :d, 0, true
-      g.cast_array
+      g.coerce_into_array

       g.send :+, 1
-      g.cast_array
+      g.coerce_into_array

       g.lvar_set 0
       g.lvar_set 1
@@ -539,7 +539,7 @@ describe "A Masgn node" do
       g.push :self
       g.send :d, 0, true

-      g.cast_array
+      g.coerce_into_array

       g.lvar_set 0
       g.lvar_set 1
@@ -602,7 +602,7 @@ describe "A Masgn node" do
       g.pop

       g.shift_array
-      g.cast_array
+      g.coerce_into_array
       g.shift_array
       g.set_local 1
       g.pop
@@ -640,7 +640,7 @@ describe "A Masgn node" do
     compile do |g|
       g.push :self
       g.send :c, 0, true
-      g.cast_array
+      g.coerce_into_array

       g.shift_array
       g.set_local 0
@@ -666,7 +666,7 @@ describe "A Masgn node" do
     compile do |g|
       g.push :self
       g.send :c, 0, true
-      g.cast_array
+      g.coerce_into_array

       g.shift_array
       g.set_local 0
@@ -694,7 +694,7 @@ describe "A Masgn node" do
       g.push :self
       g.send :d, 0, true
       g.send :m, 1, true
-      g.cast_array
+      g.coerce_into_array
       g.shift_array
       g.set_local 0
       g.pop
@@ -759,7 +759,7 @@ describe "A Masgn node" do
       g.string_dup
       g.send :e, 1, false

-      g.cast_array
+      g.coerce_into_array

       g.shift_array
       g.set_local 0
@@ -788,7 +788,7 @@ describe "A Masgn node" do
       g.push :self
       g.send :d, 0, true

-      g.cast_array
+      g.coerce_into_array

       g.shift_array
       g.set_local 0
@@ -816,7 +816,7 @@ describe "A Masgn node" do
     compile do |g|
       g.push :self
       g.send :c, 0, true
-      g.cast_array
+      g.coerce_into_array

       g.shift_array
       g.set_local 0
diff --git a/spec/compiler/return_spec.rb b/spec/compiler/return_spec.rb
index 588e50f..13bbe70 100644
--- a/spec/compiler/return_spec.rb
+++ b/spec/compiler/return_spec.rb
@@ -32,7 +32,7 @@ describe "A Return node" do
       bottom = g.new_label

       g.push 1
-      g.cast_array
+      g.coerce_into_array
       g.dup
       g.send :size, 0
       g.push 1
@@ -76,7 +76,7 @@ describe "A Return node" do

       g.push :self
       g.send :c, 0, true
-      g.cast_array
+      g.coerce_into_array

       g.send :+, 1
       g.ret
diff --git a/spec/compiler/super_spec.rb b/spec/compiler/super_spec.rb
index aa38e87..d47732d 100644
--- a/spec/compiler/super_spec.rb
+++ b/spec/compiler/super_spec.rb
@@ -111,7 +111,7 @@ describe "A Super node" do
       g.push :self
       g.send :b, 0, true

-      g.cast_array
+      g.coerce_into_array

       g.push_block

diff --git a/spec/custom/helpers/generator.rb b/spec/custom/helpers/generator.rb
index 34dc281..d971802 100644
--- a/spec/custom/helpers/generator.rb
+++ b/spec/custom/helpers/generator.rb
@@ -67,7 +67,8 @@ class TestGenerator
               :swap]
   opcodes -= [:class,
               :goto,
-              :set_label]
+              :set_label,
+              :cast_array]

   opcodes.each do |name|
     class_eval <<-CODE
@@ -104,19 +105,20 @@ class TestGenerator
     end
   end

-  def to_ary
+  def to_a
     convert_to_ary [:test_generator, @stream]
   end

   def pretty_inspect
-    to_ary.pretty_inspect
+    to_a.pretty_inspect
   end

   def inspect
-    to_ary.inspect
+    to_a.inspect
   end

   def add(*args)
+    @last = args
     @stream << args
     @ip += 1
   end
@@ -582,4 +584,19 @@ class TestGenerator
     g.push_literal "#{name} used in invalid context"
     g.send :raise, 2, true
   end
+
+  def coerce_into_array
+    unless @last and [:cast_array, :make_array].include? @last.first
+      push_const(:Rubinius)
+      swap_stack
+      send(:coerce_into_array, 1)
+      cast_array
+    end
+  end
+
+  def cast_array
+    unless @last and [:cast_array, :make_array].include? @last.first
+      add :cast_array
+    end
+  end
 end
diff --git a/spec/tags/frozen/language/array_tags.txt b/spec/tags/frozen/language/array_tags.txt
index d45bf27..e3ac2e6 100644
--- a/spec/tags/frozen/language/array_tags.txt
+++ b/spec/tags/frozen/language/array_tags.txt
@@ -1,2 +1 @@
 fails:The unpacking splat operator (*) when applied to a value with no other items in the containing array, coerces the passed value to an array and returns it unchanged
-fails:The unpacking splat operator (*) when applied to a non-Array value attempts to coerce it to Array if the object respond_to?(:to_ary)
diff --git a/spec/tags/frozen/language/method_tags.txt b/spec/tags/frozen/language/method_tags.txt
index 42f61b0..d539095 100644
--- a/spec/tags/frozen/language/method_tags.txt
+++ b/spec/tags/frozen/language/method_tags.txt
@@ -1,2 +1 @@
 fails:Calling a private setter method permits self as a receiver
-fails:Calling a method with splat operator * and non-Array value attempts to coerce it to Array if the object respond_to?(:to_ary)
diff --git a/spec/tags/frozen/language/return_tags.txt b/spec/tags/frozen/language/return_tags.txt
index 69d2ec9..19b53bc 100644
--- a/spec/tags/frozen/language/return_tags.txt
+++ b/spec/tags/frozen/language/return_tags.txt
@@ -1,4 +1,3 @@
 fails:The return keyword when passed a splat calls 'to_a' on the splatted value first
-fails:The return keyword when passed a splat calls 'to_ary' on the splatted value first
 fails:The return keyword within a block raises a LocalJumpError if there is no lexicaly enclosing method
 fails:The return keyword in a Thread raises a ThreadError if used to exit a thread
diff --git a/spec/tags/frozen/language/variables_tags.txt b/spec/tags/frozen/language/variables_tags.txt
index 946770c..c87209b 100644
--- a/spec/tags/frozen/language/variables_tags.txt
+++ b/spec/tags/frozen/language/variables_tags.txt
@@ -1,3 +1 @@
 fails:Basic assignment supports the {|r,| } form of block assignment
-fails:Assigning multiple values calls #to_ary on rhs arg if rhs has only a single arg
-fails:Assigning multiple values allows complex parallel assignment