Skip to content

Commit fc1ebbc

Browse files
committed
translateConst use expr_only and narrow return
1 parent ca9669e commit fc1ebbc

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

parser/prism/Translator.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ class ExprOnly final : public Node {
7575
}
7676
};
7777

78-
unique_ptr<parser::Node> expr_only(ast::ExpressionPtr expr) {
78+
unique_ptr<ExprOnly> expr_only(ast::ExpressionPtr expr) {
7979
return make_unique<ExprOnly>(move(expr), expr.loc());
8080
}
8181

82-
unique_ptr<parser::Node> expr_only(ast::ExpressionPtr expr, core::LocOffsets loc) {
82+
unique_ptr<ExprOnly> expr_only(ast::ExpressionPtr expr, core::LocOffsets loc) {
8383
return make_unique<ExprOnly>(move(expr), loc);
8484
}
8585

@@ -5476,15 +5476,15 @@ ast::ExpressionPtr Translator::desugarStatements(pm_statements_node *stmtsNode,
54765476
// Usually returns the `SorbetLHSNode`, but for constant writes and targets,
54775477
// it can can return an `LVarLhs` as a workaround in the case of a dynamic constant assignment.
54785478
template <typename PrismLhsNode, typename SorbetLHSNode, bool checkForDynamicConstAssign>
5479-
unique_ptr<parser::Node> Translator::translateConst(PrismLhsNode *node) {
5479+
unique_ptr<ExprOnly> Translator::translateConst(PrismLhsNode *node) {
54805480
static_assert(is_same_v<SorbetLHSNode, parser::Const> || is_same_v<SorbetLHSNode, parser::ConstLhs>,
54815481
"Invalid LHS type. Must be one of `parser::Const` or `parser::ConstLhs`.");
54825482

54835483
// Constant name might be unset, e.g. `::`.
54845484
if (node->name == PM_CONSTANT_ID_UNSET) {
54855485
auto location = translateLoc(node->base.location);
54865486
auto expr = MK::UnresolvedConstant(location, MK::EmptyTree(), core::Names::empty());
5487-
return make_node_with_expr<SorbetLHSNode>(move(expr), location, nullptr, core::Names::empty());
5487+
return expr_only(move(expr));
54885488
}
54895489

54905490
// It's important that in all branches `enterNameUTF8` is called, which `translateConstantName` does,
@@ -5509,7 +5509,7 @@ unique_ptr<parser::Node> Translator::translateConst(PrismLhsNode *node) {
55095509
// This is a copy of a workaround from `Desugar.cc`, which substitues in a fake assignment,
55105510
// so the parsing can continue. See other usages of `dynamicConstAssign` for more details.
55115511
auto expr = MK::Local(location, core::Names::dynamicConstAssign());
5512-
return make_node_with_expr<LVarLhs>(move(expr), location, core::Names::dynamicConstAssign());
5512+
return expr_only(move(expr));
55135513
}
55145514
}
55155515

@@ -5563,7 +5563,7 @@ unique_ptr<parser::Node> Translator::translateConst(PrismLhsNode *node) {
55635563
}
55645564

55655565
ast::ExpressionPtr desugaredExpr = MK::UnresolvedConstant(location, move(parentExpr), constantName);
5566-
return make_node_with_expr<SorbetLHSNode>(move(desugaredExpr), location, move(parent), constantName);
5566+
return expr_only(move(desugaredExpr));
55675567
}
55685568

55695569
core::NameRef Translator::translateConstantName(pm_constant_id_t constant_id) {

parser/prism/Translator.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace sorbet::parser::Prism {
1717
// Used by the desugar*OpAssign functions to determine the appropriate desugaring strategy.
1818
enum class OpAssignKind { And, Or, Operator };
1919

20+
class ExprOnly;
21+
2022
class Translator final {
2123
const Parser &parser;
2224
// This context holds a reference to the GlobalState allocated up the call stack, which is why we don't allow
@@ -163,7 +165,7 @@ class Translator final {
163165
std::unique_ptr<parser::Node> rhs);
164166

165167
template <typename PrismLhsNode, typename SorbetLHSNode, bool checkForDynamicConstAssign = false>
166-
std::unique_ptr<parser::Node> translateConst(PrismLhsNode *node);
168+
std::unique_ptr<ExprOnly> translateConst(PrismLhsNode *node);
167169
core::NameRef translateConstantName(pm_constant_id_t constantId);
168170

169171
// Generates a unique name for a `parser::Node`.

0 commit comments

Comments
 (0)