steep 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/steep/parser.y CHANGED
@@ -667,6 +667,15 @@ rule
667
667
  type: val[2]
668
668
  )
669
669
  }
670
+ | kAT_TYPE tCOLON type
671
+ {
672
+ loc = val.first.location + val.last.location
673
+ result = AST::Signature::Members::Ivar.new(
674
+ location: loc,
675
+ name: :"@type",
676
+ type: val[2]
677
+ )
678
+ }
670
679
 
671
680
  instance_method_member: kDEF method_annotations method_name tCOLON method_type_union
672
681
  {
@@ -918,7 +927,7 @@ rule
918
927
  name: val[2].value,
919
928
  type: val[4])
920
929
  }
921
- | kAT_TYPE kMETHOD subject tCOLON method_type
930
+ | kAT_TYPE kMETHOD method_name tCOLON method_type
922
931
  {
923
932
  loc = val.first.location + val.last.location
924
933
  result = AST::Annotation::MethodType.new(location: loc,
@@ -1212,10 +1221,6 @@ def next_token
1212
1221
  new_token(:kNOCONSTRUCTOR, :noconstructor)
1213
1222
  when input.scan(/\$\w+\b/)
1214
1223
  new_token(:tGVAR, input.matched.to_sym)
1215
- when input.scan(/::([A-Z]\w*::)*[A-Z]\w*/)
1216
- new_token(:tQUALIFIED_MODULE_NAME, Names::Module.parse(input.matched))
1217
- when input.scan(/([A-Z]\w*::)+[A-Z]\w*/)
1218
- new_token(:tQUALIFIED_MODULE_NAME, Names::Module.parse(input.matched))
1219
1224
  when input.scan(/::([A-Z]\w*::)*_\w+/)
1220
1225
  new_token(:tQUALIFIED_INTERFACE_NAME, Names::Interface.parse(input.matched))
1221
1226
  when input.scan(/([A-Z]\w*::)+_\w+/)
@@ -1224,6 +1229,10 @@ def next_token
1224
1229
  new_token(:tQUALIFIED_ALIAS_NAME, Names::Alias.parse(input.matched))
1225
1230
  when input.scan(/([A-Z]\w*::)+[a-z]\w*/)
1226
1231
  new_token(:tQUALIFIED_ALIAS_NAME, Names::Alias.parse(input.matched))
1232
+ when input.scan(/::([A-Z]\w*::)*[A-Z]\w*/)
1233
+ new_token(:tQUALIFIED_MODULE_NAME, Names::Module.parse(input.matched))
1234
+ when input.scan(/([A-Z]\w*::)+[A-Z]\w*/)
1235
+ new_token(:tQUALIFIED_MODULE_NAME, Names::Module.parse(input.matched))
1227
1236
  when input.scan(/[A-Z]\w*/)
1228
1237
  new_token(:tUIDENT, input.matched.to_sym)
1229
1238
  when input.scan(/_\w+/)
@@ -147,23 +147,17 @@ module Steep
147
147
  results.find(&:failure?)
148
148
  end
149
149
 
150
- when relation.super_type.is_a?(AST::Types::Var)
151
- if constraints.unknown?(relation.super_type.name)
152
- constraints.add(relation.super_type.name, sub_type: relation.sub_type)
153
- success(constraints: constraints)
154
- else
155
- failure(error: Result::Failure::UnknownPairError.new(relation: relation),
156
- trace: trace)
157
- end
150
+ when relation.super_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.super_type.name)
151
+ constraints.add(relation.super_type.name, sub_type: relation.sub_type)
152
+ success(constraints: constraints)
158
153
 
159
- when relation.sub_type.is_a?(AST::Types::Var)
160
- if constraints.unknown?(relation.sub_type.name)
161
- constraints.add(relation.sub_type.name, super_type: relation.super_type)
162
- success(constraints: constraints)
163
- else
164
- failure(error: Result::Failure::UnknownPairError.new(relation: relation),
165
- trace: trace)
166
- end
154
+ when relation.sub_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.sub_type.name)
155
+ constraints.add(relation.sub_type.name, super_type: relation.super_type)
156
+ success(constraints: constraints)
157
+
158
+ when relation.super_type.is_a?(AST::Types::Var) || relation.sub_type.is_a?(AST::Types::Var)
159
+ failure(error: Result::Failure::UnknownPairError.new(relation: relation),
160
+ trace: trace)
167
161
 
168
162
  when relation.sub_type.is_a?(AST::Types::Name::Base) && relation.super_type.is_a?(AST::Types::Name::Base)
169
163
  if (pairs = extract_nominal_pairs(relation))
@@ -1129,10 +1129,19 @@ module Steep
1129
1129
  when :array
1130
1130
  yield_self do
1131
1131
  if node.children.empty?
