Skip to content

Commit 3406d1d

Browse files
eregonandrykonchin
authored andcommitted
Fix checking pattern constant on the object and not on the deconstructed value
1 parent f4f24b4 commit 3406d1d

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

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

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public RubyNode visitCapturePatternNode(Nodes.CapturePatternNode node) {
9494

9595
@Override
9696
public RubyNode visitArrayPatternNode(Nodes.ArrayPatternNode node) {
97+
RubyNode condition;
98+
if (node.constant != null) { // Constant[a]
99+
condition = matchValue(node.constant);
100+
} else {
101+
condition = null;
102+
}
103+
97104
var preNodes = node.requireds;
98105
var restNode = node.rest;
99106
var postNodes = node.posts;
@@ -110,10 +117,13 @@ public RubyNode visitArrayPatternNode(Nodes.ArrayPatternNode node) {
110117
RubyNode outerPrev = currentValueToMatch;
111118
currentValueToMatch = readTemp;
112119
try {
113-
RubyNode condition = ArrayPatternLengthCheckNodeGen.create(preSize + postSize, restNode != null, readTemp);
114-
115-
if (node.constant != null) { // Constant[a]
116-
condition = AndNodeGen.create(matchValue(node.constant), condition);
120+
RubyNode check = YARPTranslator.sequence(Arrays.asList(
121+
assignTemp,
122+
ArrayPatternLengthCheckNodeGen.create(preSize + postSize, restNode != null, readTemp)));
123+
if (condition == null) {
124+
condition = check;
125+
} else {
126+
condition = AndNodeGen.create(condition, check);
117127
}
118128

119129
for (int i = 0; i < preNodes.length; i++) {
@@ -160,15 +170,21 @@ public RubyNode visitArrayPatternNode(Nodes.ArrayPatternNode node) {
160170
}
161171
}
162172

163-
RubyNode ret = YARPTranslator.sequence(Arrays.asList(assignTemp, condition));
164-
return assignPositionAndFlags(node, ret);
173+
return assignPositionAndFlags(node, condition);
165174
} finally {
166175
currentValueToMatch = outerPrev;
167176
}
168177
}
169178

170179
@Override
171180
public RubyNode visitHashPatternNode(Nodes.HashPatternNode node) {
181+
RubyNode condition;
182+
if (node.constant != null) { // Constant(a: 0)
183+
condition = matchValue(node.constant);
184+
} else {
185+
condition = null;
186+
}
187+
172188
Nodes.Node[] pairs = node.elements;
173189
RubySymbol[] keys = new RubySymbol[pairs.length];
174190
for (int i = 0; i < pairs.length; i++) {
@@ -200,11 +216,13 @@ public RubyNode visitHashPatternNode(Nodes.HashPatternNode node) {
200216
RubyNode outerPrev = currentValueToMatch;
201217
currentValueToMatch = readTemp;
202218
try {
203-
204-
RubyNode condition = HashPatternLengthCheckNodeGen.create(node.elements.length, readTemp);
205-
206-
if (node.constant != null) { // Constant[a: 0]
207-
condition = AndNodeGen.create(matchValue(node.constant), condition);
219+
RubyNode check = YARPTranslator.sequence(Arrays.asList(
220+
assignTemp,
221+
HashPatternLengthCheckNodeGen.create(node.elements.length, readTemp)));
222+
if (condition == null) {
223+
condition = check;
224+
} else {
225+
condition = AndNodeGen.create(condition, check);
208226
}
209227

210228
for (int i = 0; i < pairs.length; i++) {
@@ -220,10 +238,10 @@ public RubyNode visitHashPatternNode(Nodes.HashPatternNode node) {
220238
RubyNode prev = currentValueToMatch;
221239
currentValueToMatch = readValue;
222240
try {
223-
RubyNode check = YARPTranslator.sequence(assocNode, Arrays.asList(
241+
RubyNode valueCondition = YARPTranslator.sequence(assocNode, Arrays.asList(
224242
writeValue,
225243
AndNodeGen.create(new IsNotUndefinedNode(readValue), assocNode.value.accept(this))));
226-
condition = AndNodeGen.create(condition, check);
244+
condition = AndNodeGen.create(condition, valueCondition);
227245
} finally {
228246
currentValueToMatch = prev;
229247
}
@@ -251,8 +269,7 @@ public RubyNode visitHashPatternNode(Nodes.HashPatternNode node) {
251269
}
252270
}
253271

254-
RubyNode ret = YARPTranslator.sequence(Arrays.asList(assignTemp, condition));
255-
return assignPositionAndFlags(node, ret);
272+
return assignPositionAndFlags(node, condition);
256273
} finally {
257274
currentValueToMatch = outerPrev;
258275
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
exclude :test_alternative_pattern, "/home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:204:in `test_alternative_pattern'."
2+
exclude :test_hash_pattern, "Failed assertion, no message given."
23
exclude :test_invalid_syntax, "/home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:1269:in `test_invalid_syntax'."
34
exclude :test_literal_value_pattern, "/home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:347:in `test_literal_value_pattern'."
45
exclude :test_single_pattern_error_alternative_pattern, "Expected Exception(NoMatchingPatternError) was raised, but the message doesn't match."
@@ -8,8 +9,6 @@
89
exclude :test_single_pattern_error_hash_pattern, "Expected Exception(NoMatchingPatternError) was raised, but the message doesn't match."
910
exclude :test_single_pattern_error_value_pattern, "Expected Exception(NoMatchingPatternError) was raised, but the message doesn't match."
1011
exclude :test_var_pattern, "/home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:243:in `test_var_pattern'."
11-
exclude :test_array_pattern, "NoMatchingPatternError: #<TestPatternMatching::C:0x578 @obj=[0]>"
1212
exclude :test_deconstruct_cache, "SyntaxError: /home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:1451: YARPBlockNodeTranslator does not know how to translate FindPatternNode at /home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:1451"
1313
exclude :test_find_pattern, "SyntaxError: /home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:806: YARPDefNodeTranslator does not know how to translate FindPatternNode at /home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:806"
14-
exclude :test_hash_pattern, "NoMatchingPatternError: #<TestPatternMatching::C:0x778 @obj={:a=>0}>"
1514
exclude :test_single_pattern_error_find_pattern, "SyntaxError: /home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:1623: YARPBlockNodeTranslator does not know how to translate FindPatternNode at /home/eregon/code/truffleruby-ws/truffleruby/test/mri/tests/ruby/test_pattern_matching.rb:1623"

test/mri/excludes/TestPatternMatching/TestPatternMatchingRefinements.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)