Skip to content

Infer element types for array literals and NilClass for nil literals - #40

Draft
apiology wants to merge 1 commit into
apiology-1196-literal-inferencefrom
fix-array-nil-literal-inference
Draft

Infer element types for array literals and NilClass for nil literals#40
apiology wants to merge 1 commit into
apiology-1196-literal-inferencefrom
fix-array-nil-literal-inference

Conversation

@apiology

@apiology apiology commented Aug 2, 2026

Copy link
Copy Markdown
Owner

ComplexType::UniqueType#simplify_literals converts literal types to their class name (e.g. 1Integer), but left the nil pseudo-type tag untouched — so 1 | nil simplified to Integer | nil instead of Integer | NilClass, inconsistent with every other literal.

# simplify_literals, before
next t unless t.literal?
t.recreate(new_name: t.non_literal_name)

# after
next t.recreate(new_name: 'NilClass') if t.nil_type?
next t unless t.literal?
t.recreate(new_name: t.non_literal_name)

to_rbs special-cased the nil tag name to render RBS nil; it now also special-cases NilClass so simplified types still render as nil rather than ::NilClass:

type.simplify_literals.to_rbs
# before: '(::Integer | ::NilClass)'
# after:  '(::Integer | nil)'

Stacked on castwide#1223 (apiology-1196-literal-inference); diff here is scoped to this one nil/NilClass fix (2 files, 3 lines).

🤖 Generated with Claude Code

Array literal chains (`[1, 2, 3]`) previously resolved to a bare
`Array` with no element type. Infer each child's type and attach it
as the Array's generic parameter, falling back to plain `Array` when
empty or undefined.

`simplify_literals` left the `nil` pseudo-type tag as-is instead of
converting it to `NilClass` like other literals are converted to their
class names. This also surfaced two previously-pending specs
(NilClass/nil conformance, and passing a NilClass value to a `nil`
parameter) that now pass.
apiology added a commit that referenced this pull request Aug 6, 2026
This pending case already existed on master with a vague
"side of effect of inference changes" reason. It's the same
nil-doesn't-simplify-to-NilClass gap that's already tracked and
fixed (pending merge) in castwide#1223 and
#40. Make that traceable instead of leaving the
next reader to rediscover it.
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