Skip to content

Commit ca1659e

Browse files
committed
added typecheck for complex
1 parent 8a1ed2b commit ca1659e

5 files changed

Lines changed: 67 additions & 8 deletions

File tree

core/kernel.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ module Kernel : BasicObject
501501
| (String real_or_both, ?exception: true) -> Complex
502502
| (untyped real_or_both, exception: bool) -> Complex?
503503
| (Numeric | String real, Numeric | String imag, ?exception: true) -> Complex
504-
| (Numeric | String real, Integer | Float | Rational | Complex imag, exception: bool) -> Complex
504+
| (Numeric | String real, Integer | Float | Rational | Complex imag, exception: bool) -> Complex?
505505
| (Numeric | String real, untyped imag, exception: bool) -> Complex?
506506

507507
# <!--

test/stdlib/Kernel_test.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,25 @@ def test_Complex
6262
Kernel, :Complex, real_untype, exception: false
6363
end
6464

65-
with '1', 1, 1r, 1.0, (1+0i), numeric do |real|
66-
with '2', 2, 2r, 2.0, (2+0i), numeric do |imag|
65+
numeric_fail = Class.new(Numeric) { def to_c = fail }.new
66+
67+
with '1', 1, 1r, 1.0, (1+0i), numeric, numeric_fail do |real|
68+
with '2', 2, 2r, 2.0, (2+0i), numeric, numeric_fail do |imag|
6769
# (Numeric | String real, Numeric | String imag, ?exception: true) -> Complex
68-
assert_send_type "(Numeric | String, Numeric | String) -> Complex",
69-
Kernel, :Complex, real, imag
70-
assert_send_type "(Numeric | String, Numeric | String, exception: true) -> Complex",
71-
Kernel, :Complex, real, imag, exception: true
70+
if real != numeric_fail && imag != numeric_fail
71+
assert_send_type "(Numeric | String, Numeric | String) -> Complex",
72+
Kernel, :Complex, real, imag
73+
assert_send_type "(Numeric | String, Numeric | String, exception: true) -> Complex",
74+
Kernel, :Complex, real, imag, exception: true
75+
end
7276

7377
# Complex has an awkward edgecase where `exception: false` will unconditionally return `nil`
7478
# if the imaginary argument is not one of the builtin `Numeric`s. Oddly enough, it's not for
7579
# the `real` one...
7680
case imag
7781
when Integer, Float, Rational, Complex
7882
# (Numeric | String real, Integer | Float | Rational | Complex imag, exception: bool) -> Complex
79-
assert_send_type "(Numeric | String, Integer | Float | Rational | Complex, exception: bool) -> Complex",
83+
assert_send_type "(Numeric | String, Integer | Float | Rational | Complex, exception: bool) -> Complex?",
8084
Kernel, :Complex, real, imag, exception: false
8185
end
8286
end

test/typecheck/complex/Steepfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
D = Steep::Diagnostic
2+
3+
target :test do
4+
signature "."
5+
check "."
6+
configure_code_diagnostics(D::Ruby.all_error)
7+
end

test/typecheck/complex/test.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Raises
2+
def to_c = fail
3+
end
4+
5+
class RaisesNumeric < Numeric
6+
def to_c = fail
7+
end
8+
9+
Complex(1) #: Complex
10+
Complex(1.23) #: Complex
11+
Complex(1r) #: Complex
12+
Complex(1i) #: Complex
13+
Complex(1ri) #: Complex
14+
Complex(Numeric.new) #: Complex
15+
Complex('1+2i') #: Complex
16+
17+
Complex(1, exception: true) #: Complex
18+
Complex(1.23, exception: true) #: Complex
19+
Complex(1r, exception: true) #: Complex
20+
Complex(1i, exception: true) #: Complex
21+
Complex(1ri, exception: true) #: Complex
22+
Complex(Numeric.new, exception: true) #: Complex
23+
Complex("1+2i", exception: true) #: Complex
24+
25+
Complex(Raises.new, exception: false) #: nil
26+
Complex(RaisesNumeric.new, exception: false) #: nil
27+
Complex('a', exception: false) #: nil
28+
29+
Complex(1, 1) #: Complex
30+
Complex(1.23, 1.23) #: Complex
31+
Complex(1r, 1r) #: Complex
32+
Complex(1i, 1i) #: Complex
33+
Complex(1ri, 1ri) #: Complex
34+
Complex(Numeric.new, Numeric.new) #: Complex
35+
Complex('1+2i', '1+2i') #: Complex
36+
Complex(2, '1+2i') #: Complex
37+
38+
Complex(1, Raises.new, exception: false) #: nil
39+
Complex(RaisesNumeric.new, 2, exception: false) #: nil
40+
Complex('a', 3, exception: false) #: nil
41+
Complex(2, :untyped, exception: false) #: nil

test/typecheck/complex/test.rbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Raises
2+
def to_c: () -> bot
3+
end
4+
5+
class RaisesNumeric < Numeric
6+
def to_c: () -> bot
7+
end

0 commit comments

Comments
 (0)