Skip to content

Make RBS first-class pin data source; apply every @!override overload - #1104

Draft
apiology wants to merge 75 commits into
castwide:masterfrom
apiology:docstring_abstraction
Draft

apiology wants to merge 75 commits into
castwide:masterfrom
apiology:docstring_abstraction

Conversation

@apiology

@apiology apiology commented Sep 28, 2025 •

Copy link
Copy Markdown
Contributor

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

Problem: RBS supplies most of what Solargraph knows about core and stdlib, but LSP's #documentation renders the YARD docstring alone, so RBS data only counted once translated into YARD tags. Parsing RBS wrote eight of them, one per kind of pin, flattening a type it already had into tag text:

constant_pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag))
pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag))
class_pin.docstring.add_tag(YARD::Tags::Tag.new(:abstract, '(RBS interface)'))

Even then it did not all render. A method typed by RBS documented none of its parameters, and the abstract marker showed nowhere:

# Hovering String#sub, before:
#   Returns:
#   * [::String]
#
# The parameter types are on the pin. Nothing renders them.

Solution: RBS values are held on the pin as themselves — return_type:, abstract: — and LSP's #documentation reads pin and docstring together: descriptions and tags still come from comments, types now come from the pin. The conversion writes no YARD tags.

Problem: RBS types still reached Solargraph as YARD strings: each type was printed as tag text and parsed back. A literal holding type syntax did not survive the trip:

# Gem::Requirement::operator = "=" | "!=" | ">" | "<" | ">=" | "<=" | "~>"
Gem::Requirement.parse(s)  # => undefined
# OptionParser::NoArgument: [ :NONE, nil ]
OptionParser::NoArgument   # => undefined
# RDoc::Context#full_name: () -> "(unknown)"
context.full_name          # => ""(unknown)
# NilClass#to_a: () -> [ ]
nil.to_a                   # => Array() holding a type named ""

# module Operating[T]; def operator: () -> T; end
# class Comparer; include Operating["<"]; end
Comparer.new.operator      # => generic<T>

Solution: RBS types become Solargraph types directly from RBS's own type nodes, with no tag text in between. Mixin and superclass references carry those types, and method lookup through them works on the types instead of re-parsing tags. One translation changes on purpose: singleton(X) now becomes Class<X>, the class object, rather than an instance of X, so passing a subclass where RBS expects singleton(X) checks and passing an instance is reported. Every other core and stdlib translation result is unchanged.

Problem: An @!override with two @overload tags applied only the first, and on a method typed by RBS the override was discarded entirely.

# @!override Array#first
#   @overload first(n)
#     @param n [Integer]
#     @return [Array<String>]

# Array#first ends up with one signature, (n) -> Array<String>.
# RBS's zero-argument () -> Elem signature is gone.

Whether it applied at all depended on what had already read the method's signatures, because the tags were added to a shared, already-materialised pin.

Solution: An override is now a pin of its own, ranked above its target and combined through the join below, with the result swapped into the index instead of mutating a pin every consumer already holds.

Problem: Because both sources had to share the one docstring, combining or proxying a pin lost data, or produced a pin that documented one source and rendered from another:

  • #combine_with took comments from one side and the docstring from the other, so a merged pin matched neither, and proxying flipped which side showed
  • proxying dropped every docstring tag but the return type, so an attr_writer's @param vanished from hover
  • replacing a @return tag's types dropped its prose description
  • a docstring supplied at construction was discarded when there were no comments to reparse from

Solution: #combine_with becomes the join between sources, each keeping its own representation: a pin may declare authority, winning each field it supplies, with docstrings merged tag-wise rather than one chosen whole.

Centralize management of the type-related parts of the docstring and
keep it in sync
…tion

# Conflicts:
#	lib/solargraph/api_map/index.rb
#	lib/solargraph/pin/base.rb
#	lib/solargraph/pin/method.rb
#	lib/solargraph/rbs_map/conversions.rb
@apiology
apiology force-pushed the docstring_abstraction branch from 190892f to 5d48f9d Compare August 2, 2026 04:16
Comment thread lib/solargraph/pin/base.rb Resolved
Addresses review comment asking what spec requires checking
all_rooted? in Pin::Base#realize before short-circuiting on an
already-defined return type. Without the guard, a pin whose return
type comes straight from an unrooted YARD tag never gets qualified
against the ApiMap, so #realize returns an unrooted type.
@apiology
apiology marked this pull request as ready for review August 2, 2026 17:25
@apiology
apiology marked this pull request as draft August 2, 2026 17:25
@apiology
apiology marked this pull request as ready for review August 2, 2026 18:08
@apiology

apiology commented Aug 2, 2026

