Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle dynamic constant assignment #295

Merged
merged 2 commits into from
Oct 28, 2024

Conversation

amomchilov
Copy link

@amomchilov amomchilov commented Oct 22, 2024

Motivation

Closes #293 by reimplementing this workaround done by Sorbet's parser:

sorbet/parser/Builder.cc

Lines 348 to 354 in 97d7fff

} else if (auto *c = parser::cast_node<Const>(node.get())) {
if (driver_->lex.context.inDef) {
error(ruby_parser::dclass::DynamicConst, node->loc);
// Error recovery: lie and say it was an assign to something else so that the parse can continue
auto name = core::Names::dynamicConstAssign();
driver_->lex.declare(name.shortName(gs_));
return make_unique<LVarLhs>(c->loc, name);

parser/prism/Translator.h Outdated Show resolved Hide resolved
@amomchilov amomchilov force-pushed the Alex/handle-dynamic-constant-assignment branch 4 times, most recently from b31b872 to d384577 Compare October 28, 2024 14:13
@amomchilov amomchilov self-assigned this Oct 28, 2024
@amomchilov amomchilov requested review from egiurleo and st0012 October 28, 2024 14:17
Comment on lines +1641 to +1664
// Enter the name of the constant so that it's available for the rest of the pipeline
gs.enterNameConstant(name);
Copy link
Author

@amomchilov amomchilov Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could have extracted this up out of the conditional, like this:

auto prismName = parser.resolveConstant(node->name);
auto sorbetName = gs.enterNameConstant(name);

if (isInMethodDef) {
  return make_unique<LVarLhs>(location, core::Names::dynamicConstAssign());
}

return make_unique<SorbetLHSNode>(location, move(parent), sorbetName);

... but I opted not to, because it makes it look like sorbetName is an unused variable in the early-return. Repeating it here gives me a place to explicit document the side-effect of enterNameConstant that we're relying on.


Translator(Translator &&) = delete; // Move constructor
Translator(const Translator &) = delete; // Copy constructor
Translator &operator=(Translator &&) = delete; // Move assignment
Translator &operator=(const Translator &) = delete; // Copy assignment
public:
Translator(Parser parser, core::GlobalState &gs) : parser(parser), gs(gs) {} // Default constructor
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe this was never the default constructor, since it had parameters. Such are the perils of comments.

@@ -63,13 +69,16 @@ class Translator final {
std::unique_ptr<SorbetAssignmentNode> translateOpAssignment(pm_node_t *node);

template <typename PrismLhsNode, typename SorbetLHSNode>
std::unique_ptr<SorbetLHSNode> translateConst(PrismLhsNode *node);
std::unique_ptr<parser::Node> translateConst(PrismLhsNode *node);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to be less specific about the return type, to account for the case where if SorbetLHSNode is parser::CVarLHS, where we need to return a parser::LVarLhs for the workaround instead.

Copy link

@egiurleo egiurleo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woohoo! Looks like you might need a rebase.

@amomchilov amomchilov force-pushed the Alex/handle-dynamic-constant-assignment branch from d384577 to 029a23c Compare October 28, 2024 14:29
@amomchilov amomchilov enabled auto-merge October 28, 2024 14:29
@amomchilov amomchilov merged commit bc8caf7 into prism Oct 28, 2024
1 check passed
@amomchilov amomchilov deleted the Alex/handle-dynamic-constant-assignment branch October 28, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when handing dynamic constant assignments
3 participants