Skip to content

Commit 56ea0ce

Browse files
sinsokuclaude
authored andcommitted
Fix NotImplementedError in SigTyProcNode for RBS proc types
Previously, using proc types in RBS files (e.g., ^() -> Integer) would raise NotImplementedError when TypeProf tried to process them through SigTyProcNode#covariant_vertex0. This commit: - Adds initialize method to SigTyProcNode to properly parse RBS::Types::Proc - Implements covariant_vertex0 and contravariant_vertex0 methods - Wraps RBS::Types::Function in RBS::MethodType for compatibility - Adds comprehensive test scenarios for proc type handling The implementation currently returns the base Proc type without detailed function signature information, which can be enhanced in the future. 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b24ef21 commit 56ea0ce

2 files changed

Lines changed: 99 additions & 3 deletions

File tree

lib/typeprof/core/ast/sig_type.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,16 +658,39 @@ def show
658658
end
659659

660660
class SigTyProcNode < SigTyNode
661+
def initialize(raw_decl, lenv)
662+
super(raw_decl, lenv)
663+
# raw_decl.type is an RBS::Types::Function, we need to wrap it in a MethodType
664+
if raw_decl.type
665+
method_type = RBS::MethodType.new(
666+
type: raw_decl.type,
667+
type_params: [],
668+
block: raw_decl.block,
669+
location: raw_decl.location
670+
)
671+
@type = AST.create_rbs_func_type(method_type, nil, raw_decl.block, lenv)
672+
else
673+
@type = nil
674+
end
675+
end
676+
677+
attr_reader :type
678+
def subnodes = { type: }
679+
661680
def covariant_vertex0(genv, changes, vtx, subst)
662-
raise NotImplementedError
681+
# For now, just return the base Proc type without the function signature details
682+
# TODO: Create a proper Type::Proc with the function signature
683+
changes.add_edge(genv, Source.new(genv.proc_type), vtx)
663684
end
664685

665686
def contravariant_vertex0(genv, changes, vtx, subst)
666-
Source.new()
687+
# For now, just return the base Proc type without the function signature details
688+
# TODO: Create a proper Type::Proc with the function signature
689+
changes.add_edge(genv, Source.new(genv.proc_type), vtx)
667690
end
668691

669692
def show
670-
"(...proc...)"
693+
"^(...)"
671694
end
672695
end
673696

scenario/rbs/proc.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## update: test.rbs
2+
class Foo
3+
@proc: ^() -> Integer
4+
5+
def set_proc: (^(String) -> Integer) -> void
6+
def get_proc: () -> ^() -> String
7+
end
8+
9+
## update: test.rb
10+
class Foo
11+
def initialize
12+
@proc = -> { 42 }
13+
end
14+
15+
def set_proc(p)
16+
@proc = p
17+
end
18+
19+
def get_proc
20+
-> { "hello" }
21+
end
22+
end
23+
24+
## assert
25+
class Foo
26+
def initialize: -> Proc
27+
def set_proc: (Proc) -> Proc
28+
def get_proc: -> Proc
29+
end
30+
31+
## update: test.rbs
32+
class Object
33+
def take_proc: (^(Integer) -> String) -> void
34+
def call_proc: (^() -> Integer) -> Integer
35+
end
36+
37+
## update: test.rb
38+
def take_proc(p)
39+
p.call(42)
40+
end
41+
42+
def call_proc(p)
43+
p.call
44+
end
45+
46+
## assert
47+
class Object
48+
def take_proc: (Proc) -> untyped
49+
def call_proc: (Proc) -> untyped
50+
end
51+
52+
## update: test.rbs
53+
class Bar
54+
def with_block: () { () -> Integer } -> void
55+
def proc_arg: (^(String) -> Integer) -> Integer
56+
end
57+
58+
## update: test.rb
59+
class Bar
60+
def with_block(&block)
61+
block.call
62+
end
63+
64+
def proc_arg(p)
65+
p.call("test")
66+
end
67+
end
68+
69+
## assert
70+
class Bar
71+
def with_block: { () -> untyped } -> untyped
72+
def proc_arg: (Proc) -> untyped
73+
end

0 commit comments

Comments
 (0)