typed.rb 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +26 -0
  3. data/bin/typed.rb +110 -0
  4. data/lib/typed/language.rb +131 -0
  5. data/lib/typed/model/tm_abs.rb +104 -0
  6. data/lib/typed/model/tm_array_literal.rb +25 -0
  7. data/lib/typed/model/tm_boolean.rb +15 -0
  8. data/lib/typed/model/tm_boolean_operator.rb +34 -0
  9. data/lib/typed/model/tm_break.rb +24 -0
  10. data/lib/typed/model/tm_case_when.rb +38 -0
  11. data/lib/typed/model/tm_class.rb +63 -0
  12. data/lib/typed/model/tm_const.rb +29 -0
  13. data/lib/typed/model/tm_defined.rb +19 -0
  14. data/lib/typed/model/tm_error.rb +16 -0
  15. data/lib/typed/model/tm_float.rb +15 -0
  16. data/lib/typed/model/tm_for.rb +42 -0
  17. data/lib/typed/model/tm_fun.rb +165 -0
  18. data/lib/typed/model/tm_global_var.rb +22 -0
  19. data/lib/typed/model/tm_global_var_assignment.rb +20 -0
  20. data/lib/typed/model/tm_hash_literal.rb +32 -0
  21. data/lib/typed/model/tm_if_else.rb +24 -0
  22. data/lib/typed/model/tm_instance_var.rb +23 -0
  23. data/lib/typed/model/tm_instance_var_assignment.rb +32 -0
  24. data/lib/typed/model/tm_int.rb +15 -0
  25. data/lib/typed/model/tm_local_var_asgn.rb +35 -0
  26. data/lib/typed/model/tm_mass_asgn.rb +60 -0
  27. data/lib/typed/model/tm_mlhs.rb +87 -0
  28. data/lib/typed/model/tm_module.rb +51 -0
  29. data/lib/typed/model/tm_next.rb +24 -0
  30. data/lib/typed/model/tm_nil.rb +14 -0
  31. data/lib/typed/model/tm_range_literal.rb +30 -0
  32. data/lib/typed/model/tm_regexp.rb +27 -0
  33. data/lib/typed/model/tm_rescue.rb +27 -0
  34. data/lib/typed/model/tm_return.rb +24 -0
  35. data/lib/typed/model/tm_s_class.rb +30 -0
  36. data/lib/typed/model/tm_self.rb +22 -0
  37. data/lib/typed/model/tm_send.rb +300 -0
  38. data/lib/typed/model/tm_sequencing.rb +53 -0
  39. data/lib/typed/model/tm_string.rb +15 -0
  40. data/lib/typed/model/tm_string_interpolation.rb +21 -0
  41. data/lib/typed/model/tm_super.rb +27 -0
  42. data/lib/typed/model/tm_symbol.rb +15 -0
  43. data/lib/typed/model/tm_symbol_interpolation.rb +21 -0
  44. data/lib/typed/model/tm_try.rb +29 -0
  45. data/lib/typed/model/tm_var.rb +28 -0
  46. data/lib/typed/model/tm_while.rb +43 -0
  47. data/lib/typed/model.rb +48 -0
  48. data/lib/typed/prelude.rb +939 -0
  49. data/lib/typed/prelude_existential_registry.bin +0 -0
  50. data/lib/typed/prelude_generic_registry.bin +0 -0
  51. data/lib/typed/prelude_registry.bin +0 -0
  52. data/lib/typed/runtime/ast_parser.rb +589 -0
  53. data/lib/typed/runtime/method_signature_processor.rb +72 -0
  54. data/lib/typed/runtime/normalization/validations.rb +47 -0
  55. data/lib/typed/runtime/normalization.rb +196 -0
  56. data/lib/typed/runtime/parser_context.rb +36 -0
  57. data/lib/typed/runtime/type_parser.rb +215 -0
  58. data/lib/typed/runtime/type_registry.rb +170 -0
  59. data/lib/typed/runtime/type_signature_processor.rb +34 -0
  60. data/lib/typed/runtime.rb +33 -0
  61. data/lib/typed/type_signature/parser.rb +240 -0
  62. data/lib/typed/types/polymorphism/existential_type_variable.rb +13 -0
  63. data/lib/typed/types/polymorphism/generic_comparisons.rb +134 -0
  64. data/lib/typed/types/polymorphism/generic_variables.rb +24 -0
  65. data/lib/typed/types/polymorphism/type_variable.rb +138 -0
  66. data/lib/typed/types/polymorphism/type_variable_register.rb +298 -0
  67. data/lib/typed/types/polymorphism/unification.rb +579 -0
  68. data/lib/typed/types/ty_boolean.rb +15 -0
  69. data/lib/typed/types/ty_dynamic.rb +39 -0
  70. data/lib/typed/types/ty_either.rb +168 -0
  71. data/lib/typed/types/ty_error.rb +18 -0
  72. data/lib/typed/types/ty_existential_type.rb +22 -0
  73. data/lib/typed/types/ty_function.rb +144 -0
  74. data/lib/typed/types/ty_generic_function.rb +115 -0
  75. data/lib/typed/types/ty_generic_object.rb +180 -0
  76. data/lib/typed/types/ty_generic_singleton_object.rb +238 -0
  77. data/lib/typed/types/ty_object.rb +256 -0
  78. data/lib/typed/types/ty_singleton_object.rb +78 -0
  79. data/lib/typed/types/ty_stack_jump.rb +44 -0
  80. data/lib/typed/types/ty_top_level_object.rb +38 -0
  81. data/lib/typed/types.rb +60 -0
  82. data/lib/typed/typing_context.rb +206 -0
  83. data/lib/typed/version.rb +3 -0
  84. data/lib/typed.rb +161 -0
  85. data/spec/lib/ast_parser_spec.rb +101 -0
  86. data/spec/lib/examples/animals.rb +44 -0
  87. data/spec/lib/examples/counter.rb +16 -0
  88. data/spec/lib/examples/if.rb +31 -0
  89. data/spec/lib/language_spec.rb +36 -0
  90. data/spec/lib/model/tm_abs_spec.rb +66 -0
  91. data/spec/lib/model/tm_array_literal_spec.rb +36 -0
  92. data/spec/lib/model/tm_case_when_spec.rb +39 -0
  93. data/spec/lib/model/tm_class_spec.rb +67 -0
  94. data/spec/lib/model/tm_defined_spec.rb +10 -0
  95. data/spec/lib/model/tm_for_spec.rb +150 -0
  96. data/spec/lib/model/tm_fun_spec.rb +11 -0
  97. data/spec/lib/model/tm_hash_literal_spec.rb +40 -0
  98. data/spec/lib/model/tm_mass_asgn_spec.rb +104 -0
  99. data/spec/lib/model/tm_module_spec.rb +42 -0
  100. data/spec/lib/model/tm_regexp_spec.rb +9 -0
  101. data/spec/lib/model/tm_return_spec.rb +47 -0
  102. data/spec/lib/model/tm_s_class_spec.rb +27 -0
  103. data/spec/lib/model/tm_self_spec.rb +19 -0
  104. data/spec/lib/model/tm_string_interpolation_spec.rb +10 -0
  105. data/spec/lib/model/tm_symbol_interpolation_spec.rb +10 -0
  106. data/spec/lib/model/tm_symbol_spec.rb +9 -0
  107. data/spec/lib/model/tm_while_spec.rb +141 -0
  108. data/spec/lib/polymorphism/type_variable_spec.rb +14 -0
  109. data/spec/lib/polymorphism/unification_spec.rb +328 -0
  110. data/spec/lib/prelude/array_spec.rb +263 -0
  111. data/spec/lib/prelude/class_spec.rb +12 -0
  112. data/spec/lib/prelude/enumerable_spec.rb +278 -0
  113. data/spec/lib/prelude/enumerator_spec.rb +101 -0
  114. data/spec/lib/prelude/hash_spec.rb +361 -0
  115. data/spec/lib/prelude/kernel_spec.rb +23 -0
  116. data/spec/lib/prelude/object_spec.rb +22 -0
  117. data/spec/lib/prelude/pair_spec.rb +16 -0
  118. data/spec/lib/prelude/showable_spec.rb +31 -0
  119. data/spec/lib/prelude/string_spec.rb +98 -0
  120. data/spec/lib/runtime/normalization_spec.rb +29 -0
  121. data/spec/lib/runtime/validations_spec.rb +56 -0
  122. data/spec/lib/runtime_spec.rb +503 -0
  123. data/spec/lib/type_signature/parser_spec.rb +239 -0
  124. data/spec/lib/types/comparisons_spec.rb +35 -0
  125. data/spec/lib/types/polymorphism/generic_comparisons_spec.rb +492 -0
  126. data/spec/lib/types/polymorphism/type_variable_register_spec.rb +128 -0
  127. data/spec/lib/types/ty_dynamic_spec.rb +103 -0
  128. data/spec/lib/types/ty_either_spec.rb +288 -0
  129. data/spec/lib/types/ty_error_spec.rb +18 -0
  130. data/spec/lib/types/ty_generic_object_spec.rb +78 -0
  131. data/spec/lib/types/ty_generic_singleton_object_spec.rb +288 -0
  132. data/spec/lib/types/typing_context_spec.rb +86 -0
  133. data/spec/lib/types_spec.rb +174 -0
  134. data/spec/lib/typing/boolean_asgn_spec.rb +134 -0
  135. data/spec/lib/typing/break_spec.rb +79 -0
  136. data/spec/lib/typing/generics_spec.rb +191 -0
  137. data/spec/lib/typing/instance_vars_spec.rb +103 -0
  138. data/spec/lib/typing/next_spec.rb +29 -0
  139. data/spec/lib/typing/op_asgn_spec.rb +104 -0
  140. data/spec/lib/typing/overriden_methods_spec.rb +31 -0
  141. data/spec/lib/typing/subtyping_spec.rb +112 -0
  142. data/spec/lib/typing/tm_boolean_operator_spec.rb +100 -0
  143. data/spec/lib/typing/tm_boolean_spec.rb +61 -0
  144. data/spec/lib/typing/tm_const_spec.rb +28 -0
  145. data/spec/lib/typing/tm_defined_spec.rb +12 -0
  146. data/spec/lib/typing/tm_fun_spec.rb +347 -0
  147. data/spec/lib/typing/tm_global_var_spec.rb +33 -0
  148. data/spec/lib/typing/tm_if_else_spec.rb +104 -0
  149. data/spec/lib/typing/tm_ignore_spec.rb +24 -0
  150. data/spec/lib/typing/tm_instance_vars_spec.rb +117 -0
  151. data/spec/lib/typing/tm_local_var_asgn_spec.rb +134 -0
  152. data/spec/lib/typing/tm_mlhs_spec.rb +164 -0
  153. data/spec/lib/typing/tm_module_spec.rb +89 -0
  154. data/spec/lib/typing/tm_raise_spec.rb +31 -0
  155. data/spec/lib/typing/tm_range_literal_spec.rb +25 -0
  156. data/spec/lib/typing/tm_regexp_spec.rb +14 -0
  157. data/spec/lib/typing/tm_return_spec.rb +45 -0
  158. data/spec/lib/typing/tm_send_casting_spec.rb +26 -0
  159. data/spec/lib/typing/tm_send_class_methods_spec.rb +42 -0
  160. data/spec/lib/typing/tm_send_generic_apply_spec.rb +103 -0
  161. data/spec/lib/typing/tm_send_generic_methods_spec.rb +77 -0
  162. data/spec/lib/typing/tm_send_initialize_spec.rb +68 -0
  163. data/spec/lib/typing/tm_send_lambda_spec.rb +135 -0
  164. data/spec/lib/typing/tm_send_spec.rb +217 -0
  165. data/spec/lib/typing/tm_send_yield_block_spec.rb +308 -0
  166. data/spec/lib/typing/tm_sequencing_spec.rb +174 -0
  167. data/spec/lib/typing/tm_string_interpolation_spec.rb +19 -0
  168. data/spec/lib/typing/tm_super_spec.rb +63 -0
  169. data/spec/lib/typing/tm_symbol_interpolation_spec.rb +19 -0
  170. data/spec/lib/typing/tm_symbol_spec.rb +14 -0
  171. data/spec/lib/typing/tm_try_spec.rb +73 -0
  172. data/spec/spec_helper.rb +140 -0
  173. metadata +216 -0
