Skip to content

Commit

Permalink
Make rand(Integer) returns Integer
Browse files Browse the repository at this point in the history
Assuming we rarely pass an unknown integer value to `rand` for `Float` result.
  • Loading branch information
soutaro committed Sep 15, 2024
1 parent 68d8dde commit d856842
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/kernel.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ module Kernel : BasicObject
# See also Random.rand.
#
def self?.rand: (?0) -> Float
| (int arg0) -> (Integer | Float)
| (int arg0) -> Integer
| (::Range[Integer] arg0) -> Integer?
| (::Range[Float] arg0) -> Float?

Expand Down
2 changes: 1 addition & 1 deletion core/random.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Random < RBS::Unnamed::Random_Base
#
# See also Random#rand.
#
def self.rand: () -> Float
def self.rand: (?0) -> Float
| (Integer | ::Range[Integer] max) -> Integer
| (Float | ::Range[Float] max) -> Float
| [T < Numeric] (::Range[T]) -> T
Expand Down
16 changes: 8 additions & 8 deletions core/rbs/unnamed/random.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module RBS
# (`-`) and add (`+`)methods, or rand will raise an ArgumentError.
#
%a{annotate:rdoc:copy:Random#rand}
def rand: () -> Float
def rand: (?0) -> Float
| (Integer | ::Range[Integer] max) -> Integer
| (Float | ::Range[Float] max) -> Float

Expand Down Expand Up @@ -200,13 +200,13 @@ module RBS
# Generates formatted random number from raw random bytes. See Random#rand.
#
%a{annotate:rdoc:copy:Random::Formatter#rand}
def rand: () -> Float
| (?Float? n) -> Float
| (?Integer? n) -> Integer
| (?Numeric? n) -> Numeric
| (?::Range[Float]? n) -> Float
| (?::Range[Integer]? n) -> Integer
| (?::Range[Numeric]? n) -> Numeric
def rand: (?0) -> Float
| (Float? n) -> Float
| (Integer n) -> Integer
| (Numeric n) -> Numeric
| (::Range[Float] n) -> Float
| (::Range[Integer] n) -> Integer
| (::Range[Numeric] n) -> Numeric

%a{annotate:rdoc:copy:Random::Formatter#random_byte}
def random_bytes: (?Integer? n) -> String
Expand Down
7 changes: 7 additions & 0 deletions test/typecheck/random/Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
D = Steep::Diagnostic

target :test do
signature "."
check "."
configure_code_diagnostics(D::Ruby.all_error)
end
13 changes: 13 additions & 0 deletions test/typecheck/random/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @type var int: Integer
# @type var float: Float

float = rand(0)

Check failure on line 4 in test/typecheck/random/test.rb

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

Rubycw/Rubycw: assigned but unused variable - float

Check failure on line 4 in test/typecheck/random/test.rb

View workflow job for this annotation

GitHub Actions / test (3.3, rubocop validate test_doc build test_generate_stdlib raap)

Rubycw/Rubycw: assigned but unused variable - float
int = rand(1)

Check failure on line 5 in test/typecheck/random/test.rb

View workflow job for this annotation

GitHub Actions / test (head, stdlib_test rubocop)

Rubycw/Rubycw: assigned but unused variable - int

Check failure on line 5 in test/typecheck/random/test.rb

View workflow job for this annotation

GitHub Actions / test (3.3, rubocop validate test_doc build test_generate_stdlib raap)

Rubycw/Rubycw: assigned but unused variable - int

i = 2
int = rand(i)

int = rand(1..10) || 0
float = rand((1.0)..(2.2)) || 0.1

int = rand(1.1)

0 comments on commit d856842

Please sign in to comment.