@@ -2449,9 +2449,7 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node, bool preserveCon
24492449 }
24502450 case PM_CONSTANT_PATH_NODE: { // Part of a constant path, like the `A::B` in `A::B::C`.
24512451 // See`PM_CONSTANT_READ_NODE`, which handles the `::C` part
2452- auto constantPathNode = down_cast<pm_constant_path_node>(node);
2453-
2454- return translateConst<pm_constant_path_node, parser::Const>(constantPathNode);
2452+ return expr_only (translateConst<pm_constant_path_node>(node));
24552453 }
24562454 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: { // Compound assignment to a constant path, e.g. `A::B += 1`
24572455 return expr_only (
@@ -2464,17 +2462,14 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node, bool preserveCon
24642462 }
24652463 case PM_CONSTANT_PATH_TARGET_NODE: { // Target of an indirect write to a constant path
24662464 // ... like `A::TARGET1, A::TARGET2 = 1, 2`, `rescue => A::TARGET`, etc.
2467- auto constantPathTargetNode = down_cast<pm_constant_path_target_node>(node);
2468-
2469- return translateConst<pm_constant_path_target_node, parser::ConstLhs>(constantPathTargetNode);
2465+ return expr_only (translateConst<pm_constant_path_target_node>(node));
24702466 }
24712467 case PM_CONSTANT_PATH_WRITE_NODE: { // Regular assignment to a constant path, e.g. `A::B = 1`
24722468 return expr_only (desugarAssignment<pm_constant_path_write_node>(node));
24732469 }
24742470 case PM_CONSTANT_TARGET_NODE: { // Target of an indirect write to a constant
24752471 // ... like `TARGET1, TARGET2 = 1, 2`, `rescue => TARGET`, etc.
2476- auto constantTargetNode = down_cast<pm_constant_target_node>(node);
2477- return translateConst<pm_constant_target_node, parser::ConstLhs>(constantTargetNode);
2472+ return expr_only (translateConst<pm_constant_target_node>(node));
24782473 }
24792474 case PM_CONSTANT_AND_WRITE_NODE: { // And-assignment to a constant, e.g. `C &&= false`
24802475 return expr_only (desugarConstantOpAssign<pm_constant_and_write_node, OpAssignKind::And>(node), location);
@@ -2487,8 +2482,7 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node, bool preserveCon
24872482 return expr_only (desugarConstantOpAssign<pm_constant_or_write_node, OpAssignKind::Or>(node), location);
24882483 }
24892484 case PM_CONSTANT_READ_NODE: { // A single, unnested, non-fully qualified constant like `Foo`
2490- auto constantReadNode = down_cast<pm_constant_read_node>(node);
2491- return translateConst<pm_constant_read_node, parser::Const>(constantReadNode);
2485+ return expr_only (translateConst<pm_constant_read_node>(node));
24922486 }
24932487 case PM_CONSTANT_WRITE_NODE: { // Regular assignment to a constant, e.g. `Foo = 1`
24942488 return expr_only (desugarAssignment<pm_constant_write_node>(node));
@@ -5475,16 +5469,14 @@ ast::ExpressionPtr Translator::desugarStatements(pm_statements_node *stmtsNode,
54755469//
54765470// Usually returns the `SorbetLHSNode`, but for constant writes and targets,
54775471// it can can return an `LVarLhs` as a workaround in the case of a dynamic constant assignment.
5478- template <typename PrismLhsNode, typename SorbetLHSNode, bool checkForDynamicConstAssign>
5479- unique_ptr<ExprOnly> Translator::translateConst (PrismLhsNode *node) {
5480- static_assert (is_same_v<SorbetLHSNode, parser::Const> || is_same_v<SorbetLHSNode, parser::ConstLhs>,
5481- " Invalid LHS type. Must be one of `parser::Const` or `parser::ConstLhs`." );
5472+ template <typename PrismLhsNode, bool checkForDynamicConstAssign>
5473+ ast::ExpressionPtr Translator::translateConst (pm_node_t *anyNode) {
5474+ auto node = down_cast<PrismLhsNode>(anyNode);
54825475
54835476 // Constant name might be unset, e.g. `::`.
54845477 if (node->name == PM_CONSTANT_ID_UNSET) {
54855478 auto location = translateLoc (node->base .location );
5486- auto expr = MK::UnresolvedConstant (location, MK::EmptyTree (), core::Names::empty ());
5487- return expr_only (move (expr));
5479+ return MK::UnresolvedConstant (location, MK::EmptyTree (), core::Names::empty ());
54885480 }
54895481
54905482 // It's important that in all branches `enterNameUTF8` is called, which `translateConstantName` does,
@@ -5508,8 +5500,7 @@ unique_ptr<ExprOnly> Translator::translateConst(PrismLhsNode *node) {
55085500 // Check if this is a dynamic constant assignment (SyntaxError at runtime)
55095501 // This is a copy of a workaround from `Desugar.cc`, which substitues in a fake assignment,
55105502 // so the parsing can continue. See other usages of `dynamicConstAssign` for more details.
5511- auto expr = MK::Local (location, core::Names::dynamicConstAssign ());
5512- return expr_only (move (expr));
5503+ return MK::Local (location, core::Names::dynamicConstAssign ());
55135504 }
55145505 }
55155506
@@ -5520,8 +5511,7 @@ unique_ptr<ExprOnly> Translator::translateConst(PrismLhsNode *node) {
55205511 is_same_v<PrismLhsNode, pm_constant_path_write_node> ||
55215512 is_same_v<PrismLhsNode, pm_constant_path_node>;
55225513
5523- unique_ptr<parser::Node> parent;
5524- ast::ExpressionPtr parentExpr = nullptr ;
5514+ ast::ExpressionPtr parentExpr;
55255515
55265516 if constexpr (isConstantPath) { // Handle constant paths, has a parent node that needs translation.
55275517 if (auto *prismParentNode = node->parent ) {
@@ -5533,11 +5523,9 @@ unique_ptr<ExprOnly> Translator::translateConst(PrismLhsNode *node) {
55335523 // A::B ::C
55345524 // / \
55355525 // A ::B
5536- parent = translate (prismParentNode);
5537- parentExpr = parent ? parent->takeDesugaredExpr () : nullptr ;
5526+ parentExpr = takeDesugaredExprOrEmptyTree (translate (prismParentNode));
55385527 } else { // This is the root of a fully qualified constant reference, like `::A`.
55395528 auto delimiterLoc = translateLoc (node->delimiter_loc ); // The location of the `::`
5540- parent = make_unique<parser::Cbase>(delimiterLoc);
55415529 parentExpr = MK::Constant (delimiterLoc, core::Symbols::root ());
55425530 }
55435531 } else { // Handle plain constants like `A`, that aren't part of a constant path.
@@ -5554,16 +5542,10 @@ unique_ptr<ExprOnly> Translator::translateConst(PrismLhsNode *node) {
55545542 is_same_v<PrismLhsNode, pm_constant_write_node>) {
55555543 location = translateLoc (node->name_loc );
55565544 }
5557- parent = nullptr ;
5558- parentExpr = MK::EmptyTree ();
5559- }
5560-
5561- if (parentExpr == nullptr ) {
55625545 parentExpr = MK::EmptyTree ();
55635546 }
55645547
5565- ast::ExpressionPtr desugaredExpr = MK::UnresolvedConstant (location, move (parentExpr), constantName);
5566- return expr_only (move (desugaredExpr));
5548+ return MK::UnresolvedConstant (location, move (parentExpr), constantName);
55675549}
55685550
55695551core::NameRef Translator::translateConstantName (pm_constant_id_t constant_id) {
0 commit comments