Copy link
Copy Markdown
Contributor Author

@castwide - ready for review

ApiMap::Index#map_overrides added each override tag to the target
pin's docstring and immediately called Pin::Method#reset_generated!
inside the same loop iteration. reset_generated! reads the memoizing
#signatures getter (via "unless signatures.empty?"), so the first
@overload tag added triggered #signatures to memoize against a
docstring that only had that one tag applied. Every later @overload
tag in the same override comment was added to the docstring but never
picked up.

Symptoms depended on whether #signatures had already been memoized
before map_overrides ran at all:

- Not yet memoized: the first @overload's parameter/return types got
  frozen onto the pin and forced onto every call site, regardless of
  which overload the arguments actually matched (castwide#1283).
- Already memoized (e.g. an RBS-sourced pin like Array#sum): the
  override had no effect at all, since reset_generated! never resets
  the @signatures ivar itself.

Fix: apply all of an override's tags to the docstring first, then
invalidate the pin's cached @signatures/@Overloads once, before any
redefine_return_type call reads #signatures again. The invalidation
clears the ivars directly instead of going through
Pin::Method#reset_generated!, which also clobbers signatures assigned
explicitly by unrelated callers (#transform_types, #combine_with).
Clearing @signatures/@Overloads for every override (from the previous
commit) also cleared it for overrides that only redefine a return
type via a bare @return tag, or via a tag like @PARAM that carries no
signature info at all. Pin::Method#signatures then rebuilt from
scratch using the pin's bare #parameters, which is empty or stale for
many RBS/combined pins -- dropping argument-type checking on
unrelated overrides (e.g. Bundler::Dsl#ruby's arg-type check went from
one problem reported to zero).

Gate the ivar-clearing on the override actually carrying an @overload
tag, matching the two repro shapes this fix targets. pin.reset_generated!
still runs unconditionally so non-signature tags (@PARAM, @deprecated,
etc.) keep re-typifying as before.
solargraph.org/guides/yard documents @!override with a plain @return
tag (the Benchmark.measure example), and solargraph-rspec relies on
the same shape for ActionController::TestCase::Behavior#request and
#response. Neither had spec coverage: the one existing @!override
spec in spec/source_map/mapper_spec.rb only checks that the directive
parses, and index_spec.rb's pre-existing override specs use @PARAM,
never reaching redefine_return_type's @return branch at all.
ApiMap::Index#reset_overridden_signatures reached into
Pin::Method#@signatures/@Overloads via instance_variable_set from
outside the class, bypassing encapsulation to force a memoized
Method pin to rebuild its signatures after an override docstring
mutation.

Add Pin::Method#invalidate_signatures!, a public method the class
owns, and call it from ApiMap::Index instead. Behavior is unchanged:
still only invoked when the override carries @overload tags, for
the same reason documented on the new method.
x ||= y already evaluates to x, so the bare @Overloads line after it
returned the same value. Removing it also resolves a new strong
typecheck complaint the prior commit introduced: Solargraph inferred
the method could return nil by reading the ivar a second time,
separately from the ||= expression that already excludes nil.
Drop the "(matches prior behavior)" changelog phrase and compress
the remaining prose, keeping every stated invariant.
@apiology
apiology marked this pull request as draft August 31, 2026 17:08
map_overrides mutates a pin's docstring and return_type via
redefine_return_type, but only refreshed the raw comments string
for the synthesized .new pin, not the #initialize pin it mirrors.
Pin::Base#== compares comments, not docstring content, so an
overridden #initialize pin could still equal its pre-override
snapshot.
Pin::Base#proxy dups a pin and sets a fresh, rooted return_type, but
never resyncs the copy's already-parsed docstring. BaseVariable's
own #return_type read a :type tag from that stale docstring before
falling back to @return_type, so a realized/proxied ivar, cvar,
gvar, or local variable pin returned an unrooted type from its
public reader even though proxy had just set a correctly-rooted one
internally.
parse_comments assigns @Docstring in both branches of an if/else,
then narrows it with @Docstring ||= ... before calling .tags/.tag/
.add_tag on it. Nil is provably excluded by the ||=, but Solargraph
loses all inferred type for the ivar at that point regardless,
flagging three calls as unresolved.

Reproduced the same shape in isolation: an ivar assigned in both
branches of an if/else, then narrowed with ||=, then called -
identical failure, unrelated to this PR's code. castwide/solargraph
PR 1259 adds ||= narrowing for lvars and ivars and fixes this exact
case.
A YARD-commented method's docstring is parsed straight from its
source comment, so its Returns: section shows up regardless of
call order. An RBS-sourced method's docstring only gets that
section if something already computed return_type first -
signature_help calls documentation with nothing having done that.
proxy() dup'd the original docstring and never resynced it after
setting the new return_type, so a realized/probed pin kept showing
the pre-probe type in hover/documentation text. Method#documentation
also never forced return_type computation itself, so a pin whose
type lives only in #signatures (RBS-sourced, no @return comment)
showed no Returns: section unless something else happened to call
#return_type first - real for LSP hover/signature_help, which call
#documentation directly.

Clear @Docstring in proxy() so it resyncs lazily via the existing
parse_comments hook. Force return_type in Method#documentation, but
only when it can't fall through to generate_signature (assumes real
Parameter pins, not the raw strings some legacy specs still pass) -
i.e. only when #signatures is already resolved or comes from inline
RBS. Extracted the tag-sync logic (Base#sync_return_type_tag) so both
call sites share it; it reads @return_type directly rather than the
#return_type method, since BaseVariable#return_type reads back from
the docstring tag this method writes and would otherwise sync a
stale value to itself.

This also makes BaseVariable#return_type's proxied? special case
redundant - the docstring it was working around is no longer stale -
so it's removed.
The proxy() and sync_return_type_tag comments added in e4f9935
ran 3 and 6 lines; this repo's convention caps comments at 1-3
lines, budgeted per method. Shortened without changing meaning.
Two coverage gaps in the docstring/return_type sync work: no
regression guard on realize's already-rooted fast path (which
skips proxy entirely, so it never needed the docstring-clearing
fix, but nothing confirmed that), and no direct return_type
coverage for the RBS-conversion methods that set return_type at
construction (attr_reader_to_pin, attr_writer_to_pin,
constant_decl_to_pin, global_decl_to_pin, ivar_to_pin, cvar_to_pin,
civar_to_pin) - existing coverage only checked docstring tag
naming for ivar, not return_type for any of the seven.

Both pass against current code; these are regression guards, not
specs for known-broken behavior.
Vince's pending review on PR 1330 flagged five multi-line comments
as "1 line at most" or "3 lines at most": two explanatory comments
in ApiMap::Index#map_overrides, the invalidate_signatures!
docstring on Pin::Method, and two setup comments in
spec/api_map/index_spec.rb explaining why a memoization-forcing
line is present. Compressed each to state only the current fact,
within budget.
undercover flagged the method as the only uncovered node on this
branch, at 0.0%.

The new example realizes a constant pin carrying an unrooted
@return tag, then asserts the resynced docstring keeps that type
on :return and adds no :type tag. Without the override the pin
inherits the :type tag name from BaseVariable, which leaves
:return holding the stale unrooted type, so the example covers
the method through realize rather than reaching in with send.
Pin::Base already declares attr_writer :docstring under protected, so
#proxy can call it directly rather than poking the instance variable of
the pin it just duplicated.

The clear stays in #proxy rather than moving into reset_generated!:
@Docstring is a constructor attribute some callers set independently of
#comments, so a reset would have nothing to reparse from. #combine_with
takes comments and docstring from different sides before calling
reset_generated!, and Common#closure= resets on every reparent.
In a YARD-documented Ruby file a leading at-sign reads as a docstring
tag, so the old wording left it unclear whether the comment meant the
return type instance variable or the reader method. Say both in words.
The guard tests whether signatures have already been computed, which
is why it cannot call the reader: the reader generates them. Naming
that as a predicate says so, and the ivar is on this class already, so
instance_variable_get bought nothing.
Undercover flagged RbsTranslator.to_type once the fallback branch
changed. Also require rbs so the spec runs on its own.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
apiology added a commit to apiology/solargraph that referenced this pull request Oct 27, 2026
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
apiology and others added 4 commits October 27, 2026 21:16
RBS mixin and superclass arguments still went through strings:
conversions joined them into Reference#generic_values, Reference#type
parsed name and values back, and ApiMap turned each dereferenced
reference into a tag that inner_get_methods parsed again. A literal
argument such as include Operating["<"] did not survive.

Reference#generic_values now holds ComplexTypes and #type builds its
type from them. Constants#dereference_type, Store#superclass_type and
ApiMap#inner_get_methods pass types through include, prepend,
extend, superclass and included-hook lookups; dereference and
qualify_superclass remain as string wrappers for callers outside.
Store's superclass cache is removed: it was written but never read.

An include that does not resolve is now skipped rather than looked
up as the root namespace. namespace_pin and resolve_generics document
the nil they already accept.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
Undercover flagged Store#qualify_superclass once it became a thin
wrapper over superclass_type; only a stub in shell_spec called it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
Master's castwide#1447 keeps relative RBS names unrooted so they resolve in
the pin's namespace; only absolute names and builtins come back
rooted. Absolute inputs keep the spec checking each RBS form's
translation to a fully rooted type.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LTpguFhKz2V87XqntgzKGe
apiology added a commit to apiology/solargraph that referenced this pull request Oct 28, 2026
The two sg-ignores on VisibilityDirective#no_empty_lines? are labelled
"probably nil false alarm", and the @buffer_lines lookup in
CommentRipper#on_comment is labelled "Use fetch to suppress false
alarm"; none needs a nil check.

The three sg-ignores on CommentRipper#on_embdoc_beg, #on_embdoc and
#on_embdoc_end cite castwide#1364, which strong typecheck
reports makes them unneeded, instead of castwide#1104.

Parameter#param_tag drops the continuation line about
narrow-pure-rbs-calls: castwide#1258 alone clears it.

Update the label counts in rules.rb.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Mixin and superclass references resolved as ComplexTypes is separate
from this PR's docstring work, so it now lives in its own PR:
#374

This reverts commit 5dd774b.
This reverts commit 7777ace.
apiology added a commit to apiology/solargraph that referenced this pull request Oct 29, 2026
Write Intersection against the Type contract from
castwide#1459 and the reduced interface from
castwide#1460 (formerly #345), with
ignore markers citing those PRs. Its specs load the class inside each
example and are pending on both plus castwide#1104, so the
NameError fails only them until the prerequisites land.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
apiology and others added 2 commits October 29, 2026 21:10
Name the mixin after what decides the winner: authority_over becomes
higher_priority, Authority becomes CombinePriority, and the boss locals
become winner, as agreed in review.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
#abstract held the note only when it came from somewhere other than a
YARD @abstract tag, so its meaning depended on how the fact arrived.
It now returns the note from the constructor or the docstring, and
Abstractable reads only #abstract.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
@apiology
apiology marked this pull request as ready for review October 30, 2026 03:02
apiology and others added 6 commits October 30, 2026 08:53
castwide#1223 closed without merging, so the two
sg-ignores in Method#parameter_bullets cited a fix that will never
land. castwide#1461 types declared tuples by position and clears both errors.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
Pin::Base#sync_return_type_tag read @return_type.items without a nil
check, so it raised whenever @return_type was unset. It now returns
early in that case and behaves as before when the type is set.

BaseVariable#combine_return_type called defined? on the result of
authoritative_value, which is declared to return any Object. It now
checks for a ComplexType first, so a non-type value falls through to
the normal combination.

The combined RBS-sourced method spec now builds its Signature pins with
a method closure, as castwide#1393 requires.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFJrPm9QhVqVqk5QPMD1c6
singleton(X) is the class object, but the translator produced an
instance of X: passing a subclass where RBS expects singleton(X)
was reported as a wrong argument type, and passing an instance was
accepted.

Carried over from #364.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LTpguHxFEQL9KJ7MGedKby
A combined instance variable pin keeps one location but the
assignments of every file it was combined from, so probing it clipped
another file's node at a position in the pin's own file.  Past that
file's end this raised InvalidOffsetError, crashing the strong
typecheck on lib/solargraph/pin/method.rb once master grew it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LTpguHxFEQL9KJ7MGedKby
Pin::Base#proxy reached the private reset_own_generated and
sync_return_type_tag on its duplicate through send. Make both
protected so another Pin::Base may call them directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LTpguHxFEQL9KJ7MGedKby
@apiology
apiology marked this pull request as ready for review November 18, 2026 20:05
apiology added a commit to apiology/solargraph that referenced this pull request Nov 18, 2026
Index#map_rbs_annotation_overrides adds each Override's
rbs_annotations to the methods it names before map_overrides runs,
instead of inside map_overrides' per-pin tag loop. The result is the
same, and map_overrides is left as on master, so castwide/solargraph
castwide#1104, which rewrites that loop, merges without conflict.
apiology added a commit to apiology/solargraph that referenced this pull request Nov 18, 2026
castwide#1104 adds a constant after Pin::Method's include
and a conversions spec after 'with a prepended module', the spots
where this branch added RBS_ANNOTATION and its pure-only specs. Define
RBS_ANNOTATION above the include and move the specs before 'with
implicitly-returns-nil on some overloads' so the two PRs merge in
either order.
apiology added a commit to apiology/solargraph that referenced this pull request Nov 18, 2026
Index#map_rbs_annotation_overrides adds each Override's
rbs_annotations to the methods it names before map_overrides runs,
instead of inside map_overrides' per-pin tag loop. The result is the
same, and map_overrides is left as on master, so castwide/solargraph
castwide#1104, which rewrites that loop, merges without conflict.
@apiology
apiology marked this pull request as draft November 25, 2026 20:58
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