From 86851655acffd9bf15c41d09feea2bf266b7cccb Mon Sep 17 00:00:00 2001 From: Takumi Shotoku Date: Sat, 5 Sep 2026 17:47:30 +0900 Subject: [PATCH] Give the RBS instance type its type arguments Type.default_param_map built the `*instance` entry with an empty argument list, so a singleton method declared to return `instance` lost its type arguments, and crashed when that result reached a type variable check (SigTyVarNode#typecheck got nil instead of an actual argument). After base_type the receiver is always an Instance or a Singleton, so get_instance_type is always defined. table = Hash[[["a", "b"]]] # Hash.[] is declared as `-> instance` "foo".gsub(/o/, table) # gsub takes `hash[String, _ToS]` --- lib/typeprof/core/type.rb | 2 +- scenario/rbs/instance-type.rb | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 scenario/rbs/instance-type.rb diff --git a/lib/typeprof/core/type.rb b/lib/typeprof/core/type.rb index 5450608aa..7ddf86346 100644 --- a/lib/typeprof/core/type.rb +++ b/lib/typeprof/core/type.rb @@ -48,7 +48,7 @@ def self.collect_hash_value_types(type) def self.default_param_map(genv, ty) ty = ty.base_type(genv) - instance_ty = ty.is_a?(Type::Instance) ? ty : Type::Instance.new(genv, ty.mod, []) # TODO: type params + instance_ty = ty.is_a?(Type::Instance) ? ty : ty.get_instance_type(genv) singleton_ty = ty.is_a?(Type::Instance) ? Type::Singleton.new(genv, ty.mod) : ty { "*self": Source.new(ty), diff --git a/scenario/rbs/instance-type.rb b/scenario/rbs/instance-type.rb new file mode 100644 index 000000000..aa8cc504c --- /dev/null +++ b/scenario/rbs/instance-type.rb @@ -0,0 +1,25 @@ +## update: test.rbs +class Gen[T] + def self.create: () -> instance +end + +type gen[T] = Gen[T] + +class Object + def accept: (gen[Integer]) -> String +end + +## update: test.rb +def test + accept(Gen.create) +end + +def raw + Gen.create +end + +## assert +class Object + def test: -> String + def raw: -> Gen[untyped] +end