From 3296d92b5fe65a47d38e626a3289bba2507528ea Mon Sep 17 00:00:00 2001 From: Takumi Shotoku Date: Sun, 14 Dec 2025 15:18:39 +0900 Subject: [PATCH] Fix crash when singleton types are passed to generic methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, TypeProf would crash with "unknown type variable: Elem" when a class constant like `Array` or `Hash` was passed to methods like `all?`: [].all?(Array) # => RuntimeError: unknown type variable: Elem This happened because type parameters of Singleton types were not registered in ty_env when resolving generic method signatures. This commit fixes the issue by handling Type::Singleton in get_instance_type, registering its type parameters with Source.new. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- lib/typeprof/core/env.rb | 4 ++++ scenario/regressions/singleton-type-param.rb | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 scenario/regressions/singleton-type-param.rb diff --git a/lib/typeprof/core/env.rb b/lib/typeprof/core/env.rb index f3d47eac..13a4a8e5 100644 --- a/lib/typeprof/core/env.rb +++ b/lib/typeprof/core/env.rb @@ -116,6 +116,10 @@ def get_instance_type(mod, type_args, changes, base_ty_env, base_ty) base_ty.mod.type_params.zip(base_ty.args) do |param, arg| ty_env[param] = arg || Source.new end + elsif base_ty.is_a?(Type::Singleton) + base_ty.mod.type_params&.each do |param| + ty_env[param] = Source.new + end end args = mod.type_params.zip(type_args).map do |param, arg| arg && changes ? arg.covariant_vertex(self, changes, ty_env) : Source.new diff --git a/scenario/regressions/singleton-type-param.rb b/scenario/regressions/singleton-type-param.rb new file mode 100644 index 00000000..9e202548 --- /dev/null +++ b/scenario/regressions/singleton-type-param.rb @@ -0,0 +1,7 @@ +## update +# Passing class constants (Singleton types) to methods like `all?` +# should not crash with "unknown type variable" error +[].all?(Array) +[].all?(Hash) + +## assert