Skip to content

Commit f47f2eb

Browse files
sinsokuclaude
authored andcommitted
Fix unknown type variable error in generic RBS class definitions
Resolves the "unknown type variable: X" error that occurred when processing instance variables using type parameters within generic RBS class definitions like `class Foo[X]` with `@x: X`. The fix creates proper type parameter substitution maps for SigInstanceVariableNode contexts, allowing type variables to be correctly resolved within generic classes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e2675ac commit f47f2eb

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

lib/typeprof/core/graph/box.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,20 @@ def initialize(node, genv, rbs_type)
8787
attr_reader :node, :rbs_type, :ret
8888

8989
def run0(genv, changes)
90-
vtx = @rbs_type.covariant_vertex(genv, changes, {})
90+
# Create substitution map for type parameters if we're in a SigInstanceVariableNode within a generic class
91+
subst = {}
92+
if @node.is_a?(AST::SigInstanceVariableNode) && @node.cpath
93+
mod = genv.resolve_cpath(@node.cpath)
94+
if mod.type_params && !mod.type_params.empty?
95+
# Create a substitution map where each type parameter maps to a type variable vertex
96+
subst = mod.type_params.to_h do |param|
97+
type_var_vtx = Vertex.new(@node)
98+
[param, type_var_vtx]
99+
end
100+
end
101+
end
102+
103+
vtx = @rbs_type.covariant_vertex(genv, changes, subst)
91104
changes.add_edge(genv, vtx, @ret)
92105
end
93106
end

scenario/rbs/param8.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## update: test.rbs
2+
class Foo[X]
3+
@x: X
4+
end

0 commit comments

Comments
 (0)