1132
- unless hint
1133
- typing.add_error Errors::FallbackAny.new(node: node)
1134
- end
1135
- typing.add_typing(node, AST::Builtin::Array.instance_type(AST::Builtin.any_type))
1132
+ typing.add_error Errors::FallbackAny.new(node: node) unless hint
1133
+
1134
+ array_type = if hint
1135
+ relation = Subtyping::Relation.new(
1136
+ sub_type: AST::Builtin::Array.instance_type(AST::Builtin.any_type),
1137
+ super_type: hint
1138
+ )
1139
+ if checker.check(relation, constraints: Subtyping::Constraints.empty).success?
1140
+ hint
1141
+ end
1142
+ end
1143
+
1144
+ typing.add_typing(node, array_type || AST::Builtin::Array.instance_type(AST::Builtin.any_type))
1136
1145
  else
1137
1146
  is_tuple = nil
1138
1147
 
@@ -1749,46 +1758,49 @@ module Steep
1749
1758
  receiver_type = unwrap(receiver_type)
1750
1759
  end
1751
1760
 
1752
- case receiver_type
1753
- when AST::Types::Any
1754
- typing.add_typing node, AST::Builtin.any_type
1761
+ return_type = case receiver_type
1762
+ when AST::Types::Any
1763
+ typing.add_typing node, AST::Builtin.any_type
1755
1764
 
1756
- when nil
1757
- fallback_to_any node
1765
+ when nil
1766
+ fallback_to_any node
1758
1767
 
1759
- else
1760
- begin
1761
- interface = checker.resolve(receiver_type)
1762
-
1763
- method = interface.methods[method_name]
1764
-
1765
- if method
1766
- args = TypeInference::SendArgs.from_nodes(arguments)
1767
- return_type_or_error = type_method_call(node,
1768
- method: method,
1769
- args: args,
1770
- block_params: block_params,
1771
- block_body: block_body,
1772
- receiver_type: receiver_type)
1773
-
1774
- if return_type_or_error.is_a?(Errors::Base)
1775
- fallback_to_any node do
1776
- return_type_or_error
1777
- end
1778
- else
1779
- typing.add_typing node, return_type_or_error
1780
- end
1781
- else
1782
- fallback_to_any node do
1783
- Errors::NoMethod.new(node: node, method: method_name, type: receiver_type)
1784
- end
1785
- end
1786
- rescue Subtyping::Check::CannotResolveError
1787
- fallback_to_any node do
1788
- Errors::NoMethod.new(node: node, method: method_name, type: receiver_type)
1789
- end
1790
- end
1791
- end.tap do
1768
+ else
1769
+ begin
1770
+ interface = checker.resolve(receiver_type)
1771
+
1772
+ method = interface.methods[method_name]
1773
+
1774
+ if method
1775
+ args = TypeInference::SendArgs.from_nodes(arguments)
1776
+ return_type_or_error = type_method_call(node,
1777
+ method: method,
1778
+ args: args,
1779
+ block_params: block_params,
1780
+ block_body: block_body,
1781
+ receiver_type: receiver_type)
1782
+
1783
+ if return_type_or_error.is_a?(Errors::Base)
1784
+ fallback_to_any node do
1785
+ return_type_or_error
1786
+ end
1787
+ else
1788
+ typing.add_typing node, return_type_or_error
1789
+ end
1790
+ else
1791
+ fallback_to_any node do
1792
+ Errors::NoMethod.new(node: node, method: method_name, type: receiver_type)
1793
+ end
1794
+ end
1795
+ rescue Subtyping::Check::CannotResolveError
1796
+ fallback_to_any node do
1797
+ Errors::NoMethod.new(node: node, method: method_name, type: receiver_type)
1798
+ end
1799
+ end
1800
+ end
1801
+
1802
+ case return_type
1803
+ when nil, Errors::Base
1792
1804
  arguments.each do |arg|
1793
1805
  unless typing.has_type?(arg)
1794
1806
  if arg.type == :splat
@@ -1814,6 +1826,8 @@ module Steep
1814
1826
  for_block.synthesize(block_body)
1815
1827
  end
1816
1828
  end
1829
+ else
1830
+ return_type
1817
1831
  end
1818
1832
  end
1819
1833
 
@@ -1932,6 +1946,8 @@ module Steep
1932
1946
  occurence = Subtyping::VariableOccurence.from_method_type(method_type)
1933
1947
 
1934
1948
  arg_pairs.each do |(arg_node, param_type)|
1949
+ param_type = param_type.subst(instantiation)
1950
+
1935
1951
  arg_type = if arg_node.type == :splat
1936
1952
  type = construction.synthesize(arg_node.children[0])
1937
1953
  child_typing.add_typing(arg_node, type)
@@ -1941,7 +1957,7 @@ module Steep
1941
1957
 
1942
1958
  relation = Subtyping::Relation.new(
1943
1959
  sub_type: arg_type,
1944
- super_type: param_type.subst(instantiation)
1960
+ super_type: param_type
1945
1961
  )
