Skip to content

Give the RBS instance type its type arguments - #480

Open
sinsoku wants to merge 1 commit into
ruby:masterfrom
sinsoku:fix-rbs-instance-type-args
Open

Give the RBS instance type its type arguments#480
sinsoku wants to merge 1 commit into
ruby:masterfrom
sinsoku:fix-rbs-instance-type-args

Conversation

@sinsoku

@sinsoku sinsoku commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

Motivation / Background

This Pull Request has been created because passing the result of Hash[...] to String#gsub crashes TypeProf:

table = Hash[[["a", "b"]]]
"foo".gsub(/o/, table)
#=> SigTyVarNode#typecheck: undefined method 'new_vertex' for nil (NoMethodError)

Hash.[] returns instance, but Type.default_param_map built the *instance entry with an empty argument list, so matching the result against gsub's generic hash[String, _ToS] left a type variable with no actual argument. ruby/ruby aborts on lib/cgi/escape.rb for this reason.

Detail

The fix delegates to Type::Singleton#get_instance_type, which already fills the arguments in; after base_type the receiver is always an Instance or a Singleton, so it is always defined.

instance now prints with its type arguments, so a singleton method returning it reports Gen[untyped] where it used to report Gen, which is what an explicit Gen[T] return type has always printed. The added scenario covers that output as well as the crash above.

🤖 Generated with Claude Code

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]`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant