Skip to content

[DO NOT MERGE] Infer the result of .new on Class and Class<generic<T>> values - #59

Draft
apiology wants to merge 3 commits into
masterfrom
poc-classnew-inference
Draft

apiology wants to merge 3 commits into
masterfrom
poc-classnew-inference

Conversation

@apiology

@apiology apiology commented Aug 16, 2026 •

Copy link
Copy Markdown
Owner

Claude: This PR was written by Claude Code on behalf of @apiology.

Problem:

Methods returning .new on a Class-typed value fail strong typecheck:

class Factory
  # @generic T
  # @param clazz [Class<generic<T>>]
  # @return [generic<T>]
  # Factory#create_object return type could not be inferred
  def create_object(clazz)
    clazz.new
  end

  # @param interface_methods [Array<Symbol>]
  # @return [Object]
  # Factory#duck_responder return type could not be inferred
  def duck_responder(interface_methods)
    Class.new do
      # Unresolved call to define_method
      interface_methods.each { |m| define_method(m) { nil } }
    end.new
  end
end

Each such method needs an @sg-ignore at its definition; callers are unaffected, since they use the declared @return.

Solution:

Types Class.new as Class<Object> and .new on any other bare Class value as Object, and gives Class<generic<T>> a new returning generic<T> with arguments unchecked, since its initializer is unknown.

define_method inside the block is castwide#1310's to fix.

apiology and others added 3 commits August 16, 2026 16:15
RBS declares Class#new as (*untyped) -> untyped because RBS cannot
parameterize Class. ApiMap#get_methods already replaces that pin with a
self-returning synthesis for concrete namespaces, but deliberately skips
rooted_tag Class / Class<Class>, leaving untyped - so Class.new.new and
k.new (k: bare Class) were uninferrable. Proxy the pin instead: scope
:class (literal Class.new) -> Class<Object> (the default-superclass
anonymous class), scope :instance (.new on an unparameterized
Class-typed receiver) -> Object (some instance of an unknown class).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H1FEjW6nMpZrWPmeWX9miT
For a receiver typed Class<generic<T>>, singleton-method lookup runs
against the nonexistent 'generic' namespace, so no Class#new pin ever
appears and .new goes undefined - even though the instance type is
exactly the generic the caller binds. Synthesize a permissive new pin
(restarg/kwrestarg, since the real initializer is unknown) returning the
generic tag itself. Caller-side binding already worked; this fixes the
method-body side.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H1FEjW6nMpZrWPmeWX9miT
@apiology apiology changed the title [DO NOT MERGE] Infer through Class#new for unparameterized and generic Class receivers [DO NOT MERGE] Infer the result of .new on Class and Class<generic<T>> values Oct 16, 2026
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