Skip to content

Commit 5ce7e00

Browse files
committed
Translate constant reads/writes
1 parent 07bbd86 commit 5ce7e00

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

ast/desugar/Desugar.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@ ExpressionPtr node2TreeImpl(DesugarContext dctx, unique_ptr<parser::Node> what)
731731
// add entries here, without consulting the "node.*" counters from a
732732
// run over a representative code base.
733733
[&](parser::Const *const_) {
734+
TRANSLATED_BY_PRISM(dctx, const_);
735+
734736
auto scope = node2TreeImpl(dctx, std::move(const_->scope));
735737
ExpressionPtr res = MK::UnresolvedConstant(loc, std::move(scope), const_->name);
736738
result = std::move(res);
@@ -1586,11 +1588,15 @@ ExpressionPtr node2TreeImpl(DesugarContext dctx, unique_ptr<parser::Node> what)
15861588
result = std::move(res);
15871589
},
15881590
[&](parser::ConstLhs *constLhs) {
1591+
TRANSLATED_BY_PRISM(dctx, constLhs);
1592+
15891593
auto scope = node2TreeImpl(dctx, std::move(constLhs->scope));
15901594
ExpressionPtr res = MK::UnresolvedConstant(loc, std::move(scope), constLhs->name);
15911595
result = std::move(res);
15921596
},
15931597
[&](parser::Cbase *cbase) {
1598+
TRANSLATED_BY_PRISM(dctx, cbase);
1599+
15941600
ExpressionPtr res = MK::Constant(loc, core::Symbols::root());
15951601
result = std::move(res);
15961602
},

parser/prism/Translator.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,7 @@ unique_ptr<parser::Node> Translator::translateConst(PrismLhsNode *node, bool rep
18861886
// It's important that in all branches `enterNameUTF8` is called, which `translateConstantName` does,
18871887
// so that the name is available for the rest of the pipeline.
18881888
auto name = translateConstantName(node->name);
1889+
auto sorbetName = gs.enterNameConstant(name);
18891890

18901891
if (isInMethodDef && replaceWithDynamicConstAssign) {
18911892
// Check if this is a dynamic constant assignment (SyntaxError at runtime)
@@ -1899,6 +1900,8 @@ unique_ptr<parser::Node> Translator::translateConst(PrismLhsNode *node, bool rep
18991900
is_same_v<PrismLhsNode, pm_constant_path_node>;
19001901

19011902
std::unique_ptr<parser::Node> parent;
1903+
ast::ExpressionPtr parentExpr;
1904+
19021905
if constexpr (isConstantPath) { // Handle constant paths, has a parent node that needs translation.
19031906
if (auto prismParentNode = node->parent; prismParentNode != nullptr) {
19041907
// This constant reference is chained onto another constant reference.
@@ -1909,9 +1912,12 @@ unique_ptr<parser::Node> Translator::translateConst(PrismLhsNode *node, bool rep
19091912
// / \
19101913
// A ::B
19111914
parent = translate(prismParentNode);
1915+
parentExpr = parent ? parent->getCachedDesugaredExpr() : nullptr;
19121916
} else { // This is the root of a fully qualified constant reference, like `::A`.
19131917
auto delimiterLoc = translateLoc(node->delimiter_loc); // The location of the `::`
1918+
19141919
parent = make_node<parser::Cbase>(delimiterLoc);
1920+
parentExpr = MK::Constant(delimiterLoc, core::Symbols::root());
19151921
}
19161922
} else { // Handle plain constants like `A`, that aren't part of a constant path.
19171923
static_assert(
@@ -1928,9 +1934,16 @@ unique_ptr<parser::Node> Translator::translateConst(PrismLhsNode *node, bool rep
19281934
location = translateLoc(node->name_loc);
19291935
}
19301936
parent = nullptr;
1937+
parentExpr = MK::EmptyTree();
1938+
}
1939+
1940+
auto sorbetNode = make_node<SorbetLHSNode>(location, move(parent), sorbetName);
1941+
1942+
if (parentExpr != nullptr) {
1943+
sorbetNode->cacheDesugaredExpr(MK::UnresolvedConstant(location, move(parentExpr), sorbetName));
19311944
}
19321945

1933-
return make_node<SorbetLHSNode>(location, move(parent), gs.enterNameConstant(name));
1946+
return sorbetNode;
19341947
}
19351948

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

0 commit comments

Comments
 (0)