1946
1962
 
1947
1963
  checker.check(relation, constraints: constraints).else do |result|
@@ -2167,123 +2183,6 @@ module Steep
2167
2183
  end
2168
2184
  end
2169
2185
 
2170
- def test_args(params:, arguments:)
2171
- params.each_missing_argument arguments do |_|
2172
- return nil
2173
- end
2174
-
2175
- params.each_extra_argument arguments do |_|
2176
- return nil
2177
- end
2178
-
2179
- params.each_missing_keyword arguments do |_|
2180
- return nil
2181
- end
2182
-
2183
- params.each_extra_keyword arguments do |_|
2184
- return nil
2185
- end
2186
-
2187
- self.class.argument_typing_pairs(params: params, arguments: arguments.dup)
2188
- end
2189
-
2190
- def applicable_args?(params:, arguments:)
2191
- params.each_missing_argument arguments do |_|
2192
- return false
2193
- end
2194
-
2195
- params.each_extra_argument arguments do |_|
2196
- return false
2197
- end
2198
-
2199
- params.each_missing_keyword arguments do |_|
2200
- return false
2201
- end
2202
-
2203
- params.each_extra_keyword arguments do |_|
2204
- return false
2205
- end
2206
-
2207
- all_args = arguments.dup
2208
-
2209
- self.class.argument_typing_pairs(params: params, arguments: arguments.dup).each do |(param_type, argument)|
2210
- all_args.delete_if {|a| a.equal?(argument) }
2211
-
2212
- check(argument, param_type) do |_, _|
2213
- return false
2214
- end
2215
- end
2216
-
2217
- all_args.each do |arg|
2218
- synthesize(arg)
2219
- end
2220
-
2221
- true
2222
- end
2223
-
2224
- def self.block_param_typing_pairs(param_types: , param_nodes:)
2225
- pairs = []
2226
-
2227
- param_types.required.each.with_index do |type, index|
2228
- if (param = param_nodes[index])
2229
- pairs << [param, type]
2230
- end
2231
- end
2232
-
2233
- pairs
2234
- end
2235
-
2236
- def self.argument_typing_pairs(params:, arguments:)
2237
- keywords = {}
2238
- unless params.required_keywords.empty? && params.optional_keywords.empty? && !params.rest_keywords
2239
- # has keyword args
2240
- last_arg = arguments.last
2241
- if last_arg&.type == :hash
2242
- arguments.pop
2243
-
2244
- last_arg.children.each do |elem|
2245
- case elem.type
2246
- when :pair
2247
- key, value = elem.children
2248
- if key.type == :sym
2249
- name = key.children[0]
2250
-
2251
- keywords[name] = value
2252
- end
2253
- end
2254
- end
2255
- end
2256
- end
2257
-
2258
- pairs = []
2259
-
2260
- params.flat_unnamed_params.each do |param_type|
2261
- arg = arguments.shift
2262
- pairs << [param_type.last, arg] if arg
2263
- end
2264
-
2265
- if params.rest
2266
- arguments.each do |arg|
2267
- pairs << [params.rest, arg]
2268
- end
2269
- end
2270
-
2271
- params.flat_keywords.each do |name, type|
2272
- arg = keywords.delete(name)
2273
- if arg
2274
- pairs << [type, arg]
2275
- end
2276
- end
2277
-
2278
- if params.rest_keywords
2279
- keywords.each_value do |arg|
2280
- pairs << [params.rest_keywords, arg]
2281
- end
2282
- end
2283
-
2284
- pairs
2285
- end
2286
-
2287
2186
  def self.parameter_types(nodes, type)
2288
2187
  nodes = nodes.dup
2289
2188
 
data/lib/steep/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Steep
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
data/stdlib/builtin.rbi CHANGED
@@ -26,6 +26,10 @@ class Object < BasicObject
26
26
  def !: -> bool
27
27
  def Array: (any) -> Array<any>
28
28
  def Hash: (any) -> Hash<any, any>
29
+ def instance_eval: <'x> { (self) -> 'x } -> 'x
30
+ | (String, ?String, ?Integer) -> any
31
+ def define_singleton_method: (Symbol | String, any) -> Symbol
32
+ | (Symbol) { (*any) -> any } -> Symbol
29
33
  end
30
34
 
31
35
  class Module
@@ -234,6 +238,7 @@ class Hash<'key, 'value>
234
238
  | -> Enumerator<['key, 'value], self>
235
239
  def key?: ('key) -> bool
236
240
  def merge: (Hash<'key, 'value>) -> Hash<'key, 'value>
241
+ def delete: ('key) -> 'value?
237
242
 
238
243
  include Enumerable<['key, 'value], self>
239
244
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-22 00:00:00.000000000 Z
11
+ date: 2018-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler