Skip to content

Commit ca9cfa9

Browse files
committed
[GR-18163] Fix #cloneUninitialized() for assignable nodes
PullRequest: truffleruby/4291
2 parents 59cf3f4 + 3eb8220 commit ca9cfa9

File tree

9 files changed

+76
-31
lines changed

9 files changed

+76
-31
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 2.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../ruby/spec_helper'
10+
11+
describe "RubyNode#cloneUninitialized() for nodes implementing AssignableNode interface" do
12+
# The Module#define_method with block argument triggers cloning of the proc and converting it into a lambda
13+
it "works for local variable assignment" do
14+
Module.new.define_method("foo") do
15+
rescue => e # rubocop:disable Lint/SuppressedException, Lint/UselessAssignment
16+
end
17+
18+
1.should == 1
19+
end
20+
21+
it "works for local variable assignment when it is already declared in an outer scope" do
22+
e = nil
23+
Module.new.define_method("foo") do
24+
rescue => e # rubocop:disable Lint/SuppressedException
25+
end
26+
27+
e.should == nil
28+
end
29+
30+
it "works for instance variable assignment" do
31+
Module.new.define_method("foo") do
32+
rescue => @e # rubocop:disable Lint/SuppressedException
33+
end
34+
35+
1.should == 1
36+
end
37+
38+
it "works for class variable assignment" do
39+
Module.new.define_method("foo") do
40+
rescue => @@e # rubocop:disable Lint/SuppressedException
41+
end
42+
43+
1.should == 1
44+
end
45+
46+
it "works for global variable assignment" do
47+
Module.new.define_method("foo") do
48+
rescue => $clonning_uninitialized_spec # rubocop:disable Lint/SuppressedException
49+
end
50+
51+
$clonning_uninitialized_spec.should == nil
52+
end
53+
54+
it "works for a constant assignment" do
55+
m = Module.new
56+
m.define_method("foo") do
57+
rescue => m::E # rubocop:disable Lint/SuppressedException
58+
end
59+
60+
1.should == 1
61+
end
62+
63+
it "works for a multi-assignment" do
64+
a = nil
65+
Module.new.define_method("foo") do
66+
a, b, c = [] # rubocop:disable Lint/UselessAssignment
67+
end
68+
69+
a.should == nil
70+
end
71+
end

spec/truffle/parsing/fixtures/rescue/capturing/with_local_variable.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@ ast: |
7272
frameSlot = 2 # error
7373
sourceCharIndex = 37
7474
sourceLength = 5
75-
children:
76-
valueNode =
77-
DeadNode
78-
attributes:
79-
flags = 0
80-
reason = "YARPTranslator#visitLocalVariableTargetNode"
81-
sourceCharIndex = -1
82-
sourceLength = 0
8375
IntegerFixnumLiteralNode
8476
attributes:
8577
flags = 1

spec/truffle/parsing/fixtures/rescue/with_exception_and_variable.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ ast: |
7575
frameSlot = 2 # error
7676
sourceCharIndex = 37
7777
sourceLength = 5
78-
children:
79-
valueNode =
80-
DeadNode
81-
attributes:
82-
flags = 0
83-
reason = "YARPTranslator#visitLocalVariableTargetNode"
84-
sourceCharIndex = -1
85-
sourceLength = 0
8678
IntegerFixnumLiteralNode
8779
attributes:
8880
flags = 1

spec/truffle/parsing/fixtures/rescue/without_exception_but_with_variable.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ ast: |
4646
frameSlot = 2 # error
4747
sourceCharIndex = 24
4848
sourceLength = 5
49-
children:
50-
valueNode =
51-
DeadNode
52-
attributes:
53-
flags = 0
54-
reason = "YARPTranslator#visitLocalVariableTargetNode"
55-
sourceCharIndex = -1
56-
sourceLength = 0
5749
IntegerFixnumLiteralNode
5850
attributes:
5951
flags = 1

src/main/java/org/truffleruby/language/constants/WriteConstantNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public RubyNode cloneUninitialized() {
112112
var copy = new WriteConstantNode(
113113
name,
114114
moduleNode.cloneUninitialized(),
115-
valueNode.cloneUninitialized());
115+
cloneUninitialized(valueNode));
116116
return copy.copyFlags(this);
117117
}
118118

src/main/java/org/truffleruby/language/locals/WriteLocalVariableNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public String toString() {
6565
public RubyNode cloneUninitialized() {
6666
var copy = new WriteLocalVariableNode(
6767
frameSlot,
68-
valueNode.cloneUninitialized());
68+
cloneUninitialized(valueNode));
6969
return copy.copyFlags(this);
7070
}
7171

src/main/java/org/truffleruby/language/objects/classvariables/WriteClassVariableNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public RubyNode cloneUninitialized() {
7979
var copy = new WriteClassVariableNode(
8080
lexicalScopeNode.cloneUninitialized(),
8181
name,
82-
rhs.cloneUninitialized());
82+
cloneUninitialized(rhs));
8383
return copy.copyFlags(this);
8484
}
8585

src/main/java/org/truffleruby/parser/YARPMultiTargetNodeTranslator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ public AssignableNode visitSplatNode(Nodes.SplatNode node) {
229229
public AssignableNode visitRequiredParameterNode(Nodes.RequiredParameterNode node) {
230230
final String name = node.name;
231231
final ReadLocalNode lhs = yarpTranslator.getEnvironment().findLocalVarNode(name);
232-
final RubyNode rhs = new DeadNode("YARPMultiTargetNodeTranslator#visitRequiredParameterNode");
233-
final WriteLocalNode rubyNode = lhs.makeWriteNode(rhs);
232+
final WriteLocalNode rubyNode = lhs.makeWriteNode(null);
234233

235234
return rubyNode.toAssignableNode();
236235
}

src/main/java/org/truffleruby/parser/YARPTranslator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,8 +2654,7 @@ public WriteLocalNode visitLocalVariableWriteNode(Nodes.LocalVariableWriteNode n
26542654
public WriteLocalNode visitLocalVariableTargetNode(Nodes.LocalVariableTargetNode node) {
26552655
final String name = node.name;
26562656
final ReadLocalNode lhs = environment.findLocalVarNode(name);
2657-
final RubyNode rhs = new DeadNode("YARPTranslator#visitLocalVariableTargetNode");
2658-
final WriteLocalNode rubyNode = lhs.makeWriteNode(rhs);
2657+
final WriteLocalNode rubyNode = lhs.makeWriteNode(null);
26592658

26602659
assignPositionAndFlags(node, rubyNode);
26612660
return rubyNode;

0 commit comments

Comments
 (0)