Skip to content

Commit e6f894a

Browse files
committed
Infer element types for array literals and NilClass for nil literals
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.
1 parent 42f5643 commit e6f894a

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

lib/solargraph/complex_type/unique_type.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def to_s
103103
# @return [self]
104104
def simplify_literals
105105
transform do |t|
106+
next t.recreate(new_name: 'NilClass') if t.nil_type?
106107
next t unless t.literal?
107108
t.recreate(new_name: t.non_literal_name)
108109
end
@@ -308,7 +309,7 @@ def to_rbs
308309
'untyped'
309310
elsif name == 'Boolean'
310311
'bool'
311-
elsif name.downcase == 'nil'
312+
elsif name.downcase == 'nil' || name == 'NilClass'
312313
'nil'
313314
elsif name == GENERIC_TAG_NAME
314315
all_params.first&.name

spec/pin/base_variable_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def bar
4444
expect(type.tags).to eq('1, nil')
4545
expect(type.simple_tags).to eq('Integer, NilClass')
4646
expect(type.to_rbs).to eq('(1 | nil)')
47-
expect(type.simplify_literals.to_rbs).to eq('(::Integer | ::NilClass)')
47+
expect(type.simplify_literals.to_rbs).to eq('(::Integer | nil)')
4848
end
4949

5050
it "understands proc kwarg parameters aren't affected by @type" do

0 commit comments

Comments
 (0)