Skip to content

Commit 14813de

Browse files
committed
Refactor normalizing of _$n variable names and introduce a new String constant for "_$" literal
It's a follow up on #3745
1 parent 154e38f commit 14813de

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/main/java/org/truffleruby/language/arguments/ArgumentDescriptorUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.truffleruby.parser.ArgumentType;
2020

2121
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
22+
import org.truffleruby.parser.parser.ParserSupport;
2223

2324
public class ArgumentDescriptorUtils {
2425

@@ -48,7 +49,7 @@ private static RubyArray toArray(RubyLanguage language, RubyContext context, Arg
4849
store = new Object[]{ language.getSymbol(argType.symbolicName) };
4950
} else {
5051
// make sure to normalize parameter names to "_" if they start with "_$"
51-
if (name.startsWith("_$")) {
52+
if (name.startsWith(ParserSupport.UNDERSCORE_PREFIX)) {
5253
name = "_";
5354
}
5455

src/main/java/org/truffleruby/parser/parser/ParserSupport.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ public class ParserSupport {
160160
public static final String FORWARD_ARGS_BLOCK_VAR = Layouts.TEMP_PREFIX + "forward_block";
161161
public static final TruffleString FORWARD_ARGS_BLOCK_VAR_TSTRING = TStringUtils
162162
.usAsciiString(FORWARD_ARGS_BLOCK_VAR);
163+
/** A prefix for duplicated '_' local variables to build unique names */
164+
public static final String UNDERSCORE_PREFIX = "_$";
163165

164166
// Parser states:
165167
protected StaticScope currentScope;
@@ -1587,7 +1589,7 @@ public ArgumentParseNode arg_var(String string) {
15871589
if (name == "_") {
15881590
int count = 0;
15891591
while (current.exists(name) >= 0) {
1590-
name = ("_$" + count++).intern();
1592+
name = (UNDERSCORE_PREFIX + count++).intern(); // e.g. _$1, _$2, etc
15911593
}
15921594
}
15931595

0 commit comments

Comments
 (0)