Skip to content

Commit eb9bd3c

Browse files
committed
patternTranslate returns ast::ExpressionPtr
1 parent 1741f1e commit eb9bd3c

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

parser/prism/Translator.cc

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,7 +3777,7 @@ const uint8_t *endLoc(pm_node_t *anyNode) {
37773777
//
37783778
// E.g. `PM_LOCAL_VARIABLE_TARGET_NODE` normally translates to `parser::LVarLhs`, but `parser::MatchVar` in the context
37793779
// of a pattern.
3780-
unique_ptr<ExprOnly> Translator::patternTranslate(pm_node_t *node) {
3780+
ast::ExpressionPtr Translator::patternTranslate(pm_node_t *node) {
37813781
if (node == nullptr)
37823782
return nullptr;
37833783

@@ -3792,7 +3792,7 @@ unique_ptr<ExprOnly> Translator::patternTranslate(pm_node_t *node) {
37923792

37933793
// Like array/hash patterns, MatchAlt is a structural pattern that doesn't have
37943794
// a simple desugared expression - it's handled specially during pattern matching desugaring
3795-
return expr_only(MK::Nil(location));
3795+
return MK::Nil(location);
37963796
}
37973797
case PM_ASSOC_NODE: { // A key-value pair in a Hash pattern, e.g. the `k: v` in `h in { k: v }
37983798
auto assocNode = down_cast<pm_assoc_node>(node);
@@ -3806,7 +3806,7 @@ unique_ptr<ExprOnly> Translator::patternTranslate(pm_node_t *node) {
38063806
auto value = desugar(assocNode->value);
38073807

38083808
// Pair is a structural component of hash patterns with no simple desugared expression
3809-
return expr_only(MK::Nil(location));
3809+
return MK::Nil(location);
38103810
}
38113811
case PM_ARRAY_PATTERN_NODE: { // An array pattern such as the `[head, *tail]` in the `a in [head, *tail]`
38123812
auto arrayPatternNode = down_cast<pm_array_pattern_node>(node);
@@ -3849,18 +3849,18 @@ unique_ptr<ExprOnly> Translator::patternTranslate(pm_node_t *node) {
38493849
auto sorbetConstant = desugar(prismConstant);
38503850

38513851
// ConstPattern wrapping the array pattern - the desugared expression is Nil as it's structural
3852-
return expr_only(MK::Nil(location));
3852+
return MK::Nil(location);
38533853
}
38543854

3855-
return expr_only(MK::Nil(patternLoc));
3855+
return MK::Nil(patternLoc);
38563856
}
38573857
case PM_CAPTURE_PATTERN_NODE: { // A variable capture such as the `Integer => i` in `in Integer => i`
38583858
auto capturePatternNode = down_cast<pm_capture_pattern_node>(node);
38593859

38603860
auto pattern = patternTranslate(capturePatternNode->value);
38613861
auto target = patternTranslate(up_cast(capturePatternNode->target));
38623862

3863-
return expr_only(MK::Nil(location));
3863+
return MK::Nil(location);
38643864
}
38653865
case PM_FIND_PATTERN_NODE: { // A find pattern such as the `[*, middle, *]` in the `a in [*, middle, *]`
38663866
auto findPatternNode = down_cast<pm_find_pattern_node>(node);
@@ -3883,7 +3883,7 @@ unique_ptr<ExprOnly> Translator::patternTranslate(pm_node_t *node) {
38833883
}
38843884

38853885
// FindPattern is a structural pattern with no simple desugared expression
3886-
return expr_only(MK::Nil(location));
3886+
return MK::Nil(location);
38873887
}
38883888
case PM_HASH_PATTERN_NODE: { // An hash pattern such as the `{ k: Integer }` in the `h in { k: Integer }`
38893889
auto hashPatternNode = down_cast<pm_hash_pattern_node>(node);
@@ -3929,10 +3929,10 @@ unique_ptr<ExprOnly> Translator::patternTranslate(pm_node_t *node) {
39293929
auto sorbetConstant = desugar(prismConstant);
39303930

39313931
// ConstPattern wrapping the hash pattern - the desugared expression is Nil as it's structural
3932-
return expr_only(MK::Nil(location));
3932+
return MK::Nil(location);
39333933
}
39343934

3935-
return expr_only(MK::Nil(patternLoc));
3935+
return MK::Nil(patternLoc);
39363936
}
39373937
case PM_IMPLICIT_NODE: {
39383938
auto implicitNode = down_cast<pm_implicit_node>(node);
@@ -3942,7 +3942,6 @@ unique_ptr<ExprOnly> Translator::patternTranslate(pm_node_t *node) {
39423942
auto inNode = down_cast<pm_in_node>(node);
39433943

39443944
auto prismPattern = inNode->pattern;
3945-
unique_ptr<ExprOnly> sorbetPattern;
39463945
ast::ExpressionPtr sorbetGuard;
39473946
auto statements = desugarStatements(inNode->statements);
39483947

@@ -3965,47 +3964,46 @@ unique_ptr<ExprOnly> Translator::patternTranslate(pm_node_t *node) {
39653964
conditionalStatements->body.size == 1,
39663965
"In pattern-matching's `in` clause, a conditional (if/unless) guard must have a single statement.");
39673966

3968-
sorbetPattern = patternTranslate(conditionalStatements->body.nodes[0]);
3967+
patternTranslate(conditionalStatements->body.nodes[0]);
39693968
} else {
3970-
sorbetPattern = patternTranslate(prismPattern);
3969+
patternTranslate(prismPattern);
39713970
}
39723971

39733972
// A single `in` clause does not desugar into a standalone Ruby expression; it only
39743973
// becomes meaningful when the enclosing `case` stitches together all clauses. Wrapping it
39753974
// in a NodeWithExpr seeded with `EmptyTree` satisfies the API contract so that
39763975
// `hasExpr(inNodes)` can succeed. The enclosing `case` later consumes the real
39773976
// expressions from the pattern and body when it assembles the final AST.
3978-
return empty_expr();
3977+
return MK::EmptyTree();
39793978
}
39803979
case PM_LOCAL_VARIABLE_TARGET_NODE: { // A variable binding in a pattern, like the `head` in `[head, *tail]`
39813980
auto localVarTargetNode = down_cast<pm_local_variable_target_node>(node);
39823981
auto name = translateConstantName(localVarTargetNode->name);
39833982

39843983
// For a match variable, the desugared expression is a local variable reference
39853984
// This represents what the variable will be bound to when the pattern matches
3986-
auto expr = MK::Local(location, name);
3987-
return expr_only(move(expr));
3985+
return MK::Local(location, name);
39883986
}
39893987
case PM_PINNED_EXPRESSION_NODE: { // A "pinned" expression, like `^(1 + 2)` in `in ^(1 + 2)`
39903988
auto pinnedExprNode = down_cast<pm_pinned_expression_node>(node);
39913989

39923990
auto expr = desugar(pinnedExprNode->expression);
39933991

3994-
return expr_only(MK::Nil(location));
3992+
return MK::Nil(location);
39953993
}
39963994
case PM_PINNED_VARIABLE_NODE: { // A "pinned" variable, like `^x` in `in ^x`
39973995
auto pinnedVarNode = down_cast<pm_pinned_variable_node>(node);
39983996

39993997
auto variable = desugar(pinnedVarNode->variable);
40003998

4001-
return expr_only(MK::Nil(location));
3999+
return MK::Nil(location);
40024000
}
40034001
case PM_SPLAT_NODE: { // A splat, like `*a` in an array pattern
40044002
auto prismSplatNode = down_cast<pm_splat_node>(node);
40054003
auto expr = desugar(prismSplatNode->expression);
40064004

40074005
// MatchRest is a structural pattern component with no simple desugared expression
4008-
return expr_only(MK::Nil(location));
4006+
return MK::Nil(location);
40094007
}
40104008
case PM_SYMBOL_NODE: { // A symbol literal, e.g. `:foo`, or `a:` in `{a: 1}`
40114009
auto symNode = down_cast<pm_symbol_node>(node);
@@ -4025,10 +4023,10 @@ unique_ptr<ExprOnly> Translator::patternTranslate(pm_node_t *node) {
40254023
// no-op: leave the whole location as-is.
40264024
}
40274025

4028-
return expr_only(MK::Symbol(location, content));
4026+
return MK::Symbol(location, content);
40294027
}
40304028
default: {
4031-
return expr_only(desugar(node));
4029+
return desugar(node);
40324030
}
40334031
}
40344032
}

parser/prism/Translator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class Translator final {
227227

228228
// Pattern-matching
229229
// ... variation of the main translation functions for pattern-matching related node.
230-
std::unique_ptr<ExprOnly> patternTranslate(pm_node_t *node);
230+
ast::ExpressionPtr patternTranslate(pm_node_t *node);
231231
ast::ExpressionPtr desugarOnelinePattern(core::LocOffsets loc, pm_node_t *match);
232232

233233
std::string_view sliceLocation(pm_location_t loc) const;

0 commit comments

Comments
 (0)