From 48af9c5fd26acd5055ac6ce86448b1de95171882 Mon Sep 17 00:00:00 2001
From: Daniel Luz <dev@mernen.com>
Date: Fri, 6 Feb 2009 01:51:17 -0200
Subject: [PATCH] Instead of whitelisting rewrites of splat, list the cases that are left unchanged.
Also fixes `[*x]` when used inside certain constructs, mostly other literals.
---
lib/compiler/sydney_rewriter.rb | 8 ++++----
spec/compiler/array_spec.rb | 14 ++++++++++++++
spec/compiler/hash_spec.rb | 19 +++++++++++++++++++
3 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/lib/compiler/sydney_rewriter.rb b/lib/compiler/sydney_rewriter.rb
index 8838a5f..bbedbf9 100644
--- a/lib/compiler/sydney_rewriter.rb
+++ b/lib/compiler/sydney_rewriter.rb
@@ -366,13 +366,13 @@ class Rubinius::SydneyRewriter
def rewrite_splat(exp)
case context.last
- when nil, :fcall, :break, :lasgn, :next, :return
- return s(:array, exp)
+ when :svalue, :resbody, :super, :argspush, :masgn
+ return exp
when :array
- return s(:array, exp) if context[-2] == :fcall
+ return exp if [:masgn, :super].include?(context[-2])
end
- exp
+ s(:array, exp)
end
def rewrite_super(exp)
diff --git a/spec/compiler/array_spec.rb b/spec/compiler/array_spec.rb
index 41350f6..8e7c345 100644
--- a/spec/compiler/array_spec.rb
+++ b/spec/compiler/array_spec.rb
@@ -113,6 +113,20 @@ describe "An Array node" do
end
end
+ relates "[[*1]]" do
+ parse do
+ [:array, [:array, [:splat, [:lit, 1]]]]
+ end
+
+ compile do |g|
+ g.make_array 0
+ g.push 1
+ g.cast_array
+ g.send :+, 1
+ g.make_array 1
+ end
+ end
+
relates "[1, *2]" do
parse do
[:array, [:lit, 1], [:splat, [:lit, 2]]]
diff --git a/spec/compiler/hash_spec.rb b/spec/compiler/hash_spec.rb
index 2ddc5f5..f778350 100644
--- a/spec/compiler/hash_spec.rb
+++ b/spec/compiler/hash_spec.rb
@@ -39,4 +39,23 @@ describe "A Hash node" do
g.send :[], 2
end
end
+
+ relates "{ 1 => [*1] }" do
+ parse do
+ [:hash, [:lit, 1], [:array, [:splat, [:lit, 1]]]]
+ end
+
+ compile do |g|
+ g.push_cpath_top
+ g.find_const :Hash
+ g.push 1
+
+ g.make_array 0
+ g.push 1
+ g.cast_array
+ g.send :+, 1
+
+ g.send :[], 2
+ end
+ end
end
--
1.6.1.2