Skip to content

Commit

Permalink
add inspected value to NoMatchingPatternError
Browse files Browse the repository at this point in the history
  • Loading branch information
razetime committed Oct 25, 2022
1 parent 01373f9 commit c900330
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 0 additions & 3 deletions spec/tags/language/pattern_matching_tags.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
fails:Pattern matching raises NoMatchingPatternError if no pattern matches and no else clause
fails:Pattern matching guards raises NoMatchingPatternError if no guarded pattern matches and no else clause
fails:Pattern matching variable pattern supports using any name with _ at the beginning in a pattern several times
fails:Pattern matching variable pattern supports existing variables in a pattern specified with ^ operator
fails:Pattern matching variable pattern allows applying ^ operator to bound variables
Expand All @@ -13,7 +11,6 @@ fails:Pattern matching Array pattern supports form Constant(pat, pat, ...)
fails:Pattern matching Hash pattern supports form id: pat, id: pat, ...
fails:Pattern matching Hash pattern supports a: which means a: a
fails:Pattern matching Hash pattern can mix key (a:) and key-value (a: b) declarations
fails:Pattern matching Hash pattern raise SyntaxError when keys duplicate in pattern
fails:Pattern matching Hash pattern does not match object if Constant === object returns false
fails:Pattern matching Hash pattern does not match object without #deconstruct_keys method
fails:Pattern matching Hash pattern does not match object if #deconstruct_keys method does not return Hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
import org.truffleruby.language.RubyContextSourceNode;

import com.oracle.truffle.api.frame.VirtualFrame;
import org.truffleruby.language.RubyNode;

public final class CheckIfPatternsMatchedNode extends RubyContextSourceNode {
public CheckIfPatternsMatchedNode() {

RubyNode inspected;

public CheckIfPatternsMatchedNode(RubyNode inspected) {
this.inspected = inspected;
}

@Override
public Object execute(VirtualFrame frame) {
throw new RaiseException(getContext(), coreExceptions().noMatchingPatternError("No Matching Pattern", this));
String message = inspected.execute(frame).toString();
throw new RaiseException(getContext(), coreExceptions().noMatchingPatternError(message, this));
}
}
16 changes: 13 additions & 3 deletions src/main/java/org/truffleruby/parser/BodyTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -850,10 +850,20 @@ public RubyNode visitCaseNode(CaseParseNode node) {
@Override
public RubyNode visitCaseInNode(CaseInParseNode node) {
final SourceIndexLength sourceSection = node.getPosition();

RubyNode caseExprValue = node.getCaseNode().accept(this);
RubyNode elseNode;
if (node.getElseNode() == null) {
elseNode = new CheckIfPatternsMatchedNode();
RubyCallNodeParameters inspectCallParameters = new RubyCallNodeParameters(
caseExprValue,
"inspect",
null,
EmptyArgumentsDescriptor.INSTANCE,
new RubyNode[0]{},
false,
true);
RubyNode inspected = language.coreMethodAssumptions
.createCallNode(inspectCallParameters, environment);
elseNode = new CheckIfPatternsMatchedNode(inspected);
} else {
elseNode = translateNodeOrNil(sourceSection, node.getElseNode());
}
Expand All @@ -867,7 +877,7 @@ public RubyNode visitCaseInNode(CaseInParseNode node) {

final int tempSlot = environment.declareLocalTemp("case in value");
final ReadLocalNode readTemp = environment.readNode(tempSlot, sourceSection);
final RubyNode assignTemp = readTemp.makeWriteNode(node.getCaseNode().accept(this));
final RubyNode assignTemp = readTemp.makeWriteNode(caseExprValue);

/* Build an if expression from the ins and else. Work backwards because the first if contains all the others in
* its else clause. */
Expand Down

0 comments on commit c900330

Please sign in to comment.