Skip to content

Commit

Permalink
activesupport: Update the type of Array.wrap
Browse files Browse the repository at this point in the history
At present, `Array.wrap` will be return an empty tuple for untyped value
because it matches to the first method_type: `(nil) -> []`.

In the type world, the behavior of an empty tuple is different from an
empty array.  For example, calling a enumerable method will cause type
an error:

```
app/app.rb:18:28: [error] Type `bot` does not have method `to_s`
│ Diagnostic ID: Ruby::NoMethod
│
└ Array.wrap(foo).map { |e| e.to_s }
                              ~~~~

```

This changes the return type of `Array.wrap` for nil value to
`Array[untyped]`.

refs: #683
  • Loading branch information
tk0miya committed Oct 8, 2024
1 parent ddba0c6 commit 3f48ea5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions gems/activesupport/6.0/_test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TestAttrInternal
ActiveSupport::TimeZone['Asia/Tokyo'].to_s
Time.find_zone(Object.name)

Array.wrap(nil)
Array.wrap([1, 2, 3])
Array.wrap("hello")
Array.wrap({a: 1, b: 2})
Array.wrap(nil).map(&:to_s)
Array.wrap([1, 2, 3]).map(&:to_s)
Array.wrap("hello").map(&:to_s)
Array.wrap({a: 1, b: 2}).map(&:to_s)
2 changes: 1 addition & 1 deletion gems/activesupport/6.0/activesupport.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ class Array[unchecked out Elem]
#
# The differences with <tt>Kernel#Array</tt> explained above
# apply to the rest of <tt>object</tt>s.
def self.wrap: (nil) -> []
def self.wrap: (nil) -> Array[untyped]
| [T] (_ToAry[T] array_like) -> Array[T]
| [T] (T ele) -> [T]
end
Expand Down

0 comments on commit 3f48ea5

Please sign in to comment.