Skip to content

Infer value types from RBS record types - #360

Draft
apiology wants to merge 12 commits into
masterfrom
type-rbs-records-as-symbol-hashes
Draft

apiology wants to merge 12 commits into
masterfrom
type-rbs-records-as-symbol-hashes

Conversation

@apiology

@apiology apiology commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

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

Problem

An RBS record return type loses its field types:

class Server
  #: () -> { name: String, port: Integer }
  def config
    { name: 'web', port: 80 }
  end

  # @param settings [Hash{String => String}]
  # @return [void]
  def apply(settings); end

  # @return [void]
  def run
    # (no problem reported: the record is a bare Hash)
    apply(config)
  end
end

Records appear in core, stdlib and gem RBS as well as inline #: signatures.

Solution

Translate a record to Hash{<key classes> => <union of field types>}, so reads infer and mismatched Hash parameters are reported, and add a Record type class that keeps each field typed on its own.

Record answers record[:port] through one literal-keyed [] overload per field (Integer? when optional), conforms as the Hash it describes, and prints back to RBS as the same record. A non-literal key takes the first field's type, following the first-match fallback of castwide/solargraph#1385.

The translator line that builds a Record stays commented out until castwide/solargraph#1104, castwide/solargraph#1459 and castwide/solargraph#1460 are in; per-key reads also need castwide/solargraph#1451 and castwide#1385. Examples needing those are pending on them.

Also types Initialize#process's capabilities result with a #: record assertion, and sets RuboCop's Layout/LeadingCommentSpace to allow rbs-inline #: comments.

apiology and others added 12 commits October 27, 2026 11:03
RbsTranslator turned every RBS record type into a bare Hash, so a
method returning `{ name: String, port: Integer }` lost its value
types: `config[:name]` inferred as undefined, and the record conformed
to any Hash parameter, including `Hash{String => String}`.

Translate a record to a Hash whose key type is the union of its key
classes (Symbol for `name:`, String for `'id' =>`) and whose value type
is the union of its field types. Optional fields join the union
without nil, matching how Hash{K => V} treats any absent key. Per-key
precision needs intersection types, which this does not attempt.
Collect record-type specs in spec/rbs_record_types_spec.rb, moving the
five record specs from strong_spec.rb and conversions_spec.rb there.

Records translate to Hash{<key classes> => <union of field types>}.
New examples cover typechecking, field reads, RBS and YARD output,
inline RBS parameters and #: assertions, optional fields, string keys,
and Hash#fetch/size cases ported from castwide#1231. Those
needing open PRs are pending on them; each single-PR citation was
checked by applying that PR and this branch to master in a throwaway
worktree and watching the example pass.

Per-key reads and record round trips to RBS stay pending on
#284 and castwide#1451, but no
combination of open PRs makes them pass: per-key Hash dispatch on an
intersection is not implemented anywhere open, and nothing renders an
intersection back as a record.
YARD has no accepted intersection syntax, and the agreed scope on
castwide#1231 is RBS input, output and typechecking. The
ported per-key specs now state each expected type as an inline RBS
return type instead of a YARD @type tag, and the string-key rejection
expects an RBS record parameter instead of a YARD literal-key Hash.
Mark the LSP initialize result hash with an inline #: record assertion
naming the ServerCapabilities fields the method sets: textDocumentSync,
workspace.workspaceFolders and the optional positionEncoding. This is
the RBS form of the YARD intersection castwide#1231 used.

Solargraph ignores trailing #: assertions until
castwide#1444, so typechecking is unchanged on master.
Enable RuboCop AllowRBSInlineAnnotation so #: needs no space.
RBS before 4.1 types Hash#fetch as (K) -> V, which already resolves
the record value, so the spec passed there and RSpec failed it as
FIXED. RBS 4.1 types the key as the _Key interface, which needs the
structural conformance from castwide#1266.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
Capitalized symbol literals lose their type in any RBS type, not just
records; #359 covers that on its own.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
Hash#fetch fails on every Hash under RBS 4.1, not just records, and
castwide#1266 fixes that on its own; the [] reads cover
reading record fields.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
period_checker took three positional strings copied from
castwide#1231; problems_reading_record takes record_type:,
returning: and readers: keywords.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
Translating a record to Hash{<key classes> => <union of field types>}
loses which field holds which type, so config[:port] reads as
String | Integer and the record cannot print back to RBS.

Add ComplexType::UniqueType::Record, written against the Type
contract of castwide#1459 and the type interface of
castwide#1460. It keeps each field's type and optionality,
renders its tags as the Hash it describes, answers [] with one
literal-keyed overload per field (T? for an optional field), conforms
field by field to another record and as its Hash otherwise, and
prints itself back via to_rbs.

The translator line that builds a Record is commented out until
castwide#1104 translates RBS to type objects, so behavior
on master is unchanged. Per-key reads also need literal argument
matching from castwide#1385 and literal types from
castwide#1451; a non-literal key takes the first field's
type under castwide#1385's first-match fallback.

Ignore markers cite castwide#1459 and castwide#1460 where Record calls into them, and
a new rules.rb label records that an &:method block-pass types a
[self] return as the outer receiver.

Also commits the record spec rewrite, with pending citations updated
to the PRs each example needs, and unit specs for Record pending on
the same PRs.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
castwide#1459 and castwide#1460 were closed and their changes
now live in castwide#1244, so point the pending specs,
sg-ignore labels and the commented-out record translation there.
The inline RBS record spec that master brought in expects a bare
Hash; with records translated to Hash{K => V}, the assertion now
infers Hash{Symbol => String}.
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