Skip to content

Commit

Permalink
[GR-45043] Fix Hash value omission for constant names
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4279
  • Loading branch information
andrykonchin committed Jun 4, 2024
2 parents bdfd19d + 8fc9d13 commit 7a6f722
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Bug fixes:
* Fix parsing literal floats when the locale does not use `.` for the decimal separator (e.g. `LANG=fr_FR.UTF-8`) (#3512, @eregon).
* Fix `IO#{read_nonblock,readpartial,sysread}`, `BasicSocket#{recv,recv_nonblock}`, `{Socket,UDPSocket}#recvfrom_nonblock`, `UnixSocket#recvfrom` and preserve a provided buffer's encoding (#3506, @andrykonchyn).
* Repair `IO#{wait_readable,wait_writable,wait}` to be interruptible (#3504, @andrykonchin).
* Fix Hash value omission for constant names (@andrykonchin).

Compatibility:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
subject: "Hash"
description: "with omitted value and a constant"
notes: >
treats name as a constant
focused_on_node: "org.truffleruby.core.hash.library.PackedHashStoreLibrary$SmallHashLiteralNode"
ruby: |
A = 100500
{A:, b: 42}
ast: |
PackedHashStoreLibraryFactory$SmallHashLiteralNodeGen
attributes:
flags = 1
sourceCharIndex = 11
sourceLength = 11
children:
keyValues = [
ObjectLiteralNode
attributes:
flags = 0
object = :A
sourceCharIndex = 12
sourceLength = 2
ReadConstantWithLexicalScopeNode
attributes:
flags = 0
lexicalScope = :: Object
name = "A"
sourceCharIndex = 12
sourceLength = 2
children:
getConstantNode =
GetConstantNodeGen
lookupConstantNode =
LookupConstantWithLexicalScopeNodeGen
attributes:
lexicalScope = :: Object
name = "A"
ObjectLiteralNode
attributes:
flags = 0
object = :b
sourceCharIndex = 16
sourceLength = 2
IntegerFixnumLiteralNode
attributes:
flags = 0
sourceCharIndex = 19
sourceLength = 2
value = 42
]
9 changes: 5 additions & 4 deletions src/main/java/org/truffleruby/parser/YARPTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1963,20 +1963,21 @@ public RubyNode visitHashNode(Nodes.HashNode node) {

keyValues.add(keyNode);

// when value is omitted, e.g. `a = 1; {a: }`
if (assocNode.value instanceof Nodes.ImplicitNode implicit) {
if (implicit.value instanceof Nodes.CallNode call) {
// a special case for a method call because
// Prism doesn't set VARIABLE_CALL flag
int flags = call.flags | Nodes.CallNodeFlags.VARIABLE_CALL;
final var copy = new Nodes.CallNode((short) flags, call.receiver, call.name, call.arguments,
call.block, call.startOffset, call.length);

final RubyNode valueNode = copy.accept(this);
keyValues.add(valueNode);
} else if (implicit.value instanceof Nodes.LocalVariableReadNode localVariableRead) {
final RubyNode valueNode = localVariableRead.accept(this);
keyValues.add(valueNode);
} else {
throw CompilerDirectives.shouldNotReachHere();
// expect here Nodes.LocalVariableReadNode or Nodes.ConstantReadNode
final RubyNode valueNode = implicit.value.accept(this);
keyValues.add(valueNode);
}
} else {
keyValues.add(assocNode.value.accept(this));
Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestRubyLiteral.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
exclude :test_integer, "needs investigation"
exclude :test_hash_value_omission, "NameError: undefined local variable or method `FOO' for #<TestRubyLiteral:0x2914b8>"

0 comments on commit 7a6f722

Please sign in to comment.