Skip to content

Fix crash on empty parentheses () in AST conversion#441

Merged
mame merged 1 commit intoruby:masterfrom
ahogappa:fix/empty-parentheses-node
Apr 28, 2026
Merged

Fix crash on empty parentheses () in AST conversion#441
mame merged 1 commit intoruby:masterfrom
ahogappa:fix/empty-parentheses-node

Conversation

@ahogappa
Copy link
Copy Markdown
Contributor

Problem

TypeProf::Core::AST.create_node crashes with NoMethodError on the empty parentheses expression ():

require "typeprof"
TypeProf::Core::Service.new({}).update_rb_file("t.rb", "()")
# => lib/typeprof/core/ast.rb:62:in 'create_node':
#    undefined method 'type' for nil (NoMethodError)
#            case raw_node.type

`AST.create_node` unwraps `:parentheses_node` by assigning
`raw_node = raw_node.body`, but for `()` the body is nil, causing a
NoMethodError on the subsequent `raw_node.type` dispatch. Return
`DummyNilNode` when the body is nil, following the existing convention
for empty syntactic slots that evaluate to nil.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sinsoku
Copy link
Copy Markdown
Collaborator

sinsoku commented Apr 26, 2026

I think it's a good change.

It might also be worth testing a couple more AST paths where DummyNilNode is received by a different parent node:

  • [()]DummyNilNode as an ArrayNode element
  • if (); 1; endDummyNilNode as a condition

Separate issue:
foo(()) (empty parens as a method argument) causes a different error:
RuntimeError: *anonymous_rest

This is unrelated to this PR, but it's worth addressing as a separate issue.

Copy link
Copy Markdown
Member

@mame mame left a comment

Choose a reason for hiding this comment

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

Good catch! DummyNilNode has gradually been pulled into more places than I originally intended, so I should probably revisit its design at some point. That said, returning it here is much better than crashing, so I'll go ahead and merge this. Thanks for the fix!

@mame mame merged commit ba9f9f6 into ruby:master Apr 28, 2026
6 checks passed
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.

3 participants