Skip to content

Commit a5d8cac

Browse files
Use static names for anonymous parameters
The original RBS rewriter runs after the parser has assigned unique names to anonymous parameters, which it can then pull out of the method definition. With the Prism RBS rewriter, we can't generate unique names until later on when the translator is run. We can use static names instead as `*`, `**`, and `&` are guaranteed to be unique anyway (in fact this is already the case for the block param).
1 parent c4688c1 commit a5d8cac

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

parser/prism/Translator.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,7 +2840,7 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node, bool preserveCon
28402840
constexpr uint32_t length = "**"sv.size();
28412841
kwrestLoc = core::LocOffsets{location.beginPos() + length, location.endPos()};
28422842
} else { // An anonymous keyword rest parameter, like `def foo(**)`
2843-
sorbetName = nextUniqueParserName(core::Names::starStar());
2843+
sorbetName = core::Names::starStar();
28442844

28452845
// This location *does* include the whole `**`.
28462846
kwrestLoc = location;
@@ -4208,7 +4208,7 @@ Translator::translateParametersNode(pm_parameters_node *paramsNode, core::LocOff
42084208
constexpr uint32_t length = "&"sv.size();
42094209
blockParamLoc = core::LocOffsets{blockParamLoc.beginPos() + length, blockParamLoc.endPos()};
42104210
} else { // An anonymous block parameter, like `def foo(&)`
4211-
enclosingBlockParamName = nextUniqueParserName(core::Names::ampersand());
4211+
enclosingBlockParamName = core::Names::ampersand();
42124212
}
42134213

42144214
auto blockParamExpr = MK::BlockParam(blockParamLoc, MK::Local(blockParamLoc, enclosingBlockParamName));
@@ -5408,10 +5408,6 @@ core::NameRef Translator::translateConstantName(pm_constant_id_t constant_id) {
54085408
return ctx.state.enterNameUTF8(parser.resolveConstant(constant_id));
54095409
}
54105410

5411-
core::NameRef Translator::nextUniqueParserName(core::NameRef original) {
5412-
return ctx.state.freshNameUnique(core::UniqueNameKind::Parser, original, ++parserUniqueCounter);
5413-
}
5414-
54155411
core::NameRef Translator::nextUniqueDesugarName(core::NameRef original) {
54165412
ENFORCE(directlyDesugar, "This shouldn't be called if we're not directly desugaring.");
54175413
return ctx.state.freshNameUnique(core::UniqueNameKind::Desugar, original, ++desugarUniqueCounter);

parser/prism/Translator.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ class Translator final {
168168
std::unique_ptr<parser::Node> translateConst(PrismLhsNode *node);
169169
core::NameRef translateConstantName(pm_constant_id_t constantId);
170170

171-
// Generates a unique name for a `parser::Node`.
172-
core::NameRef nextUniqueParserName(core::NameRef original);
173-
174171
// Generates a unique name for a directly desugared `ast::ExpressionPtr`.
175172
core::NameRef nextUniqueDesugarName(core::NameRef original);
176173

rbs/prism/MethodTypeToParserNodePrism.cc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,21 @@ pm_node_t *MethodTypeToParserNodePrism::methodSignature(const pm_node_t *methodD
702702
if (!methodArgs.empty() && paramIndex < methodArgs.size()) {
703703
auto methodArg = methodArgs[paramIndex];
704704

705-
// Special case: anonymous block parameter (&) should use symbol :&
706-
if (arg.kind == RBSArg::Kind::Block && methodArg.nameId == PM_CONSTANT_ID_UNSET) {
707-
symbolNode = prism.Symbol(tinyLocOffsets, "&"sv);
708-
} else if (methodArg.nameId != PM_CONSTANT_ID_UNSET) {
705+
if (methodArg.nameId == PM_CONSTANT_ID_UNSET) {
706+
switch (arg.kind) {
707+
case RBSArg::Kind::RestKeyword:
708+
symbolNode = prism.Symbol(tinyLocOffsets, "**"sv);
709+
break;
710+
case RBSArg::Kind::RestPositional:
711+
symbolNode = prism.Symbol(tinyLocOffsets, "*"sv);
712+
break;
713+
case RBSArg::Kind::Block:
714+
symbolNode = prism.Symbol(tinyLocOffsets, "&"sv);
715+
break;
716+
default:
717+
break;
718+
}
719+
} else {
709720
symbolNode = prism.SymbolFromConstant(tinyLocOffsets, methodArg.nameId);
710721
}
711722
}

0 commit comments

Comments
 (0)