typeprof 0.2.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +1 -2
- data/.gitignore +1 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +10 -21
- data/LICENSE +21 -0
- data/README.md +6 -0
- data/doc/demo.md +398 -0
- data/doc/doc.ja.md +6 -1
- data/doc/doc.md +7 -2
- data/exe/typeprof +2 -1
- data/lib/typeprof.rb +9 -0
- data/lib/typeprof/analyzer.rb +427 -325
- data/lib/typeprof/arguments.rb +405 -0
- data/lib/typeprof/block.rb +136 -0
- data/lib/typeprof/builtin.rb +36 -25
- data/lib/typeprof/cli.rb +51 -98
- data/lib/typeprof/config.rb +114 -0
- data/lib/typeprof/container-type.rb +280 -92
- data/lib/typeprof/export.rb +197 -108
- data/lib/typeprof/import.rb +134 -80
- data/lib/typeprof/iseq.rb +42 -1
- data/lib/typeprof/method.rb +178 -82
- data/lib/typeprof/type.rb +228 -369
- data/lib/typeprof/utils.rb +4 -18
- data/lib/typeprof/version.rb +3 -0
- data/smoke/arguments2.rb +55 -0
- data/smoke/array-each3.rb +1 -4
- data/smoke/array12.rb +1 -1
- data/smoke/array6.rb +1 -0
- data/smoke/block-ambiguous.rb +36 -0
- data/smoke/block-args1-rest.rb +62 -0
- data/smoke/block-args1.rb +59 -0
- data/smoke/block-args2-rest.rb +62 -0
- data/smoke/block-args2.rb +59 -0
- data/smoke/block-args3-rest.rb +73 -0
- data/smoke/block-args3.rb +70 -0
- data/smoke/block-blockarg.rb +27 -0
- data/smoke/block-kwarg.rb +52 -0
- data/smoke/block10.rb +1 -1
- data/smoke/block11.rb +1 -1
- data/smoke/block14.rb +17 -0
- data/smoke/block4.rb +2 -2
- data/smoke/block5.rb +1 -0
- data/smoke/block6.rb +1 -1
- data/smoke/block7.rb +0 -2
- data/smoke/block8.rb +2 -2
- data/smoke/block9.rb +1 -1
- data/smoke/blown.rb +1 -1
- data/smoke/class-hierarchy.rb +54 -0
- data/smoke/class-hierarchy2.rb +27 -0
- data/smoke/constant1.rb +11 -6
- data/smoke/constant2.rb +2 -0
- data/smoke/cvar.rb +1 -0
- data/smoke/demo10.rb +1 -1
- data/smoke/demo8.rb +2 -2
- data/smoke/demo9.rb +1 -3
- data/smoke/flow7.rb +1 -7
- data/smoke/flow8.rb +13 -0
- data/smoke/hash-fetch.rb +3 -3
- data/smoke/hash-merge-bang.rb +11 -0
- data/smoke/hash1.rb +1 -1
- data/smoke/hash3.rb +1 -1
- data/smoke/hash4.rb +1 -1
- data/smoke/instance_eval.rb +1 -1
- data/smoke/int_times.rb +1 -1
- data/smoke/ivar2.rb +1 -1
- data/smoke/keyword3.rb +1 -2
- data/smoke/keyword4.rb +1 -1
- data/smoke/kwsplat1.rb +1 -1
- data/smoke/kwsplat2.rb +1 -1
- data/smoke/module4.rb +2 -0
- data/smoke/multiple-superclass.rb +4 -0
- data/smoke/next2.rb +1 -1
- data/smoke/optional1.rb +1 -1
- data/smoke/optional2.rb +1 -1
- data/smoke/optional3.rb +10 -0
- data/smoke/pattern-match1.rb +23 -0
- data/smoke/pattern-match2.rb +15 -0
- data/smoke/proc4.rb +1 -1
- data/smoke/rbs-extend.rb +9 -0
- data/smoke/rbs-extend.rbs +7 -0
- data/smoke/rbs-interface.rb +24 -0
- data/smoke/rbs-interface.rbs +12 -0
- data/smoke/rbs-proc1.rb +9 -0
- data/smoke/rbs-proc1.rbs +3 -0
- data/smoke/rbs-proc2.rb +20 -0
- data/smoke/rbs-proc2.rbs +3 -0
- data/smoke/rbs-proc3.rb +13 -0
- data/smoke/rbs-proc3.rbs +4 -0
- data/smoke/rbs-record.rb +17 -0
- data/smoke/rbs-record.rbs +4 -0
- data/smoke/rbs-tyvar.rb +18 -0
- data/smoke/rbs-tyvar.rbs +5 -0
- data/smoke/rbs-tyvar2.rb +20 -0
- data/smoke/rbs-tyvar2.rbs +9 -0
- data/smoke/rbs-tyvar3.rb +17 -0
- data/smoke/rbs-tyvar3.rbs +5 -0
- data/smoke/rbs-tyvar4.rb +36 -0
- data/smoke/rbs-tyvar5.rb +12 -0
- data/smoke/rbs-tyvar5.rbs +8 -0
- data/smoke/rest1.rb +1 -1
- data/smoke/rest2.rb +1 -1
- data/smoke/rest3.rb +1 -1
- data/smoke/rest5.rb +1 -1
- data/smoke/rest6.rb +1 -1
- data/smoke/retry1.rb +1 -1
- data/smoke/return.rb +1 -1
- data/smoke/singleton_method.rb +3 -0
- data/smoke/step.rb +1 -1
- data/smoke/struct.rb +4 -3
- data/smoke/struct3.rb +14 -0
- data/smoke/symbol-proc.rb +24 -0
- data/smoke/uninitialize-var.rb +12 -0
- data/smoke/user-demo.rb +15 -0
- data/smoke/wrong-extend.rb +1 -0
- data/typeprof.gemspec +4 -2
- metadata +53 -6
- data/run.sh +0 -3
- data/smoke/variadic1.rb.notyet +0 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c1cf845328dbfd7b8dc14113c986be27a28457153d4e3b470ca45341035bdff7
|
|
4
|
+
data.tar.gz: 7e1aa96c7623832dc63e0d60573c28deeb0112c31921d63752553dcbf8127fde
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 40e5f8da50d3b31dc96f2b010275083d9907c7510d16afc1f86b23114196a30c8170b9fc77cb8d8e1de0a7ba0b14a5deaf143450f3541e007a56a9170ce7ed93
|
|
7
|
+
data.tar.gz: 3fd01d2edc108f2ce262af023886fa04c8e2cbc6e98647dad238954428a188aa4637913c2e7f13c983deeb4ac6ff7b3dc3489acc740effcbddbd128519135662
|
data/.github/workflows/main.yml
CHANGED
|
@@ -17,9 +17,8 @@ jobs:
|
|
|
17
17
|
uses: ruby/setup-ruby@v1
|
|
18
18
|
with:
|
|
19
19
|
ruby-version: ${{ matrix.ruby-version }}
|
|
20
|
-
- name:
|
|
20
|
+
- name: Bundle install
|
|
21
21
|
run: |
|
|
22
|
-
cd rbs && bundle install && bundle exec rake parser && cd ..
|
|
23
22
|
bundle install
|
|
24
23
|
- name: Run the test suite
|
|
25
24
|
run: |
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
|
@@ -7,7 +7,7 @@ group :development do
|
|
|
7
7
|
gem "rake"
|
|
8
8
|
gem "stackprof"
|
|
9
9
|
gem "test-unit"
|
|
10
|
-
gem "simplecov"
|
|
11
|
-
gem "simplecov-html"
|
|
10
|
+
gem "simplecov"
|
|
11
|
+
gem "simplecov-html"
|
|
12
12
|
gem "coverage-helpers"
|
|
13
13
|
end
|
data/Gemfile.lock
CHANGED
|
@@ -1,23 +1,8 @@
|
|
|
1
|
-
GIT
|
|
2
|
-
remote: https://github.com/simplecov-ruby/simplecov-html.git
|
|
3
|
-
revision: 6567856c6940e285bbb70cce98edca60c7dfefdd
|
|
4
|
-
ref: 6567856c6940e285bbb70cce98edca60c7dfefdd
|
|
5
|
-
specs:
|
|
6
|
-
simplecov-html (0.12.2)
|
|
7
|
-
|
|
8
|
-
GIT
|
|
9
|
-
remote: https://github.com/simplecov-ruby/simplecov.git
|
|
10
|
-
revision: 80700ec9f9b5ae426c22d06f62620f7e7b71ff42
|
|
11
|
-
specs:
|
|
12
|
-
simplecov (0.18.5)
|
|
13
|
-
docile (~> 1.1)
|
|
14
|
-
simplecov-html (~> 0.11)
|
|
15
|
-
|
|
16
1
|
PATH
|
|
17
2
|
remote: .
|
|
18
3
|
specs:
|
|
19
|
-
typeprof (0.
|
|
20
|
-
rbs (>= 0.
|
|
4
|
+
typeprof (0.4.2)
|
|
5
|
+
rbs (>= 0.16.0)
|
|
21
6
|
|
|
22
7
|
GEM
|
|
23
8
|
remote: https://rubygems.org/
|
|
@@ -26,8 +11,12 @@ GEM
|
|
|
26
11
|
docile (1.3.2)
|
|
27
12
|
power_assert (1.2.0)
|
|
28
13
|
rake (13.0.1)
|
|
29
|
-
rbs (0.
|
|
30
|
-
|
|
14
|
+
rbs (0.16.0)
|
|
15
|
+
simplecov (0.19.1)
|
|
16
|
+
docile (~> 1.1)
|
|
17
|
+
simplecov-html (~> 0.11)
|
|
18
|
+
simplecov-html (0.12.3)
|
|
19
|
+
stackprof (0.2.16)
|
|
31
20
|
test-unit (3.3.6)
|
|
32
21
|
power_assert
|
|
33
22
|
|
|
@@ -37,8 +26,8 @@ PLATFORMS
|
|
|
37
26
|
DEPENDENCIES
|
|
38
27
|
coverage-helpers
|
|
39
28
|
rake
|
|
40
|
-
simplecov
|
|
41
|
-
simplecov-html
|
|
29
|
+
simplecov
|
|
30
|
+
simplecov-html
|
|
42
31
|
stackprof
|
|
43
32
|
test-unit
|
|
44
33
|
typeprof!
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Yusuke Endoh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
data/doc/demo.md
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
# TypeProf demo cases
|
|
2
|
+
|
|
3
|
+
## A simple demo with a "User" class
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
def hello_message(user)
|
|
7
|
+
"The name is " + user.name
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def type_error_demo(user)
|
|
11
|
+
"The age is " + user.age
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
user = User.new(name: "John", age: 20)
|
|
15
|
+
|
|
16
|
+
hello_message(user)
|
|
17
|
+
type_error_demo(user)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
class User
|
|
22
|
+
attr_reader name: String
|
|
23
|
+
attr_reader age: Integer
|
|
24
|
+
|
|
25
|
+
def initialize: (name: String, age: Integer) -> void
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Result:
|
|
30
|
+
```
|
|
31
|
+
$ typeprof test.rb test.rbs
|
|
32
|
+
# Errors
|
|
33
|
+
test.rb:6: [error] failed to resolve overload: String#+
|
|
34
|
+
|
|
35
|
+
# Classes
|
|
36
|
+
class Object
|
|
37
|
+
def hello_message : (User) -> String
|
|
38
|
+
def type_error_demo : (User) -> untyped
|
|
39
|
+
end
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Yoy can [try this analysis online](https://mame.github.io/typeprof-playground/#rb=def+hello_message%28user%29%0A++%22The+name+is+%22+%2B+user.name%0Aend%0A%0Adef+type_error_demo%28user%29%0A++%22The+age+is+%22+%2B+user.age%0Aend%0A%0Auser+%3D+User.new%28name%3A+%22John%22%2C+age%3A+20%29%0A%0Ahello_message%28user%29%0Atype_error_demo%28user%29&rbs=class+User%0A++attr_reader+name%3A+String%0A++attr_reader+age%3A+Integer%0A%0A++def+initialize%3A+%28name%3A+String%2C+age%3A+Integer%29+-%3E+void%0Aend).
|
|
43
|
+
|
|
44
|
+
## A simple demo to generate the signature prototype of "User" class
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
class User
|
|
48
|
+
def initialize(name:, age:)
|
|
49
|
+
@name, @age = name, age
|
|
50
|
+
end
|
|
51
|
+
attr_reader :name, :age
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# A test case to tell TypeProf what types are expected by the class and methods
|
|
55
|
+
User.new(name: "John", age: 20)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Result:
|
|
59
|
+
```
|
|
60
|
+
$ typeprof -v test.rb
|
|
61
|
+
# Classes
|
|
62
|
+
class User
|
|
63
|
+
attr_reader name : String
|
|
64
|
+
attr_reader age : Integer
|
|
65
|
+
def initialize : (name: String, age: Integer) -> [String, Integer]
|
|
66
|
+
end
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Type inspection by `p` (`Kernel#p`)
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
p 42 #=> Integer
|
|
73
|
+
p "str" #=> String
|
|
74
|
+
p "str".chars #=> Array[String]
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Result:
|
|
78
|
+
```
|
|
79
|
+
$ typeprof test.rb
|
|
80
|
+
# Revealed types
|
|
81
|
+
# test.rb:1 #=> Integer
|
|
82
|
+
# test.rb:2 #=> String
|
|
83
|
+
# test.rb:3 #=> Array[String]
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Block with builtin methods
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
# TypeProf runs this block only once
|
|
90
|
+
10000000000000.times do |n|
|
|
91
|
+
p n #=> Integer
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# "each" with Heterogeneous array yields a union type
|
|
95
|
+
[1, 1.0, "str"].each do |e|
|
|
96
|
+
p e #=> Float | Integer | String
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# You can use the idiom `&:method_name` too
|
|
100
|
+
p [1, 1.0, "str"].map(&:to_s) #=> Array[String]
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## User-defined blocks
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
def foo(n)
|
|
107
|
+
yield n.to_s
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
foo(42) do |n|
|
|
111
|
+
p n #=> String
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Result:
|
|
117
|
+
```
|
|
118
|
+
$ typeprof test.rb
|
|
119
|
+
# Revealed types
|
|
120
|
+
# test.rb:6 #=> String
|
|
121
|
+
|
|
122
|
+
# Classes
|
|
123
|
+
class Object
|
|
124
|
+
def foo : (Integer) { (String) -> nil } -> nil
|
|
125
|
+
end
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Arrays
|
|
129
|
+
|
|
130
|
+
```ruby
|
|
131
|
+
# A fixed-length array literal generates a "tuple" array
|
|
132
|
+
ary = [1, 1.0]
|
|
133
|
+
|
|
134
|
+
# A tuple array keeps its length, and the association between indexes and elements
|
|
135
|
+
p ary #=> [Integer, Float]
|
|
136
|
+
p ary[0] #=> Integer
|
|
137
|
+
p ary[1] #=> Float
|
|
138
|
+
|
|
139
|
+
# Destructive operation is well handled (in method-local analysis)
|
|
140
|
+
ary[0] = "str"
|
|
141
|
+
p ary #=> [String, Float]
|
|
142
|
+
|
|
143
|
+
# An calculated array generates a "sequence" array
|
|
144
|
+
ary = [1] + [1.0]
|
|
145
|
+
|
|
146
|
+
# A sequence array does not keep length nor association
|
|
147
|
+
p ary #=> Array[Float | Integer]
|
|
148
|
+
p ary[0] #=> Float | Integer
|
|
149
|
+
|
|
150
|
+
# Destructive operation is still handled (but "weak update" is applied)
|
|
151
|
+
ary[0] = "str"
|
|
152
|
+
p ary #=> Array[Float | Integer | String]
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Multiple return values by using a tuple array
|
|
156
|
+
|
|
157
|
+
```ruby
|
|
158
|
+
def foo
|
|
159
|
+
return 42, "str"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
int, str = foo
|
|
163
|
+
p int #=> Integer
|
|
164
|
+
p str #=> String
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Delegation by using a tuple array
|
|
168
|
+
|
|
169
|
+
```ruby
|
|
170
|
+
def foo(x, y, z)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def proxy(dummy, *args)
|
|
174
|
+
foo(*args)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
proxy(:dummy, 1, 1.0, "str")
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Symbols
|
|
181
|
+
|
|
182
|
+
```ruby
|
|
183
|
+
# Symbols are handled as concrete values instead of abstract ones
|
|
184
|
+
p [:a, :b, :c] #=> [:a, :b, :c]
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Hashes
|
|
188
|
+
|
|
189
|
+
```ruby
|
|
190
|
+
# A Hash is a "type-to-type" map
|
|
191
|
+
h = { "int" => 1, "float" => 1.0 }
|
|
192
|
+
p h #=> {String=>Float | Integer}
|
|
193
|
+
p h["int"] #=> Float | Intger
|
|
194
|
+
|
|
195
|
+
# Symbol-key hashes (a.k.a. records) can have distinct types for each key as Symbols are concrete
|
|
196
|
+
h = { int: 1, float: 1.0 }
|
|
197
|
+
|
|
198
|
+
p h #=> {:int=>Integer, :float=>Float}
|
|
199
|
+
p h[:int] #=> Integer
|
|
200
|
+
p h[:float] #=> Float
|
|
201
|
+
|
|
202
|
+
# Symbol-key hash can be appropriately passed to a keyword method
|
|
203
|
+
def foo(int:, float:)
|
|
204
|
+
p [int, float] #=> [Integer, Float]
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
foo(**h)
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Structs
|
|
211
|
+
|
|
212
|
+
```ruby
|
|
213
|
+
FooBar = Struct.new(:foo, :bar)
|
|
214
|
+
|
|
215
|
+
obj = FooBar.new(42)
|
|
216
|
+
obj.foo = :dummy
|
|
217
|
+
obj.bar = "str"
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Result:
|
|
221
|
+
```
|
|
222
|
+
$ typeprof test.rb
|
|
223
|
+
# Classes
|
|
224
|
+
class FooBar < Struct
|
|
225
|
+
attr_accessor foo() : :dummy | Integer
|
|
226
|
+
attr_accessor bar() : String?
|
|
227
|
+
end
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Exceptions
|
|
231
|
+
|
|
232
|
+
```ruby
|
|
233
|
+
# TypeProf assumes that any exception may be raised anywhere
|
|
234
|
+
def foo
|
|
235
|
+
x = 1
|
|
236
|
+
x = "str"
|
|
237
|
+
x = :sym
|
|
238
|
+
ensure
|
|
239
|
+
p(x) #=> :sym | Integer | String
|
|
240
|
+
end
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Result:
|
|
244
|
+
```
|
|
245
|
+
$ typeprof test.rb
|
|
246
|
+
# Revealed types
|
|
247
|
+
# test.rb:6 #=> :sym | Integer | String
|
|
248
|
+
|
|
249
|
+
# Classes
|
|
250
|
+
class Object
|
|
251
|
+
def foo : -> :sym
|
|
252
|
+
end
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## RBS overloaded methods
|
|
256
|
+
|
|
257
|
+
```ruby
|
|
258
|
+
# TypeProf selects all overloaded method declarations that matches actual arguments
|
|
259
|
+
p foo(42) #=> Integer
|
|
260
|
+
p foo("str") #=> String
|
|
261
|
+
p foo(1.0) #=> failed to resolve overload: Object#foo
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
```
|
|
265
|
+
class Object
|
|
266
|
+
def foo: (Integer) -> Integer
|
|
267
|
+
| (String) -> String
|
|
268
|
+
end
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Flow-sensitive analysis demo: case/when with class constants
|
|
272
|
+
|
|
273
|
+
```ruby
|
|
274
|
+
def foo(n)
|
|
275
|
+
case n
|
|
276
|
+
when Integer
|
|
277
|
+
p n #=> Integer
|
|
278
|
+
when String
|
|
279
|
+
p n #=> String
|
|
280
|
+
else
|
|
281
|
+
p n #=> Float
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
foo(42)
|
|
286
|
+
foo(1.0)
|
|
287
|
+
foo("str")
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Result:
|
|
291
|
+
```
|
|
292
|
+
$ typeprof test.rb
|
|
293
|
+
# Revealed types
|
|
294
|
+
# test.rb:4 #=> Integer
|
|
295
|
+
# test.rb:8 #=> Float
|
|
296
|
+
# test.rb:6 #=> String
|
|
297
|
+
|
|
298
|
+
# Classes
|
|
299
|
+
class Object
|
|
300
|
+
def foo : (Float | Integer | String) -> (Float | Integer | String)
|
|
301
|
+
end
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## Flow-sensitive analysis demo: `is_a?` and `respond_to?`
|
|
305
|
+
|
|
306
|
+
```ruby
|
|
307
|
+
def foo(n)
|
|
308
|
+
if n.is_a?(Integer)
|
|
309
|
+
p n #=> Integer
|
|
310
|
+
else
|
|
311
|
+
p n #=> Float | String
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
if n.respond_to?(:times)
|
|
315
|
+
p n #=> Integer
|
|
316
|
+
else
|
|
317
|
+
p n #=> Float | String
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
foo(42)
|
|
322
|
+
foo(1.0)
|
|
323
|
+
foo("str")
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## Flow-sensitive analysis demo: `x || y`
|
|
327
|
+
|
|
328
|
+
```ruby
|
|
329
|
+
# ENV["FOO"] returns String? (which means String | nil)
|
|
330
|
+
p ENV["FOO"] #=> String?
|
|
331
|
+
|
|
332
|
+
# Using "|| (default value)" can force it to be non-nil
|
|
333
|
+
p ENV["FOO"] || "default" #=> String
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
## Recursion
|
|
337
|
+
|
|
338
|
+
```ruby
|
|
339
|
+
def fib(x)
|
|
340
|
+
if x <= 1
|
|
341
|
+
x
|
|
342
|
+
else
|
|
343
|
+
fib(x - 1) + fib(x - 2)
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
fib(40000)
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
Result:
|
|
351
|
+
```
|
|
352
|
+
$ typeprof test.rb
|
|
353
|
+
# Classes
|
|
354
|
+
class Object
|
|
355
|
+
def fib : (Integer) -> Integer
|
|
356
|
+
end
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
## "Stub-execution" that invokes methods without tests
|
|
360
|
+
|
|
361
|
+
```ruby
|
|
362
|
+
def foo(n)
|
|
363
|
+
# bar is invoked with Integer arguments
|
|
364
|
+
bar(42)
|
|
365
|
+
n
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def bar(n)
|
|
369
|
+
n
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
# As there is no test code to call methods foo and bar,
|
|
373
|
+
# TypeProf tries to invoke them with "untyped" arguments
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
Result:
|
|
377
|
+
```
|
|
378
|
+
$ typeprof test.rb
|
|
379
|
+
# Classes
|
|
380
|
+
class Object
|
|
381
|
+
def foo : (untyped) -> untyped
|
|
382
|
+
def bar : (Integer) -> Integer
|
|
383
|
+
end
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
## Library demo
|
|
387
|
+
|
|
388
|
+
```ruby
|
|
389
|
+
require "pathname"
|
|
390
|
+
|
|
391
|
+
p Pathname("foo") #=> Pathname
|
|
392
|
+
p Pathname("foo").dirname #=> Pathname
|
|
393
|
+
p Pathname("foo").ctime #=> Time
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
## More
|
|
397
|
+
|
|
398
|
+
See ruby/typeprof's [smoke](https://github.com/ruby/typeprof/tree/master/smoke) directory.
|