@@ -0,0 +1,492 @@
1
+ require_relative '../../../spec_helper'
2
+
3
+ describe TypedRb::Types::Polymorphism::GenericComparisons do
4
+
5
+ let(:language) { TypedRb::Language.new }
6
+
7
+ let(:int_bound) do
8
+ TypedRb::Types::TySingletonObject.new(Integer)
9
+ end
10
+
11
+ let(:numeric_bound) do
12
+ TypedRb::Types::TySingletonObject.new(Numeric)
13
+ end
14
+
15
+ let(:object_bound) do
16
+ TypedRb::Types::TySingletonObject.new(Object)
17
+ end
18
+
19
+ # Array[? < Integer]
20
+ let(:array_extends_integer) do
21
+ type_arg = TypedRb::Types::Polymorphism::TypeVariable.new('Array:T:1:?',
22
+ :gen_name => false,
23
+ :upper_bound => int_bound)
24
+ TypedRb::Types::TyGenericSingletonObject.new(Array, [type_arg])
25
+ end
26
+
27
+ # Array[? > Numeric]
28
+ let(:array_super_numeric) do
29
+ type_arg = TypedRb::Types::Polymorphism::TypeVariable.new('Array:T:1:?',
30
+ :gen_name => false,
31
+ :lower_bound => numeric_bound)
32
+ # type_arg.bind(numeric_bound)
33
+ TypedRb::Types::TyGenericSingletonObject.new(Array, [type_arg])
34
+ end
35
+
36
+ # Array[?]
37
+ let(:arg0) do
38
+ type_arg = TypedRb::Types::Polymorphism::TypeVariable.new('?', :gen_name => false)
39
+ TypedRb::Types::TyGenericSingletonObject.new(Array, [type_arg])
40
+ end
41
+
42
+ # Array[Integer]
43
+ let(:arg1) do
44
+ type_arg = TypedRb::Types::Polymorphism::TypeVariable.new('Array:T', :gen_name => false,
45
+ :upper_bound => int_bound,
46
+ :lower_bound => int_bound)
47
+ type_arg.bind(int_bound)
48
+ TypedRb::Types::TyGenericSingletonObject.new(Array, [type_arg])
49
+ end
50
+
51
+ # Array[Numeric]
52
+ let(:arg2) do
53
+ type_arg = TypedRb::Types::Polymorphism::TypeVariable.new('Array:T', :gen_name => false,
54
+ :upper_bound => numeric_bound,
55
+ :lower_bound => numeric_bound)
56
+ type_arg.bind(numeric_bound)
57
+ TypedRb::Types::TyGenericSingletonObject.new(Array, [type_arg])
58
+ end
59
+
60
+ # Array[? < Numeric]
61
+ let(:arg3) do
62
+ type_arg = TypedRb::Types::Polymorphism::TypeVariable.new('Array:T:1:?',
63
+ :gen_name => false,
64
+ :upper_bound => numeric_bound)
65
+ # type_arg.bind(numeric_bound)
66
+ TypedRb::Types::TyGenericSingletonObject.new(Array, [type_arg])
67
+ end
68
+
69
+ # Array[? > Integer]
70
+ let(:arg4) do
71
+ type_arg = TypedRb::Types::Polymorphism::TypeVariable.new('Array:T:1:?',
72
+ :gen_name => false,
73
+ :lower_bound => int_bound)
74
+ # type_arg.bind(int_bound)
75
+ TypedRb::Types::TyGenericSingletonObject.new(Array, [type_arg])
76
+ end
77
+
78
+ # Array[? > Object]
79
+ let(:arg5) do
80
+ type_arg = TypedRb::Types::Polymorphism::TypeVariable.new('Array:T:1:?',
81
+ :gen_name => false,
82
+ :lower_bound => object_bound)
83
+ # type_arg.bind(object_bound)
84
+ TypedRb::Types::TyGenericSingletonObject.new(Array, [type_arg])
85
+ end
86
+
87
+ context 'base of :lt comparison is a generic type with an upper_bound' do
88
+ it 'checks correctly: Array<?> :lt Array<? extends Integer> => [?, ?] /< [?, Integer] => FALSE' do
89
+ code = <<__CODE
90
+ ts '#tgcoll2 / Array[? < Integer] -> unit'
91
+ def tgcoll2(gc); end
92
+
93
+ arg = Array.('[?]').new
94
+
95
+ tgcoll2(arg)
96
+ __CODE
97
+ expect {
98
+ language.check(code)
99
+ }.to raise_error(TypedRb::TypeCheckError)
100
+ end
101
+
102
+ it 'checks correctly: Array<?> :lt Array<? extends Integer> => [?, ?] /< [?, Integer] => FALSE' do
103
+ expect(arg0.compatible?(array_extends_integer, :lt)).to be_falsey
104
+ end
105
+
106
+
107
+ it 'checks correctly: Array<? extends Integer> :lt correctly: Array<?> => [?, Integer] < [?, ?] => TRUE' do
108
+ code = <<__CODE
109
+ ts '#tgcoll3 / Array[?] -> unit'
110
+ def tgcoll3(gc); end
111
+
112
+ arg = Array.('[? < Integer]').new
113
+
114
+ tgcoll3(arg)
115
+ __CODE
116
+
117
+ result = language.check(code)
118
+ expect(result.ruby_type).to eq(NilClass)
119
+ end
120
+
121
+ it 'checks correctly: Array<? extends Integer> :lt correctly: Array<?> => [?, Integer] < [?, ?] => TRUE' do
122
+ expect(array_extends_integer.compatible?(arg0, :lt)).to be_truthy
123
+ end
124
+
125
+
126
+ it 'checks correctly: Array<Integer> :lt Array<? extends Integer> => [Integer, Integer] < [?, Integer] => TRUE' do
127
+ code = <<__CODE
128
+ ts '#tgcoll4 / Array[? < Integer] -> unit'
129
+ def tgcoll4(gc); end
130
+
131
+ arg = Array.(Integer).new
132
+
133
+ tgcoll4(arg)
134
+ __CODE
135
+
136
+ result = language.check(code)
137
+ expect(result.ruby_type).to eq(NilClass)
138
+ end
139
+
140
+ it 'checks correctly: Array<Integer> :lt Array<? extends Integer> => [Integer, Integer] < [?, Integer] => TRUE' do
141
+ expect(arg1.compatible?(array_extends_integer, :lt)).to be_truthy
142
+ end
143
+
144
+
145
+ it 'checks correctly: Array<? extends Integer> :lt Array<Integer> => [?, Integer] /< [Integer, Integer] => FALSE' do
146
+ code = <<__CODE
147
+ ts '#tgcoll5 / Array[Integer] -> unit'
148
+ def tgcoll5(gc); end
149
+
150
+ arg = Array.('[? < Integer]').new
151
+ tgcoll5(arg)
152
+ __CODE
153
+ expect {
154
+ language.check(code)
155
+ }.to raise_error(TypedRb::TypeCheckError)
156
+ end
157
+
158
+ it 'checks correctly: Array<? extends Integer> :lt Array<Integer> => [?, Integer] /< [Integer, Integer] => FALSE' do
159
+ expect(array_extends_integer.compatible?(arg1, :lt)).to be_falsey
160
+ end
161
+
162
+
163
+
164
+ it 'checks correctly: Array<Numeric> :lt Array<? extends Integer> => [Numeric, Numeric] /< [?, Integer] => FALSE' do
165
+ code = <<__CODE
166
+ ts '#tgcoll6 / Array[? < Integer] -> unit'
167
+ def tgcoll6(gc); end
168
+
169
+ arg = Array.(Numeric).new
170
+ tgcoll6(arg)
171
+ __CODE
172
+ expect {
173
+ language.check(code)
174
+ }.to raise_error(TypedRb::TypeCheckError)
175
+ end
176
+
177
+ it 'checks correctly: Array<Numeric> :lt Array<? extends Integer> => [Numeric, Numeric] /< [?, Integer] => FALSE' do
178
+ expect(arg2.compatible?(array_extends_integer, :lt)).to be_falsey
179
+ end
180
+
181
+
182
+ it 'checks correctly: Array<? extends Integer> :lt Array<Numeric> => [?, Integer] /< [Numeric, Numeric] => FALSE' do
183
+ code = <<__CODE
184
+ ts '#tgcoll7 / Array[Numeric] -> unit'
185
+ def tgcoll7(gc); end
186
+
187
+ arg = Array.('[? < Integer]').new
188
+ tgcoll7(arg)
189
+ __CODE
190
+ expect {
191
+ language.check(code)
192
+ }.to raise_error(TypedRb::TypeCheckError)
193
+ end
194
+
195
+ it 'checks correctly: Array<? extends Integer> :lt Array<Numeric> => [?, Integer] /< [Numeric, Numeric] => FALSE' do
196
+ expect(array_extends_integer.compatible?(arg2, :lt)).to be_falsey
197
+ end
198
+
199
+
200
+
201
+ it 'checks correctly: Array<? extends Integer> :lt Array<? extends Integer> => [?, Integer] < [?, Integer] => TRUE' do
202
+ code = <<__CODE
203
+ ts '#tgcoll9 / Array[? < Integer] -> unit'
204
+ def tgcoll9(gc); end
205
+
206
+ arg = Array.('[? < Integer]').new
207
+
208
+ tgcoll9(arg)
209
+ __CODE
210
+
211
+ result = language.check(code)
212
+ expect(result.ruby_type).to eq(NilClass)
213
+ end
214
+
215
+ it 'checks correctly: Array<? extends Integer> :lt Array<? extends Integer> => [?, Integer] < [?, Integer] => TRUE' do
216
+ expect(array_extends_integer.compatible?(array_extends_integer, :lt)).to be_truthy
217
+ end
218
+
219
+
220
+ it 'checks correctly: Array<? extends Numeric> :lt Array<? extends Integer> => [?, Numeric] /< [?, Integer] => FALSE' do
221
+ code = <<__CODE
222
+ ts '#tgcoll10 / Array[? < Integer] -> unit'
223
+ def tgcoll10(gc); end
224
+
225
+ arg = Array.('[? < Numeric]').new
226
+ tgcoll10(arg)
227
+ __CODE
228
+ expect {
229
+ language.check(code)
230
+ }.to raise_error(TypedRb::TypeCheckError)
231
+ end
232
+
233
+ it 'checks correctly: Array<? extends Numeric> :lt Array<? extends Integer> => [?, Numeric] /< [?, Integer] => FALSE' do
234
+ expect(arg3.compatible?(array_extends_integer, :lt)).to be_falsey
235
+ end
236
+
237
+
238
+ it 'checks correctly: Array<? extends Integer> :lt Array<? extends Numeric> => [?, Integer] < [?, Numeric] => TRUE' do
239
+ code = <<__CODE
240
+ ts '#tgcoll11 / Array[? < Numeric] -> unit'
241
+ def tgcoll11(gc); end
242
+
243
+ arg = Array.('[? < Integer]').new
244
+
245
+ tgcoll11(arg)
246
+ __CODE
247
+
248
+ result = language.check(code)
249
+ expect(result.ruby_type).to eq(NilClass)
250
+ end
251
+
252
+ it 'checks correctly: Array<? extends Integer> :lt Array<? extends Numeric> => [?, Integer] < [?, Numeric] => TRUE' do
253
+ expect(array_extends_integer.compatible?(arg3, :lt)).to be_truthy
254
+ end
255
+
256
+
257
+ it 'checks correctly: Array<? super Integer> :lt Array<? extends Integer> => [Integer, ?] /< [?, Integer] => FALSE' do
258
+ code = <<__CODE
259
+ ts '#tgcoll12 / Array[? < Integer] -> unit'
260
+ def tgcoll12(gc); end
261
+
262
+ arg = Array.('[? > Integer]').new
263
+ tgcoll12(arg)
264
+ __CODE
265
+ expect {
266
+ language.check(code)
267
+ }.to raise_error(TypedRb::TypeCheckError)
268
+ end
269
+
270
+ it 'checks correctly: Array<? super Integer> :lt Array<? extends Integer> => [Integer, ?] /< [?, Integer] => FALSE' do
271
+ expect(arg4.compatible?(array_extends_integer, :lt)).to be_falsey
272
+ end
273
+
274
+
275
+ it 'checks correctly: Array<? extends Integer> :lt Array<? super Integer> => [?, Integer] /< [Integer, ?] => FALSE' do
276
+ code = <<__CODE
277
+ ts '#tgcoll13 / Array[? > Integer] -> unit'
278
+ def tgcoll13(gc); end
279
+
280
+ arg = Array.('[? < Integer]').new
281
+ tgcoll13(arg)
282
+ __CODE
283
+ expect {
284
+ language.check(code)
285
+ }.to raise_error(TypedRb::TypeCheckError)
286
+ end
287
+
288
+ it 'checks correctly: Array<? extends Integer> :lt Array<? super Integer> => [?, Integer] /< [Integer, ?] => FALSE' do
289
+ expect(array_extends_integer.compatible?(arg4, :lt)).to be_falsey
290
+ end
291
+
292
+
293
+ it 'checks correctly: Array<? super Numeric> :lt Array<? extends Integer> => [Numeric, ?] /< [?, Integer] => FALSE' do
294
+ code = <<__CODE
295
+ ts '#tgcoll14 / Array[? < Integer] -> unit'
296
+ def tgcoll14(gc); end
297
+
298
+ arg = Array.('[? > Numeric]').new
299
+ tgcoll14(arg)
300
+ __CODE
301
+ expect {
302
+ language.check(code)
303
+ }.to raise_error(TypedRb::TypeCheckError)
304
+ end
305
+
306
+ it 'checks correctly: Array<? super Numeric> :lt Array<? extends Integer> => [Numeric, ?] /< [?, Integer] => FALSE' do
307
+ expect(array_super_numeric.compatible?(array_extends_integer, :lt)).to be_falsey
308
+ end
309
+
310
+
311
+ it 'checks correctly: Array<? extends Integer> :lt Array<? super Numeric> => [?, Integer] /< [Numeric, ?] => FALSE' do
312
+ code = <<__CODE
313
+ ts '#tgcoll15 / Array[? > Numeric] -> unit'
314
+ def tgcoll15(gc); end
315
+
316
+ arg = Array.('[? < Integer]').new
317
+ tgcoll15(arg)
318
+ __CODE
319
+ expect {
320
+ language.check(code)
321
+ }.to raise_error(TypedRb::TypeCheckError)
322
+ end
323
+
324
+ it 'checks correctly: Array<? extends Integer> :lt Array<? super Numeric> => [?, Integer] /< [Numeric, ?] => FALSE' do
325
+ expect(array_extends_integer.compatible?(array_super_numeric, :lt)).to be_falsey
326
+ end
327
+ end
328
+
329
+ context 'base of :lt comparison is a generic type with a lower_bound' do
330
+
331
+ it 'checks compatible? Array<?> :lt Array<? super Numeric> => [?, ?] /< [Numeric, ?] => FALSE' do
332
+ expect(arg0.compatible?(array_super_numeric, :lt)).to be_falsey
333
+ end
334
+
335
+ it 'checks compatible? Array<? super Numeric> :lt Array<?> => [Numeric, ?] /< [?, ?] => TRUE' do
336
+ expect(array_super_numeric.compatible?(arg0, :lt)).to be_truthy
337
+ end
338
+
339
+ it 'checks correctly: Array<Integer> :lt Array<? super Numeric> => [Integer, Integer] /< [Numeric, ?] => FALSE' do
340
+ expect(arg1.compatible?(array_super_numeric, :lt)).to be_falsey
341
+ end
342
+
343
+ it 'checks correctly: Array<? super Numeric> :lt Array<Integer> => [Numeric, ?] /< [Integer, Integer] => FALSE' do
344
+ expect(array_super_numeric.compatible?(arg1, :lt)).to be_falsey
345
+ end
346
+
347
+ it 'checks correctly: Array<Numeric> :lt Array<? super Numeric> => [Numeric, Numeric] < [Numeric, ?] => TRUE' do
348
+ expect(arg2.compatible?(array_super_numeric, :lt)).to be_truthy
349
+ end
350
+
351
+ it 'checks correctly: Array<? super Numeric> :lt Array<Numeric> => [Numeric, ?] < [Numeric, Numeric] => FALSE' do
352
+ expect(array_super_numeric.compatible?(arg2, :lt)).to be_falsey
353
+ end
354
+
355
+ it 'checks correctly: Array<? extends Numeric> :lt Array<? super Numeric> => [?, Numeric] /< [Numeric, ?] => FALSE' do
356
+ expect(arg3.compatible?(array_super_numeric, :lt)).to be_falsey
357
+ end
358
+
359
+ it 'checks correctly: Array<? super Numeric> :lt Array<? extends Numeric> => [Numeric,?] /< [?, Numeric] => FALSE' do
360
+ expect(array_super_numeric.compatible?(arg3, :lt)).to be_falsey
361
+ end
362
+
363
+ it 'checks correctly: Array<? super Integer> :lt Array<? super Numeric> => [Integer, ?] /< [Numeric, ?] => FALSE' do
364
+ expect(arg4.compatible?(array_super_numeric, :lt)).to be_falsey
365
+ end
366
+
367
+ it 'checks correctly: Array<? super Numeric> :lt Array<? super Integer> => [Numeric, ?] < [Integer, ?] => TRUE' do
368
+ expect(array_super_numeric.compatible?(arg4, :lt)).to be_truthy
369
+ end
370
+
371
+ it 'checks correctly: Array<? super Numeric> :lt Array<? super Numeric> => [Numeric, ?] < [Numeric, ?] => TRUE' do
372
+ expect(array_super_numeric.compatible?(array_super_numeric, :lt)).to be_truthy
373
+ end
374
+
375
+ it 'checks correctly: Array<? super Object> :lt Array<? super Numeric> => [Object, ?] < [Numeric, ?] => TRUE' do
376
+ expect(arg5.compatible?(array_super_numeric, :lt)).to be_truthy
377
+ end
378
+
379
+ it 'checks correctly: Array<? super Numeric> :lt Array<? super Object> => [Numeric, ?] /< [Object, ?] => FALSE' do
380
+ expect(array_super_numeric.compatible?(arg5, :lt)).to be_falsey
381
+ end
382
+ end
383
+
384
+ context 'base of :gt comparison is a generic type with an upper_bound' do
385
+ it 'checks correctly: Array<?> :gt Array<? extends Integer> => [?, ?] > [?, Integer] => TRUE' do
386
+ expect(arg0.compatible?(array_extends_integer, :gt)).to be_truthy
387
+ end
388
+
389
+ it 'checks correctly: Array<? extends Integer> :gt correctly: Array<?> => [?, Integer] /> [?, ?] => FALSE' do
390
+ expect(array_extends_integer.compatible?(arg0, :gt)).to be_falsey
391
+ end
392
+
393
+ it 'checks correctly: Array<Integer> :gt Array<? extends Integer> => [Integer, Integer] /> [?, Integer] => FALSE' do
394
+ expect(arg1.compatible?(array_extends_integer, :gt)).to be_falsey
395
+ end
396
+
397
+ it 'checks correctly: Array<? extends Integer> :gt Array<Integer> => [?, Integer] > [Integer, Integer] => TRUE' do
398
+ expect(array_extends_integer.compatible?(arg1, :gt)).to be_truthy
399
+ end
400
+
401
+ it 'checks correctly: Array<Numeric> :gt Array<? extends Integer> => [Numeric, Numeric] /> [?, Integer] => FALSE' do
402
+ expect(arg2.compatible?(array_extends_integer, :gt)).to be_falsey
403
+ end
404
+
405
+ it 'checks correctly: Array<? extends Integer> :gt Array<Numeric> => [?, Integer] /> [Numeric, Numeric] => FALSE' do
406
+ expect(array_extends_integer.compatible?(arg2, :gt)).to be_falsey
407
+ end
408
+
409
+ it 'checks correctly: Array<? extends Integer> :gt Array<? extends Integer> => [?, Integer] > [?, Integer] => TRUE' do
410
+ expect(array_extends_integer.compatible?(array_extends_integer, :gt)).to be_truthy
411
+ end
412
+
413
+ it 'checks correctly: Array<? extends Numeric> :gt Array<? extends Integer> => [?, Numeric] > [?, Integer] => TRUE' do
414
+ expect(arg3.compatible?(array_extends_integer, :gt)).to be_truthy
415
+ end
416
+
417
+ it 'checks correctly: Array<? extends Integer> :gt Array<? extends Numeric> => [?, Integer] > [?, Numeric] => FALSE' do
418
+ expect(array_extends_integer.compatible?(arg3, :gt)).to be_falsey
419
+ end
420
+
421
+ it 'checks correctly: Array<? super Integer> :gt Array<? extends Integer> => [Integer, ?] /> [?, Integer] => FALSE' do
422
+ expect(arg4.compatible?(array_extends_integer, :gt)).to be_falsey
423
+ end
424
+
425
+ it 'checks correctly: Array<? extends Integer> :gt Array<? super Integer> => [?, Integer] /> [Integer, ?] => FALSE' do
426
+ expect(array_extends_integer.compatible?(arg4, :gt)).to be_falsey
427
+ end
428
+
429
+ it 'checks correctly: Array<? super Numeric> :gt Array<? extends Integer> => [Numeric, ?] /> [?, Integer] => FALSE' do
430
+ expect(array_super_numeric.compatible?(array_extends_integer, :gt)).to be_falsey
431
+ end
432
+
433
+ it 'checks correctly: Array<? extends Integer> :gt Array<? super Numeric> => [?, Integer] /> [Numeric, ?] => FALSE' do
434
+ expect(array_extends_integer.compatible?(array_super_numeric, :gt)).to be_falsey
435
+ end
436
+ end
437
+
438
+ context 'base of :gt comparison is a generic type with a lower_bound' do
439
+
440
+ it 'checks compatible? Array<?> :gt Array<? super Numeric> => [?, ?] > [Numeric, ?] => TRUE' do
441
+ expect(arg0.compatible?(array_super_numeric, :gt)).to be_truthy
442
+ end
443
+
444
+ it 'checks compatible? Array<? super Numeric> :gt Array<?> => [Numeric, ?] /> [?, ?] => FALSE' do
445
+ expect(array_super_numeric.compatible?(arg0, :gt)).to be_falsey
446
+ end
447
+
448
+ it 'checks correctly: Array<Integer> :gt Array<? super Numeric> => [Integer, Integer] /> [Numeric, ?] => FALSE' do
449
+ expect(arg1.compatible?(array_super_numeric, :gt)).to be_falsey
450
+ end
451
+
452
+ it 'checks correctly: Array<? super Numeric> :gt Array<Integer> => [Numeric, ?] /> [Integer, Integer] => FALSE' do
453
+ expect(array_super_numeric.compatible?(arg1, :gt)).to be_falsey
454
+ end
455
+
456
+ it 'checks correctly: Array<Numeric> :gt Array<? super Numeric> => [Numeric, Numeric] /> [Numeric, ?] => FALSE' do
457
+ expect(arg2.compatible?(array_super_numeric, :gt)).to be_falsey
458
+ end
459
+
460
+ it 'checks correctly: Array<? super Numeric> :gt Array<Numeric> => [Numeric, ?] > [Numeric, Numeric] => TRUE' do
461
+ expect(array_super_numeric.compatible?(arg2, :gt)).to be_truthy
462
+ end
463
+
464
+ it 'checks correctly: Array<? extends Numeric> :gt Array<? super Numeric> => [?, Numeric] /> [Numeric, ?] => FALSE' do
465
+ expect(arg3.compatible?(array_super_numeric, :gt)).to be_falsey
466
+ end
467
+
468
+ it 'checks correctly: Array<? super Numeric> :gt Array<? extends Numeric> => [Numeric,?] /> [?, Numeric] => FALSE' do
469
+ expect(array_super_numeric.compatible?(arg3, :gt)).to be_falsey
470
+ end
471
+
472
+ it 'checks correctly: Array<? super Integer> :gt Array<? super Numeric> => [Integer, ?] > [Numeric, ?] => TRUE' do
473
+ expect(arg4.compatible?(array_super_numeric, :gt)).to be_truthy
474
+ end
475
+
476
+ it 'checks correctly: Array<? super Numeric> :gt Array<? super Integer> => [Numeric, ?] > [Integer, ?] => FALSE' do
477
+ expect(array_super_numeric.compatible?(arg4, :gt)).to be_falsey
478
+ end
479
+
480
+ it 'checks correctly: Array<? super Numeric> :gt Array<? super Numeric> => [Numeric, ?] > [Numeric, ?] => TRUE' do
481
+ expect(array_super_numeric.compatible?(array_super_numeric, :gt)).to be_truthy
482
+ end
483
+
484
+ it 'checks correctly: Array<? super Object> :gt Array<? super Numeric> => [Object, ?] /> [Numeric, ?] => FALSE' do
485
+ expect(arg5.compatible?(array_super_numeric, :gt)).to be_falsey
486
+ end
487
+
488
+ it 'checks correctly: Array<? super Numeric> :gt Array<? super Object> => [Numeric, ?] > [Object, ?] => TRUE' do
489
+ expect(array_super_numeric.compatible?(arg5, :gt)).to be_truthy
490
+ end
491
+ end
492
+ end
@@ -0,0 +1,128 @@
1
+ require_relative '../../../spec_helper'
2
+
3
+ describe TypedRb::Types::Polymorphism::TypeVariableRegister do
4
+ describe '#initialize' do
5
+ it 'should be possible to create a register without parent' do
6
+ subject = described_class.new(nil,:top_level)
7
+ expect(subject.parent).to be_nil
8
+ end
9
+
10
+ it 'should be possible to create a nested register' do
11
+ parent = described_class.new(nil,:top_level)
12
+ child = described_class.new(parent, :lambda)
13
+ expect(parent.children).to include(child)
14
+ expect(child.parent).to eq(parent)
15
+ end
16
+ end
17
+
18
+ describe '#type_variable_for' do
19
+ it 'finds a type variable for a single register' do
20
+ register = described_class.new(nil,:top_level)
21
+ type_variable_one = register.type_variable_for(:instance_variable, '@a', String.ancestors)
22
+ type_variable_two = register.type_variable_for(:instance_variable, '@a', String.ancestors)
23
+ expect(type_variable_one).to eq(type_variable_two)
24
+ end
25
+
26
+ it 'finds a type_variable in a nested register' do
27
+ parent = described_class.new(nil,:top_level)
28
+ child = described_class.new(parent, :lambda)
29
+ type_variable_one = parent.type_variable_for(:instance_variable, '@a', String.ancestors)
30
+ type_variable_two = child.type_variable_for(:instance_variable, '@a', String.ancestors)
31
+ expect(type_variable_one).to eq(type_variable_two)
32
+ end
33
+
34
+ it 'finds a type variable in the hierarchy' do
35
+ parent = described_class.new(nil,:top_level)
36
+ child = described_class.new(parent, :lambda)
37
+ type_variable_one = parent.type_variable_for(:instance_variable, '@a', Object.ancestors)
38
+ type_variable_two = child.type_variable_for(:instance_variable, '@a', String.ancestors)
39
+ expect(type_variable_one).to eq(type_variable_two)
40
+ end
41
+ end
42
+
43
+ describe '#type_variable_for_message' do
44
+ it 'finds a type variable for a single register' do
45
+ register = described_class.new(nil,:top_level)
46
+ type_variable_one = register.type_variable_for_message('x', 'msg')
47
+ type_variable_two = register.type_variable_for_message('x', 'msg')
48
+ expect(type_variable_one).to eq(type_variable_two)
49
+ end
50
+
51
+ it 'finds a type_variable in a nested register' do
52
+ parent = described_class.new(nil,:top_level)
53
+ child = described_class.new(parent, :lambda)
54
+ type_variable_one = parent.type_variable_for_message('x', 'msg')
55
+ type_variable_two = child.type_variable_for_message('x', 'msg')
56
+ expect(type_variable_one).to eq(type_variable_two)
57
+ end
58
+ end
59
+
60
+ describe '#apply_type' do
61
+ it 'applies a type to a send constraint' do
62
+ TypedRb::Types::TypingContext.type_variables_register = described_class.new(nil,:top_level)
63
+ register = TypedRb::Types::TypingContext.type_variables_register
64
+ xvar = register.type_variable_for_abstraction(:lambda,'x',top_level_typing_context)
65
+ ret_type = xvar.add_message_constraint('id', [xvar])
66
+
67
+ renamed_x = tyvariable('renamed_x')
68
+ renamed_return_id_x = tyvariable('renamed_ret_id_x')
69
+ renamed_register = register.send(:apply_type,nil, {xvar.variable => renamed_x, ret_type.variable => renamed_return_id_x})
70
+
71
+ constraints = renamed_register.all_constraints
72
+ expect(constraints.size).to eq(1)
73
+ variable, type, info = constraints.first
74
+ expect(type).to eq :send
75
+ expect(variable).to eq renamed_x
76
+ expect(info[:return]).to eq renamed_return_id_x
77
+ expect(info[:args].size).to eq 1
78
+ expect(info[:args].first).to eq renamed_x
79
+
80
+ constraints = register.all_constraints
81
+ expect(constraints.size).to eq(1)
82
+ variable, type, info = constraints.first
83
+ expect(type).to eq :send
84
+ expect(variable).to eq xvar
85
+ expect(info[:return]).to eq ret_type
86
+ expect(info[:args].size).to eq 1
87
+ expect(info[:args].first).to eq xvar
88
+ end
89
+
90
+ it 'creates a new register with the right subsitutions in the type_variable' do
91
+ TypedRb::Types::TypingContext.type_variables_register = described_class.new(nil,:top_level)
92
+ parent = TypedRb::Types::TypingContext.type_variables_register
93
+ ata = parent.type_variable_for(:instance_variable, '@a', String.ancestors)
94
+ argy = parent.type_variable_for_abstraction(:lambda, 'y', top_level_typing_context)
95
+
96
+ child = described_class.new(parent, :lambda)
97
+ TypedRb::Types::TypingContext.type_variables_register = child
98
+
99
+ argx = child.type_variable_for_abstraction(:lambda, 'x', top_level_typing_context)
100
+
101
+ argx.compatible?(ata)
102
+ argy.compatible?(argx)
103
+
104
+ substitution = tyvariable('substitution')
105
+ result = parent.send(:apply_type, nil, argx.variable => substitution)
106
+ constraints = result.all_constraints
107
+ var, type, info = constraints[0]
108
+ expect(var).to eq(argy)
109
+ expect(type).to eq(:lt)
110
+ expect(info).to eq(substitution)
111
+ var, type, info = constraints[1]
112
+ expect(var).to eq(substitution)
113
+ expect(type).to eq(:lt)
114
+ expect(info).to eq(ata)
115
+
116
+ TypedRb::Types::TypingContext.type_variables_register = parent
117
+ constraints = TypedRb::Types::TypingContext.all_constraints
118
+ var, type, info = constraints[0]
119
+ expect(var).to eq(argy)
120
+ expect(type).to eq(:lt)
121
+ expect(info).to eq(argx)
122
+ var, type, info = constraints[1]
123
+ expect(var).to eq(argx)
124
+ expect(type).to eq(:lt)
125
+ expect(info).to eq(ata)
126
+ end
127
+ end
128
+ end