Skip to content

Commit c8bbe57

Browse files
committed
Change TranslatorEnvironment#findLocalVarNode to raise exception if variable is not found
1 parent 6484582 commit c8bbe57

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public int findFrameSlot(Object name) {
264264
}
265265

266266
public ReadLocalNode findOrAddLocalVarNodeDangerous(String name, SourceIndexLength sourceSection) {
267-
ReadLocalNode localVar = findLocalVarNode(name, sourceSection);
267+
ReadLocalNode localVar = findLocalVarNodeOrNull(name, sourceSection);
268268

269269
if (localVar == null) {
270270
declareVar(name);
@@ -287,7 +287,7 @@ public ReadLocalVariableNode readNode(int slot, Nodes.Node yarpNode) {
287287
}
288288

289289
public RubyNode findLocalVarOrNilNode(String name, SourceIndexLength sourceSection) {
290-
RubyNode node = findLocalVarNode(name, sourceSection);
290+
RubyNode node = findLocalVarNodeOrNull(name, sourceSection);
291291
if (node == null) {
292292
node = new NilLiteralNode();
293293
node.unsafeSetSourceSection(sourceSection);
@@ -296,6 +296,16 @@ public RubyNode findLocalVarOrNilNode(String name, SourceIndexLength sourceSecti
296296
}
297297

298298
public ReadLocalNode findLocalVarNode(String name, SourceIndexLength sourceSection) {
299+
final ReadLocalNode node = findLocalVarNodeOrNull(name, sourceSection);
300+
301+
if (node == null) {
302+
throw CompilerDirectives.shouldNotReachHere(name + " local variable should be declared");
303+
}
304+
305+
return node;
306+
}
307+
308+
public ReadLocalNode findLocalVarNodeOrNull(String name, SourceIndexLength sourceSection) {
299309
assert name != null;
300310
TranslatorEnvironment current = this;
301311
int level = 0;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ public AssignableNode visitSplatNode(Nodes.SplatNode node) {
168168
public AssignableNode visitRequiredParameterNode(Nodes.RequiredParameterNode node) {
169169
final String name = node.name;
170170
final ReadLocalNode lhs = yarpTranslator.getEnvironment().findLocalVarNode(name, null);
171-
172-
assert lhs != null;
173-
174171
final RubyNode rhs = new DeadNode("YARPMultiTargetNodeTranslator#visitRequiredParameterNode");
175172
final WriteLocalNode rubyNode = lhs.makeWriteNode(rhs);
176173

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,6 @@ public RubyNode visitBlockArgumentNode(Nodes.BlockArgumentNode node) {
544544
if (node.expression == null) {
545545
// def foo(&) a(&) end
546546
valueNode = environment.findLocalVarNode(FORWARDED_BLOCK_NAME, null);
547-
assert valueNode != null : "block forwarding local variable should be declared";
548547
} else {
549548
// a(&:b)
550549
valueNode = node.expression.accept(this);
@@ -1888,7 +1887,6 @@ public RubyNode visitHashNode(Nodes.HashNode node) {
18881887
// bar(**)
18891888
// end
18901889
valueNode = environment.findLocalVarNode(DEFAULT_KEYWORD_REST_NAME, null);
1891-
assert valueNode != null : "keyrest forwarding local variable should be declared";
18921890
}
18931891

18941892
hashConcats.add(HashCastNodeGen.HashCastASTNodeGen.create(valueNode));
@@ -2435,7 +2433,6 @@ public RubyNode visitLocalVariableReadNode(Nodes.LocalVariableReadNode node) {
24352433
final String name = node.name;
24362434

24372435
final RubyNode rubyNode = environment.findLocalVarNode(name, null);
2438-
assert rubyNode != null : name;
24392436

24402437
return assignPositionAndFlags(node, rubyNode);
24412438
}
@@ -2492,9 +2489,6 @@ public RubyNode visitLocalVariableOrWriteNode(Nodes.LocalVariableOrWriteNode nod
24922489
public WriteLocalNode visitLocalVariableWriteNode(Nodes.LocalVariableWriteNode node) {
24932490
final String name = node.name;
24942491
final ReadLocalNode lhs = environment.findLocalVarNode(name, null);
2495-
2496-
assert lhs != null;
2497-
24982492
final RubyNode rhs = node.value.accept(this);
24992493
final WriteLocalNode rubyNode = lhs.makeWriteNode(rhs);
25002494

@@ -2506,9 +2500,6 @@ public WriteLocalNode visitLocalVariableWriteNode(Nodes.LocalVariableWriteNode n
25062500
public WriteLocalNode visitLocalVariableTargetNode(Nodes.LocalVariableTargetNode node) {
25072501
final String name = node.name;
25082502
final ReadLocalNode lhs = environment.findLocalVarNode(name, null);
2509-
2510-
assert lhs != null;
2511-
25122503
final RubyNode rhs = new DeadNode("YARPTranslator#visitLocalVariableTargetNode");
25132504
final WriteLocalNode rubyNode = lhs.makeWriteNode(rhs);
25142505

@@ -3066,7 +3057,6 @@ public RubyNode visitSplatNode(Nodes.SplatNode node) {
30663057

30673058
// no need for SplatCastNodeGen for * because it's always an Array and cannot be reassigned
30683059
rubyNode = environment.findLocalVarNode(DEFAULT_REST_NAME, null);
3069-
assert rubyNode != null : "rest forwarding local variable should be declared";
30703060
}
30713061

30723062
return assignPositionAndFlags(node, rubyNode);

0 commit comments

Comments
 (0)