From e2036b39fe4d21d8261883a13df48fe8ac7338d2 Mon Sep 17 00:00:00 2001 From: ahogappa Date: Thu, 23 Apr 2026 11:57:53 +0900 Subject: [PATCH] Fix crash on empty parentheses `()` in AST conversion `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) --- lib/typeprof/core/ast.rb | 1 + scenario/misc/parens.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 scenario/misc/parens.rb diff --git a/lib/typeprof/core/ast.rb b/lib/typeprof/core/ast.rb index 43a43945..9b0ee743 100644 --- a/lib/typeprof/core/ast.rb +++ b/lib/typeprof/core/ast.rb @@ -61,6 +61,7 @@ def self.create_node(raw_node, lenv, use_result = true, allow_meta = false) while true case raw_node.type when :parentheses_node + return DummyNilNode.new(lenv.code_range_from_node(raw_node), lenv) if raw_node.body.nil? raw_node = raw_node.body when :implicit_node raw_node = raw_node.value diff --git a/scenario/misc/parens.rb b/scenario/misc/parens.rb new file mode 100644 index 00000000..2c104f34 --- /dev/null +++ b/scenario/misc/parens.rb @@ -0,0 +1,30 @@ +## update +def grouping + (1 + 2) +end + +def nested + ((3)) +end + +def empty + () +end + +def with_default(x = ()) + x +end + +grouping +nested +empty +with_default +with_default(1) + +## assert +class Object + def grouping: -> Integer + def nested: -> Integer + def empty: -> nil + def with_default: (?Integer?) -> Integer? +end