Class: Steep::TypeConstruction
- Inherits:
-
Object
- Object
- Steep::TypeConstruction
- Defined in:
- lib/steep/type_construction.rb
Defined Under Namespace
Classes: BlockContext, BreakContext, MethodContext, ModuleContext
Constant Summary collapse
- NOTHING =
::Object.new
Instance Attribute Summary collapse
-
#annotations ⇒ Object
readonly
Returns the value of attribute annotations.
-
#block_context ⇒ Object
readonly
Returns the value of attribute block_context.
-
#break_context ⇒ Object
readonly
Returns the value of attribute break_context.
-
#checker ⇒ Object
readonly
Returns the value of attribute checker.
-
#method_context ⇒ Object
readonly
Returns the value of attribute method_context.
-
#module_context ⇒ Object
readonly
Returns the value of attribute module_context.
-
#self_type ⇒ Object
readonly
Returns the value of attribute self_type.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#type_env ⇒ Object
readonly
Returns the value of attribute type_env.
-
#typing ⇒ Object
readonly
Returns the value of attribute typing.
Class Method Summary collapse
- .parameter_types(nodes, type) ⇒ Object
- .truthy_variables(node) ⇒ Object
- .valid_parameter_env?(env, nodes, params) ⇒ Boolean
- .value_variables(node) ⇒ Object
Instance Method Summary collapse
- #absolute_name(module_name) ⇒ Object
- #absolute_type(type) ⇒ Object
- #assign_type_to_variable(var, type, node) ⇒ Object
- #check(node, type, constraints: Subtyping::Constraints.empty) ⇒ Object
- #check_keyword_arg(receiver_type:, node:, method_type:, constraints:) ⇒ Object
- #current_namespace ⇒ Object
- #each_child_node(node) ⇒ Object
- #expand_alias(type) ⇒ Object
- #fallback_any_rec(node) ⇒ Object
- #fallback_to_any(node) ⇒ Object
- #flatten_const_name(node) ⇒ Object
- #for_block(block_annotations:, param_pairs:, method_return_type:, typing:) ⇒ Object
- #for_branch(node, truthy_vars: Set.new, type_case_override: nil) ⇒ Object
- #for_class(node) ⇒ Object
- #for_module(node) ⇒ Object
- #for_new_method(method_name, node, args:, self_type:) ⇒ Object
-
#initialize(checker:, source:, annotations:, type_env:, typing:, self_type:, method_context:, block_context:, module_context:, break_context:) ⇒ TypeConstruction
constructor
A new instance of TypeConstruction.
- #namespace_module?(node) ⇒ Boolean
- #nested_namespace_for_module(module_name) ⇒ Object
- #select_super_type(sub_type, super_type) ⇒ Object
- #self_class?(node) ⇒ Boolean
- #synthesize(node, hint: nil) ⇒ Object
- #test_literal_type(literal, hint) ⇒ Object
- #to_instance_type(type, args: nil) ⇒ Object
- #try_hash_type(node, hint) ⇒ Object
- #try_method_type(node, receiver_type:, method_type:, args:, arg_pairs:, block_params:, block_body:, child_typing:) ⇒ Object
- #type_assignment(var, rhs, node, hint: nil) ⇒ Object
- #type_block(block_param_hint:, block_type_hint:, node_type_hint:, block_params:, block_body:, block_annotations:) ⇒ Object
- #type_ivasgn(name, rhs, node) ⇒ Object
- #type_lambda(node, block_params:, block_body:, type_hint:) ⇒ Object
- #type_masgn(node) ⇒ Object
- #type_method_call(node, receiver_type:, method:, args:, block_params:, block_body:) ⇒ Object
- #type_send(node, send_node:, block_params:, block_body:, unwrap: false) ⇒ Object
- #union_type(*types) ⇒ Object
- #unwrap(type) ⇒ Object
- #validate_method_definitions(node, module_name) ⇒ Object
- #with(annotations: NOTHING, type_env: NOTHING, method_context: NOTHING, block_context: NOTHING, module_context: NOTHING, self_type: NOTHING, break_context: NOTHING) ⇒ Object
- #with_new_typing(typing) ⇒ Object
Constructor Details
#initialize(checker:, source:, annotations:, type_env:, typing:, self_type:, method_context:, block_context:, module_context:, break_context:) ⇒ TypeConstruction
Returns a new instance of TypeConstruction.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/steep/type_construction.rb', line 80 def initialize(checker:, source:, annotations:, type_env:, typing:, self_type:, method_context:, block_context:, module_context:, break_context:) @checker = checker @source = source @annotations = annotations @typing = typing @self_type = self_type @block_context = block_context @method_context = method_context @module_context = module_context @break_context = break_context @type_env = type_env end |
Instance Attribute Details
#annotations ⇒ Object (readonly)
Returns the value of attribute annotations.
71 72 73 |
# File 'lib/steep/type_construction.rb', line 71 def annotations @annotations end |
#block_context ⇒ Object (readonly)
Returns the value of attribute block_context.
74 75 76 |
# File 'lib/steep/type_construction.rb', line 74 def block_context @block_context end |
#break_context ⇒ Object (readonly)
Returns the value of attribute break_context.
77 78 79 |
# File 'lib/steep/type_construction.rb', line 77 def break_context @break_context end |
#checker ⇒ Object (readonly)
Returns the value of attribute checker.
69 70 71 |
# File 'lib/steep/type_construction.rb', line 69 def checker @checker end |
#method_context ⇒ Object (readonly)
Returns the value of attribute method_context.
73 74 75 |
# File 'lib/steep/type_construction.rb', line 73 def method_context @method_context end |
#module_context ⇒ Object (readonly)
Returns the value of attribute module_context.
75 76 77 |
# File 'lib/steep/type_construction.rb', line 75 def module_context @module_context end |
#self_type ⇒ Object (readonly)
Returns the value of attribute self_type.
76 77 78 |
# File 'lib/steep/type_construction.rb', line 76 def self_type @self_type end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
70 71 72 |
# File 'lib/steep/type_construction.rb', line 70 def source @source end |
#type_env ⇒ Object (readonly)
Returns the value of attribute type_env.
78 79 80 |
# File 'lib/steep/type_construction.rb', line 78 def type_env @type_env end |
#typing ⇒ Object (readonly)
Returns the value of attribute typing.
72 73 74 |
# File 'lib/steep/type_construction.rb', line 72 def typing @typing end |
Class Method Details
.parameter_types(nodes, type) ⇒ Object
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 |
# File 'lib/steep/type_construction.rb', line 2346 def self.parameter_types(nodes, type) nodes = nodes.dup env = {} type.params.required.each do |type| a = nodes.first if a&.type == :arg env[a.children.first] = type nodes.shift else break end end type.params.optional.each do |type| a = nodes.first if a&.type == :optarg env[a.children.first] = type nodes.shift else break end end if type.params.rest a = nodes.first if a&.type == :restarg env[a.children.first] = AST::Builtin::Array.instance_type(type.params.rest) nodes.shift end end nodes.each do |node| if node.type == :kwarg name = node.children[0] ty = type.params.required_keywords[name.name] env[name] = ty if ty end if node.type == :kwoptarg name = node.children[0] ty = type.params.optional_keywords[name.name] env[name] = ty if ty end if node.type == :kwrestarg ty = type.params.rest_keywords if ty env[node.children[0]] = AST::Builtin::Hash.instance_type(AST::Builtin::Symbol.instance_type, ty) end end end env end |
.truthy_variables(node) ⇒ Object
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 |
# File 'lib/steep/type_construction.rb', line 2573 def self.truthy_variables(node) case node&.type when :lvar Set.new([node.children.first.name]) when :lvasgn Set.new([node.children.first.name]) + truthy_variables(node.children[1]) when :and truthy_variables(node.children[0]) + truthy_variables(node.children[1]) when :begin truthy_variables(node.children.last) else Set.new() end end |
.valid_parameter_env?(env, nodes, params) ⇒ Boolean
2404 2405 2406 |
# File 'lib/steep/type_construction.rb', line 2404 def self.valid_parameter_env?(env, nodes, params) env.size == nodes.size && env.size == params.size end |
.value_variables(node) ⇒ Object
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 |
# File 'lib/steep/type_construction.rb', line 2588 def self.value_variables(node) case node&.type when :lvar Set.new([node.children.first.name]) when :lvasgn Set.new([node.children.first.name]) + value_variables(node.children[1]) when :begin value_variables(node.children.last) else Set.new end end |
Instance Method Details
#absolute_name(module_name) ⇒ Object
2420 2421 2422 2423 2424 2425 2426 |
# File 'lib/steep/type_construction.rb', line 2420 def absolute_name(module_name) if current_namespace module_name.in_namespace(current_namespace) else module_name.absolute! end end |
#absolute_type(type) ⇒ Object
2428 2429 2430 2431 2432 |
# File 'lib/steep/type_construction.rb', line 2428 def absolute_type(type) if type checker.builder.absolute_type(type, current: current_namespace) end end |
#assign_type_to_variable(var, type, node) ⇒ Object
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 |
# File 'lib/steep/type_construction.rb', line 1589 def assign_type_to_variable(var, type, node) name = var.name type_env.assign(lvar: name, type: type) do |result| var_type = type_env.get(lvar: name) typing.add_error(Errors::IncompatibleAssignment.new(node: node, lhs_type: var_type, rhs_type: type, result: result)) end end |
#check(node, type, constraints: Subtyping::Constraints.empty) ⇒ Object
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 |
# File 'lib/steep/type_construction.rb', line 1558 def check(node, type, constraints: Subtyping::Constraints.empty) type_ = synthesize(node, hint: type) result = checker.check( Subtyping::Relation.new(sub_type: type_, super_type: type), constraints: constraints ) if result.failure? yield(type, type_, result) end end |
#check_keyword_arg(receiver_type:, node:, method_type:, constraints:) ⇒ Object
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 |
# File 'lib/steep/type_construction.rb', line 1937 def check_keyword_arg(receiver_type:, node:, method_type:, constraints:) params = method_type.params case node.type when :hash keyword_hash_type = AST::Builtin::Hash.instance_type(AST::Builtin::Symbol.instance_type, AST::Builtin.any_type) typing.add_typing node, keyword_hash_type given_keys = Set.new() node.children.each do |element| case element.type when :pair key_node, value_node = element.children case key_node.type when :sym key_symbol = key_node.children[0] keyword_type = case when params.required_keywords.key?(key_symbol) params.required_keywords[key_symbol] when params.optional_keywords.key?(key_symbol) AST::Types::Union.build( types: [params.optional_keywords[key_symbol], AST::Builtin.nil_type] ) when params.rest_keywords params.rest_keywords end typing.add_typing key_node, AST::Builtin::Symbol.instance_type given_keys << key_symbol if keyword_type check(value_node, keyword_type, constraints: constraints) do |expected, actual, result| return Errors::IncompatibleAssignment.new( node: value_node, lhs_type: expected, rhs_type: actual, result: result ) end else synthesize(value_node) end else check(key_node, AST::Builtin::Symbol.instance_type, constraints: constraints) do |expected, actual, result| return Errors::IncompatibleAssignment.new( node: key_node, lhs_type: expected, rhs_type: actual, result: result ) end end when :kwsplat Steep.logger.warn("Keyword arg with kwsplat(**) node are not supported.") check(element.children[0], keyword_hash_type, constraints: constraints) do |expected, actual, result| return Errors::IncompatibleAssignment.new( node: node, lhs_type: expected, rhs_type: actual, result: result ) end given_keys = true end end case given_keys when Set missing_keywords = Set.new(params.required_keywords.keys) - given_keys unless missing_keywords.empty? return Errors::MissingKeyword.new(node: node, missing_keywords: missing_keywords) end extra_keywords = given_keys - Set.new(params.required_keywords.keys) - Set.new(params.optional_keywords.keys) if extra_keywords.any? && !params.rest_keywords return Errors::UnexpectedKeyword.new(node: node, unexpected_keywords: extra_keywords) end end else if params.rest_keywords Steep.logger.warn("Method call with rest keywords type is detected. Rough approximation to be improved.") value_types = params.required_keywords.values + params.optional_keywords.values.map {|type| AST::Types::Union.build(types: [type, AST::Builtin.nil_type]) } + [params.rest_keywords] hash_type = AST::Builtin::Hash.instance_type( AST::Builtin::Symbol.instance_type, AST::Types::Union.build(types: value_types, location: method_type.location) ) else hash_elements = params.required_keywords.merge( method_type.optional_keywords.transform_values do |type| AST::Types::Union.build(types: [type, AST::Builtin.nil_type], location: method_type.location) end ) hash_type = AST::Types::Record.new(elements: hash_elements) end node_type = synthesize(node, hint: hash_type) relation = Subtyping::Relation.new( sub_type: node_type, super_type: hash_type ) checker.check(relation, constraints: constraints).else do return Errors::ArgumentTypeMismatch.new( node: node, receiver_type: receiver_type, expected: relation.super_type, actual: relation.sub_type ) end end nil end |
#current_namespace ⇒ Object
2408 2409 2410 |
# File 'lib/steep/type_construction.rb', line 2408 def current_namespace module_context&.current_namespace || AST::Namespace.root end |
#each_child_node(node) ⇒ Object
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 |
# File 'lib/steep/type_construction.rb', line 2334 def each_child_node(node) if block_given? node.children.each do |child| if child.is_a?(::AST::Node) yield child end end else enum_for :each_child_node, node end end |
#expand_alias(type) ⇒ Object
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 |
# File 'lib/steep/type_construction.rb', line 2601 def (type) if type = checker.(type) end if block_given? yield , type else end end |
#fallback_any_rec(node) ⇒ Object
2551 2552 2553 2554 2555 2556 2557 2558 2559 |
# File 'lib/steep/type_construction.rb', line 2551 def fallback_any_rec(node) fallback_to_any(node) unless typing.has_type?(node) each_child_node(node) do |child| fallback_any_rec(child) end typing.type_of(node: node) end |
#fallback_to_any(node) ⇒ Object
2520 2521 2522 2523 2524 2525 2526 2527 2528 |
# File 'lib/steep/type_construction.rb', line 2520 def fallback_to_any(node) if block_given? typing.add_error yield else typing.add_error Errors::FallbackAny.new(node: node) end typing.add_typing node, AST::Builtin.any_type end |
#flatten_const_name(node) ⇒ Object
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 |
# File 'lib/steep/type_construction.rb', line 2501 def flatten_const_name(node) path = [] while node case node.type when :const, :casgn path.unshift(node.children[1]) node = node.children[0] when :cbase path.unshift("") break else return nil end end path.join("::").to_sym end |
#for_block(block_annotations:, param_pairs:, method_return_type:, typing:) ⇒ Object
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 |
# File 'lib/steep/type_construction.rb', line 1834 def for_block(block_annotations:, param_pairs:, method_return_type:, typing:) block_type_env = type_env.dup.yield_self do |env| param_pairs.each do |param, type| if param.type env.set(lvar: param.var.name, type: param.type) else env.set(lvar: param.var.name, type: type) end end env.with_annotations( lvar_types: block_annotations.lvar_types, ivar_types: block_annotations.ivar_types, const_types: block_annotations.const_types, ) end return_type = if block_annotations.break_type union_type(method_return_type, block_annotations.break_type) else method_return_type end Steep.logger.debug("return_type = #{return_type}") block_context = BlockContext.new(body_type: block_annotations.block_type) Steep.logger.debug("block_context { body_type: #{block_context.body_type} }") break_context = BreakContext.new( break_type: block_annotations.break_type || method_return_type, next_type: block_annotations.block_type ) Steep.logger.debug("break_context { type: #{break_context.break_type} }") [self.class.new( checker: checker, source: source, annotations: annotations.merge_block_annotations(block_annotations), type_env: block_type_env, block_context: block_context, typing: typing, method_context: method_context, module_context: module_context, self_type: block_annotations.self_type || self_type, break_context: break_context ), return_type] end |
#for_branch(node, truthy_vars: Set.new, type_case_override: nil) ⇒ Object
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/steep/type_construction.rb', line 443 def for_branch(node, truthy_vars: Set.new, type_case_override: nil) annots = source.annotations(block: node, builder: checker.builder, current_module: current_namespace) type_env = self.type_env lvar_types = self.type_env.lvar_types.each.with_object({}) do |(var, type), env| if truthy_vars.member?(var) env[var] = unwrap(type) else env[var] = type end end type_env = type_env.with_annotations(lvar_types: lvar_types) do |var, relation, result| raise "Unexpected annotate failure: #{relation}" end if type_case_override type_env = type_env.with_annotations(lvar_types: type_case_override) do |var, relation, result| typing.add_error( Errors::IncompatibleTypeCase.new(node: node, var_name: var, relation: relation, result: result) ) end end type_env = type_env.with_annotations( lvar_types: annots.lvar_types, ivar_types: annots.ivar_types, const_types: annots.const_types, gvar_types: {} ) do |var, relation, result| typing.add_error( Errors::IncompatibleAnnotation.new(node: node, var_name: var, relation: relation, result: result) ) end with(type_env: type_env) end |
#for_class(node) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/steep/type_construction.rb', line 369 def for_class(node) new_class_name = Names::Module.from_node(node.children.first) or raise "Unexpected class name: #{node.children.first}" new_namespace = nested_namespace_for_module(new_class_name) annots = source.annotations(block: node, builder: checker.builder, current_module: new_namespace) implement_module_name = yield_self do if (annotation = annots.implement_module_annotation) absolute_name(annotation.name.name).yield_self do |absolute_name| if checker.builder.signatures.class_name?(absolute_name) AST::Annotation::Implements::Module.new(name: absolute_name, args: annotation.name.args) else Steep.logger.error "Unknown class name given to @implements: #{annotation.name.name}" nil end end else absolute_name(new_class_name).yield_self do |absolute_name| if checker.builder.signatures.class_name?(absolute_name) signature = checker.builder.signatures.find_class(absolute_name) AST::Annotation::Implements::Module.new(name: absolute_name, args: signature.params&.variables || []) end end end end if implement_module_name class_name = implement_module_name.name class_args = implement_module_name.args.map {|x| AST::Types::Var.new(name: x) } _ = checker.builder.build_instance(class_name, with_initialize: true) instance_type = AST::Types::Name::Instance.new(name: class_name, args: class_args) module_type = AST::Types::Name::Class.new(name: class_name, constructor: true) end const_context = if new_namespace.empty? nil else Names::Module.new(name: new_namespace.path.last, namespace: new_namespace.parent) end class_const_env = TypeInference::ConstantEnv.new(builder: checker.builder, context: const_context) module_context = ModuleContext.new( instance_type: annots.instance_type || instance_type, module_type: annots.self_type || annots.module_type || module_type, implement_name: implement_module_name, current_namespace: new_namespace, const_env: class_const_env ) class_type_env = TypeInference::TypeEnv.build( annotations: annots, const_env: class_const_env, signatures: checker.builder.signatures, subtyping: checker ) self.class.new( checker: checker, source: source, annotations: annots, type_env: class_type_env, typing: typing, method_context: nil, block_context: nil, module_context: module_context, self_type: module_context.module_type, break_context: nil ) end |
#for_module(node) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/steep/type_construction.rb', line 278 def for_module(node) new_module_name = Names::Module.from_node(node.children.first) or raise "Unexpected module name: #{node.children.first}" new_namespace = nested_namespace_for_module(new_module_name) annots = source.annotations(block: node, builder: checker.builder, current_module: new_namespace) module_type = AST::Builtin::Module.instance_type implement_module_name = yield_self do if (annotation = annots.implement_module_annotation) absolute_name(annotation.name.name).yield_self do |absolute_name| if checker.builder.signatures.module_name?(absolute_name) AST::Annotation::Implements::Module.new(name: absolute_name, args: annotation.name.args) else Steep.logger.error "Unknown module name given to @implements: #{annotation.name.name}" nil end end else absolute_name(new_module_name).yield_self do |absolute_name| if checker.builder.signatures.module_name?(absolute_name) signature = checker.builder.signatures.find_module(absolute_name) AST::Annotation::Implements::Module.new(name: absolute_name, args: signature.params&.variables || []) end end end end if implement_module_name module_name = implement_module_name.name module_args = implement_module_name.args.map {|x| AST::Types::Var.new(name: x) } abstract = checker.builder.build_module(module_name) instance_type = AST::Types::Name::Instance.new(name: module_name, args: module_args) unless abstract.supers.empty? instance_type = AST::Types::Intersection.build( types: [instance_type, AST::Builtin::Object.instance_type] + abstract.supers.map {|x| absolute_type(x) } ) end module_type = AST::Types::Intersection.build(types: [ AST::Builtin::Module.instance_type, AST::Types::Name::Module.new(name: module_name) ]) end if annots.instance_type instance_type = annots.instance_type end if annots.module_type module_type = annots.module_type end const_context = if new_namespace.empty? nil else Names::Module.new(name: new_namespace.path.last, namespace: new_namespace.parent) end module_const_env = TypeInference::ConstantEnv.new(builder: checker.builder, context: const_context) module_context_ = ModuleContext.new( instance_type: instance_type, module_type: annots.self_type || module_type, implement_name: implement_module_name, current_namespace: new_namespace, const_env: module_const_env ) module_type_env = TypeInference::TypeEnv.build(annotations: annots, subtyping: checker, const_env: module_const_env, signatures: checker.builder.signatures) self.class.new( checker: checker, source: source, annotations: annots, type_env: module_type_env, typing: typing, method_context: nil, block_context: nil, module_context: module_context_, self_type: module_context_.module_type, break_context: nil ) end |
#for_new_method(method_name, node, args:, self_type:) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/steep/type_construction.rb', line 108 def for_new_method(method_name, node, args:, self_type:) annots = source.annotations(block: node, builder: checker.builder, current_module: current_namespace) type_env = TypeInference::TypeEnv.new(subtyping: checker, const_env: module_context&.const_env || self.type_env.const_env) self.type_env.const_types.each do |name, type| type_env.set(const: name, type: type) end self_type = (annots.self_type || self_type) self_interface = self_type && (self_type != AST::Builtin.any_type || nil) && case self_type when AST::Types::Name::Instance yield_self do class_type = AST::Types::Name::Class.new(name: self_type.name, constructor: false) checker.resolve_instance(self_type, self_type: self_type, instance_type: self_type, module_type: class_type, with_initialize: true) end else checker.resolve(self_type) end interface_method = self_interface&.yield_self do |interface| interface.methods[method_name]&.yield_self do |method| if self_type.is_a?(AST::Types::Name::Base) && method.type_name == self_type.name method else Interface::Method.new(type_name: self_type, name: method_name, types: method.types, super_method: method, attributes: []) end end end annotation_method = annotations.method_type(method_name)&.yield_self do |method_type| subst = Interface::Substitution.build([], instance_type: module_context&.instance_type || AST::Types::Instance.new, module_type: module_context&.module_type || AST::Types::Class.new, self_type: self_type) Interface::Method.new(type_name: nil, name: method_name, types: [method_type.subst(subst)], super_method: interface_method&.super_method, attributes: []) end if interface_method && annotation_method interface_types = interface_method.types.map do |method_type| subst = Interface::Substitution.build(method_type.type_params) method_type.instantiate(subst) end unknowns = [] annotation_types = annotation_method.types.each.with_object([]) do |method_type, array| fresh = method_type.type_params.map {|var| AST::Types::Var.fresh(var) } unknowns.push(*fresh) subst = Interface::Substitution.build(method_type.type_params, fresh) array << method_type.instantiate(subst) end constraints = Subtyping::Constraints.new(unknowns: unknowns) interface_types.each do |type| constraints.add_var(*type.free_variables.to_a) end result = checker.check_method(method_name, annotation_method.with_types(annotation_types), interface_method.with_types(interface_types), assumption: Set.new, trace: Subtyping::Trace.new, constraints: constraints) if result.failure? typing.add_error Errors::IncompatibleMethodTypeAnnotation.new( node: node, annotation_method: annotation_method, interface_method: interface_method, result: result ) end end method = annotation_method || interface_method case when method && method.types.size == 1 method_type = method.types.first return_type = method_type.return_type var_types = TypeConstruction.parameter_types(args, method_type) unless TypeConstruction.valid_parameter_env?(var_types, args.reject {|arg| arg.type == :blockarg }, method_type.params) typing.add_error Errors::MethodArityMismatch.new(node: node) end if (block_arg = args.find {|arg| arg.type == :blockarg }) if method_type.block block_type = if method_type.block.optional? AST::Types::Union.build(types: [method_type.block.type, AST::Builtin.nil_type]) else method_type.block.type end var_types[block_arg.children[0].name] = block_type end end when method typing.add_error Errors::MethodDefinitionWithOverloading.new(node: node, method: method) return_type = union_type(*method.types.map(&:return_type)) var_types = {} else var_types = {} end if annots.return_type && return_type return_type_relation = Subtyping::Relation.new(sub_type: annots.return_type, super_type: return_type) checker.check(return_type_relation, constraints: Subtyping::Constraints.empty).else do |result| typing.add_error Errors::MethodReturnTypeAnnotationMismatch.new(node: node, method_type: return_type, annotation_type: annots.return_type, result: result) end end constructor_method = method&.attributes&.include?(:constructor) method_context = MethodContext.new( name: method_name, method: method, method_type: method_type, return_type: annots.return_type || return_type, constructor: constructor_method ) var_types.each do |name, type| type_env.set(lvar: name, type: type) end ivar_types = {} ivar_types.merge!(self_interface.ivars) if self_interface ivar_types.merge!(annots.ivar_types) ivar_types.each do |name, type| type_env.set(ivar: name, type: type) end type_env = type_env.with_annotations( lvar_types: annots.lvar_types, ivar_types: annots.ivar_types, const_types: annots.const_types, ) self.class.new( checker: checker, source: source, annotations: annots, type_env: type_env, block_context: nil, self_type: self_type, method_context: method_context, typing: typing, module_context: module_context, break_context: nil ) end |
#namespace_module?(node) ⇒ Boolean
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 |
# File 'lib/steep/type_construction.rb', line 2534 def namespace_module?(node) nodes = case node.type when :class, :module node.children.last&.yield_self {|child| if child.type == :begin child.children else [child] end } || [] else return false end !nodes.empty? && nodes.all? {|child| child.type == :class || child.type == :module } end |
#nested_namespace_for_module(module_name) ⇒ Object
2412 2413 2414 2415 2416 2417 2418 |
# File 'lib/steep/type_construction.rb', line 2412 def nested_namespace_for_module(module_name) if module_name.relative? && module_name.namespace.empty? current_namespace.append(module_name.name) else current_namespace end end |
#select_super_type(sub_type, super_type) ⇒ Object
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 |
# File 'lib/steep/type_construction.rb', line 2626 def select_super_type(sub_type, super_type) if super_type result = checker.check( Subtyping::Relation.new(sub_type: sub_type, super_type: super_type), constraints: Subtyping::Constraints.empty ) if result.success? super_type else if block_given? yield result else sub_type end end else sub_type end end |
#self_class?(node) ⇒ Boolean
2530 2531 2532 |
# File 'lib/steep/type_construction.rb', line 2530 def self_class?(node) node.type == :send && node.children[0]&.type == :self && node.children[1] == :class end |
#synthesize(node, hint: nil) ⇒ Object
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 |
# File 'lib/steep/type_construction.rb', line 504 def synthesize(node, hint: nil) Steep.logger.tagged "synthesize:(#{node.location.expression.to_s.split(/:/, 2).last})" do Steep.logger.debug node.type case node.type when :begin, :kwbegin yield_self do *mid_nodes, last_node = each_child_node(node).to_a mid_nodes.each do |child| synthesize(child) end if last_node type = synthesize(last_node, hint: hint) else type = AST::Builtin.nil_type end typing.add_typing(node, type) end when :lvasgn yield_self do var = node.children[0] rhs = node.children[1] if var.name == :_ synthesize(rhs, hint: AST::Builtin.any_type) typing.add_typing(node, AST::Builtin.any_type) else type_assignment(var, rhs, node, hint: hint) end end when :lvar yield_self do var = node.children[0] type = type_env.get(lvar: var.name) do fallback_to_any node end typing.add_typing node, type end when :ivasgn name = node.children[0] value = node.children[1] type_ivasgn(name, value, node) when :ivar yield_self do name = node.children[0] type = type_env.get(ivar: name) do fallback_to_any node end typing.add_typing(node, type) end when :send yield_self do if self_class?(node) module_type = (module_context.module_type) type = if module_type.is_a?(AST::Types::Name::Class) AST::Types::Name::Class.new(name: module_type.name, constructor: method_context.constructor) else module_type end typing.add_typing(node, type) else type_send(node, send_node: node, block_params: nil, block_body: nil) end end when :csend yield_self do type = if self_class?(node) module_type = (module_context.module_type) type = if module_type.is_a?(AST::Types::Name::Class) AST::Types::Name::Class.new(name: module_type.name, constructor: method_context.constructor) else module_type end typing.add_typing(node, type) else type_send(node, send_node: node, block_params: nil, block_body: nil, unwrap: true) end union_type(type, AST::Builtin.nil_type) end when :match_with_lvasgn each_child_node(node) do |child| synthesize(child) end typing.add_typing(node, AST::Builtin.any_type) when :op_asgn yield_self do lhs, op, rhs = node.children synthesize(rhs) lhs_type = case lhs.type when :lvasgn type_env.get(lvar: lhs.children.first.name) do break end when :ivasgn type_env.get(ivar: lhs.children.first) do break end else Steep.logger.error("Unexpected op_asgn lhs: #{lhs.type}") nil end case when lhs_type == AST::Builtin.any_type typing.add_typing(node, lhs_type) when !lhs_type fallback_to_any(node) else lhs_interface = checker.resolve(lhs_type) op_method = lhs_interface.methods[op] if op_method args = TypeInference::SendArgs.from_nodes([rhs]) return_type_or_error = type_method_call(node, receiver_type: lhs_type, method: op_method, args: args, block_params: nil, block_body: nil) if return_type_or_error.is_a?(Errors::Base) typing.add_error return_type_or_error else result = checker.check( Subtyping::Relation.new(sub_type: return_type_or_error, super_type: lhs_type), constraints: Subtyping::Constraints.empty ) if result.failure? typing.add_error( Errors::IncompatibleAssignment.new( node: node, lhs_type: lhs_type, rhs_type: return_type_or_error, result: result ) ) end end else typing.add_error Errors::NoMethod.new(node: node, method: op, type: lhs_type) end typing.add_typing(node, lhs_type) end end when :super yield_self do if self_type && method_context&.method if method_context.super_method each_child_node(node) do |child| synthesize(child) end super_method = method_context.super_method args = TypeInference::SendArgs.from_nodes(node.children.dup) return_type_or_error = type_method_call(node, receiver_type: self_type, method: super_method, args: args, block_params: nil, block_body: nil) if return_type_or_error.is_a?(Errors::Base) fallback_to_any node do return_type_or_error end else typing.add_typing node, return_type_or_error end else fallback_to_any node do Errors::UnexpectedSuper.new(node: node, method: method_context.name) end end else typing.add_typing node, AST::Builtin.any_type end end when :block yield_self do send_node, params, body = node.children if send_node.type == :lambda type_lambda(node, block_params: params, block_body: body, type_hint: hint) else type_send(node, send_node: send_node, block_params: params, block_body: body) end end when :def new = for_new_method(node.children[0], node, args: node.children[1].children, self_type: module_context&.instance_type) each_child_node(node.children[1]) do |arg| new.synthesize(arg) end if node.children[2] return_type = (new.method_context&.return_type) if return_type && !return_type.is_a?(AST::Types::Void) new.check(node.children[2], return_type) do |_, actual_type, result| typing.add_error(Errors::MethodBodyTypeMismatch.new(node: node, expected: new.method_context&.return_type, actual: actual_type, result: result)) end else new.synthesize(node.children[2]) end else return_type = (new.method_context&.return_type) if return_type && !return_type.is_a?(AST::Types::Void) result = checker.check( Subtyping::Relation.new(sub_type: AST::Builtin.nil_type, super_type: return_type), constraints: Subtyping::Constraints.empty ) if result.failure? typing.add_error(Errors::MethodBodyTypeMismatch.new(node: node, expected: new.method_context&.return_type, actual: AST::Builtin.nil_type, result: result)) end end end if module_context module_context.defined_instance_methods << node.children[0] end typing.add_typing(node, AST::Builtin.any_type) when :defs synthesize(node.children[0]).tap do |self_type| new = for_new_method(node.children[1], node, args: node.children[2].children, self_type: self_type) each_child_node(node.children[2]) do |arg| new.synthesize(arg) end if node.children[3] return_type = (new.method_context&.return_type) if return_type && !return_type.is_a?(AST::Types::Void) new.check(node.children[3], return_type) do |return_type, actual_type, result| typing.add_error(Errors::MethodBodyTypeMismatch.new(node: node, expected: return_type, actual: actual_type, result: result)) end else new.synthesize(node.children[3]) end end end if module_context if node.children[0].type == :self module_context.defined_module_methods << node.children[1] end end typing.add_typing(node, AST::Builtin::Symbol.instance_type) when :return yield_self do if node.children.size > 0 return_types = node.children.map do |value| synthesize(value) end value_type = if return_types.size == 1 return_types.first else AST::Builtin::Array.instance_type(union_type(*return_types)) end if (ret_type = (method_context&.return_type)) unless ret_type.is_a?(AST::Types::Void) result = checker.check( Subtyping::Relation.new(sub_type: value_type, super_type: ret_type), constraints: Subtyping::Constraints.empty ) if result.failure? typing.add_error(Errors::ReturnTypeMismatch.new(node: node, expected: method_context&.return_type, actual: value_type, result: result)) end end end end typing.add_typing(node, AST::Builtin.any_type) end when :break value = node.children[0] if break_context case when value && break_context.break_type check(value, break_context.break_type) do |break_type, actual_type, result| typing.add_error Errors::BreakTypeMismatch.new(node: node, expected: break_type, actual: actual_type, result: result) end when !value # ok else synthesize(value) if value typing.add_error Errors::UnexpectedJumpValue.new(node: node) end else synthesize(value) typing.add_error Errors::UnexpectedJump.new(node: node) end typing.add_typing(node, AST::Builtin.any_type) when :next value = node.children[0] if break_context case when value && break_context.next_type check(value, break_context.next_type) do |break_type, actual_type, result| typing.add_error Errors::BreakTypeMismatch.new(node: node, expected: break_type, actual: actual_type, result: result) end when !value # ok else synthesize(value) if value typing.add_error Errors::UnexpectedJumpValue.new(node: node) end else synthesize(value) typing.add_error Errors::UnexpectedJump.new(node: node) end typing.add_typing(node, AST::Builtin.any_type) when :retry unless break_context typing.add_error Errors::UnexpectedJump.new(node: node) end typing.add_typing(node, AST::Builtin.any_type) when :arg, :kwarg, :procarg0 yield_self do var = node.children[0] type = type_env.get(lvar: var.name) do fallback_to_any node end typing.add_typing(node, type) end when :optarg, :kwoptarg yield_self do var = node.children[0] rhs = node.children[1] type_assignment(var, rhs, node, hint: hint) end when :restarg yield_self do var = node.children[0] type = type_env.get(lvar: var.name) do typing.add_error Errors::FallbackAny.new(node: node) AST::Builtin::Array.instance_type(AST::Builtin.any_type) end typing.add_typing(node, type) end when :kwrestarg yield_self do var = node.children[0] type = type_env.get(lvar: var.name) do typing.add_error Errors::FallbackAny.new(node: node) AST::Builtin::Hash.instance_type(AST::Builtin::Symbol.instance_type, AST::Builtin.any_type) end typing.add_typing(node, type) end when :float typing.add_typing(node, AST::Builtin::Float.instance_type) when :nil typing.add_typing(node, AST::Builtin.nil_type) when :int yield_self do literal_type = (hint) {|hint_| test_literal_type(node.children[0], hint_) } if literal_type typing.add_typing(node, literal_type) else typing.add_typing(node, AST::Builtin::Integer.instance_type) end end when :sym yield_self do literal_type = (hint) {|hint_| test_literal_type(node.children[0], hint_) } if literal_type typing.add_typing(node, literal_type) else typing.add_typing(node, AST::Builtin::Symbol.instance_type) end end when :str yield_self do literal_type = (hint) {|hint_| test_literal_type(node.children[0], hint_) } if literal_type typing.add_typing(node, literal_type) else typing.add_typing(node, AST::Builtin::String.instance_type) end end when :true, :false typing.add_typing(node, AST::Types::Boolean.new) when :hash yield_self do ty = try_hash_type(node, hint) and return ty if AST::Builtin::Hash.instance_type?(hint) key_hint = hint.args[0] value_hint = hint.args[1] end key_types = [] value_types = [] each_child_node(node) do |child| case child.type when :pair key, value = child.children key_types << synthesize(key).yield_self do |type| select_super_type(type, key_hint) end value_types << synthesize(value).yield_self do |type| select_super_type(type, value_hint) end when :kwsplat (synthesize(child.children[0])) do |splat_type, original_type| if AST::Builtin::Hash.instance_type?(splat_type) key_types << splat_type.args[0] value_types << splat_type.args[1] else typing.add_error Errors::UnexpectedSplat.new(node: child, type: original_type) key_types << AST::Builtin.any_type value_types << AST::Builtin.any_type end end else raise "Unexpected non pair: #{child.inspect}" unless child.type == :pair end end key_type = key_types.empty? ? AST::Builtin.any_type : AST::Types::Union.build(types: key_types) value_type = value_types.empty? ? AST::Builtin.any_type : AST::Types::Union.build(types: value_types) if key_types.empty? && value_types.empty? && !hint typing.add_error Errors::FallbackAny.new(node: node) end typing.add_typing(node, AST::Builtin::Hash.instance_type(key_type, value_type)) end when :dstr, :xstr each_child_node(node) do |child| synthesize(child) end typing.add_typing(node, AST::Builtin::String.instance_type) when :dsym each_child_node(node) do |child| synthesize(child) end typing.add_typing(node, AST::Builtin::Symbol.instance_type) when :class yield_self do for_class(node).tap do |constructor| constructor.synthesize(node.children[2]) if node.children[2] if constructor.module_context&.implement_name && !namespace_module?(node) constructor.validate_method_definitions(node, constructor.module_context.implement_name) end end typing.add_typing(node, AST::Builtin.nil_type) end when :module yield_self do for_module(node).yield_self do |constructor| constructor.synthesize(node.children[1]) if node.children[1] if constructor.module_context&.implement_name && !namespace_module?(node) constructor.validate_method_definitions(node, constructor.module_context.implement_name) end end typing.add_typing(node, AST::Builtin.nil_type) end when :self if self_type typing.add_typing(node, self_type) else fallback_to_any node end when :const const_name = Names::Module.from_node(node) if const_name type = type_env.get(const: const_name) do fallback_to_any node end typing.add_typing node, type else fallback_to_any node end when :casgn yield_self do const_name = Names::Module.from_node(node) if const_name value_type = synthesize(node.children.last) type = type_env.assign(const: const_name, type: value_type) do |error| case error when Subtyping::Result::Failure const_type = type_env.get(const: const_name) typing.add_error(Errors::IncompatibleAssignment.new(node: node, lhs_type: const_type, rhs_type: value_type, result: error)) when nil typing.add_error(Errors::UnknownConstantAssigned.new(node: node, type: value_type)) end end typing.add_typing(node, type) else synthesize(node.children.last) fallback_to_any(node) end end when :yield if method_context&.method_type if method_context.block_type block_type = method_context.block_type block_type.type.params.flat_unnamed_params.map(&:last).zip(node.children).each do |(type, node)| if node && type check(node, type) do |_, rhs_type, result| typing.add_error(Errors::IncompatibleAssignment.new(node: node, lhs_type: type, rhs_type: rhs_type, result: result)) end end end typing.add_typing(node, block_type.type.return_type) else typing.add_error(Errors::UnexpectedYield.new(node: node)) fallback_to_any node end else fallback_to_any node end when :zsuper yield_self do if method_context&.method if method_context.super_method if method_context.method.incompatible? typing.add_error Errors::IncompatibleZuper.new(node: node, method: method_context.name) typing.add_typing node, AST::Builtin.any_type else types = method_context.super_method.types.map(&:return_type) typing.add_typing(node, union_type(*types)) end else typing.add_error(Errors::UnexpectedSuper.new(node: node, method: method_context.name)) fallback_to_any node end else fallback_to_any node end end when :array yield_self do if node.children.empty? typing.add_error Errors::FallbackAny.new(node: node) unless hint array_type = if hint relation = Subtyping::Relation.new( sub_type: AST::Builtin::Array.instance_type(AST::Builtin.any_type), super_type: hint ) if checker.check(relation, constraints: Subtyping::Constraints.empty).success? hint end end typing.add_typing(node, array_type || AST::Builtin::Array.instance_type(AST::Builtin.any_type)) else is_tuple = nil (hint) do |hint| is_tuple = hint.is_a?(AST::Types::Tuple) is_tuple &&= node.children.all? {|child| child.type != :splat } is_tuple &&= node.children.map.with_index do |child, index| synthesize(child, hint: hint.types[index]) end.yield_self do |types| tuple = AST::Types::Tuple.new(types: types) relation = Subtyping::Relation.new(sub_type: tuple, super_type: hint) result = checker.check(relation, constraints: Subtyping::Constraints.empty) result.success? end end if is_tuple array_type = hint else element_hint = (hint) do |hint| AST::Builtin::Array.instance_type?(hint) && hint.args[0] end element_types = node.children.flat_map do |e| if e.type == :splat Steep.logger.info "Typing of splat in array is incompatible with Ruby; it does not use #to_a method" synthesize(e.children.first).yield_self do |type| (type) do |ty| case ty when AST::Types::Union ty.types else [ty] end end end.map do |type| case when AST::Builtin::Array.instance_type?(type) type.args.first when AST::Builtin::Range.instance_type?(type) type.args.first else type end end else [select_super_type(synthesize(e), element_hint)] end end array_type = AST::Builtin::Array.instance_type(AST::Types::Union.build(types: element_types)) end typing.add_typing(node, array_type) end end when :and yield_self do left, right = node.children left_type = synthesize(left) truthy_vars = TypeConstruction.truthy_variables(left) right_type, right_env = for_branch(right, truthy_vars: truthy_vars).yield_self do |constructor| type = constructor.synthesize(right) [type, constructor.type_env] end type_env.join!([right_env, TypeInference::TypeEnv.new(subtyping: checker, const_env: nil)]) if left_type.is_a?(AST::Types::Boolean) typing.add_typing(node, union_type(left_type, right_type)) else typing.add_typing(node, union_type(right_type, AST::Builtin.nil_type)) end end when :or yield_self do t1, t2 = each_child_node(node).map {|child| synthesize(child) } type = union_type(unwrap(t1), t2) typing.add_typing(node, type) end when :if cond, true_clause, false_clause = node.children synthesize cond truthy_vars = TypeConstruction.truthy_variables(cond) if true_clause true_type, true_env = for_branch(true_clause, truthy_vars: truthy_vars).yield_self do |constructor| type = constructor.synthesize(true_clause, hint: hint) [type, constructor.type_env] end end if false_clause false_type, false_env = for_branch(false_clause).yield_self do |constructor| type = constructor.synthesize(false_clause, hint: hint) [type, constructor.type_env] end end type_env.join!([true_env, false_env].compact) typing.add_typing(node, union_type(true_type, false_type)) when :case yield_self do cond, *whens = node.children if cond cond_type = (synthesize(cond)) if cond_type.is_a?(AST::Types::Union) var_names = TypeConstruction.value_variables(cond) var_types = cond_type.types.dup end end pairs = whens.each.with_object([]) do |clause, pairs| if clause&.type == :when test_types = clause.children.take(clause.children.size - 1).map do |child| (synthesize(child, hint: hint)) end if (body = clause.children.last) if var_names && var_types && test_types.all? {|type| type.is_a?(AST::Types::Name::Class) } var_types_in_body = test_types.flat_map {|test_type| filtered_types = var_types.select {|var_type| var_type.name == test_type.name } if filtered_types.empty? to_instance_type(test_type) else filtered_types end } var_types.reject! {|type| var_types_in_body.any? {|test_type| test_type.name == type.name } } type_case_override = var_names.each.with_object({}) do |var_name, hash| hash[var_name] = union_type(*var_types_in_body) end else type_case_override = nil end for_branch(body, type_case_override: type_case_override).yield_self do |body_construction| type = body_construction.synthesize(body, hint: hint) pairs << [type, body_construction.type_env] end else pairs << [AST::Builtin.nil_type, nil] end else if clause if var_types if !var_types.empty? type_case_override = var_names.each.with_object({}) do |var_name, hash| hash[var_name] = union_type(*var_types) end var_types.clear else typing.add_error Errors::ElseOnExhaustiveCase.new(node: node, type: cond_type) type_case_override = var_names.each.with_object({}) do |var_name, hash| hash[var_name] = AST::Builtin.any_type end end end for_branch(clause, type_case_override: type_case_override).yield_self do |body_construction| type = body_construction.synthesize(clause, hint: hint) pairs << [type, body_construction.type_env] end end end end types = pairs.map(&:first) envs = pairs.map(&:last) if var_types unless var_types.empty? || whens.last types.push AST::Builtin.nil_type end end type_env.join!(envs.compact) typing.add_typing(node, union_type(*types)) end when :rescue yield_self do body, *resbodies, else_node = node.children body_type = synthesize(body) if body resbody_pairs = resbodies.map do |resbody| exn_classes, assignment, body = resbody.children if exn_classes case exn_classes.type when :array exn_types = exn_classes.children.map {|child| synthesize(child) } else Steep.logger.error "Unexpected exception list: #{exn_classes.type}" end end if assignment case assignment.type when :lvasgn var_name = assignment.children[0].name else Steep.logger.error "Unexpected rescue variable assignment: #{assignment.type}" end end type_override = {} case when exn_classes && var_name instance_types = exn_types.map do |type| type = (type) case when type.is_a?(AST::Types::Name::Class) to_instance_type(type) else AST::Builtin.any_type end end type_override[var_name] = AST::Types::Union.build(types: instance_types) when var_name type_override[var_name] = AST::Builtin.any_type end resbody_construction = for_branch(resbody, type_case_override: type_override) type = if body resbody_construction.synthesize(body) else AST::Builtin.nil_type end [type, resbody_construction.type_env] end resbody_types, resbody_envs = resbody_pairs.transpose if else_node else_construction = for_branch(else_node) else_type = else_construction.synthesize(else_node) else_env = else_construction.type_env end type_env.join!([*resbody_envs, else_env].compact) types = [body_type, *resbody_types, else_type].compact typing.add_typing(node, union_type(*types)) end when :resbody yield_self do klasses, asgn, body = node.children synthesize(klasses) if klasses synthesize(asgn) if asgn body_type = synthesize(body) if body typing.add_typing(node, body_type) end when :ensure yield_self do body, ensure_body = node.children body_type = synthesize(body) if body synthesize(ensure_body) if ensure_body typing.add_typing(node, union_type(body_type)) end when :masgn type_masgn(node) when :while, :while_post, :until, :until_post yield_self do cond, body = node.children synthesize(cond) truthy_vars = node.type == :while ? TypeConstruction.truthy_variables(cond) : Set.new if body for_loop = for_branch(body, truthy_vars: truthy_vars).with(break_context: BreakContext.new(break_type: nil, next_type: nil)) for_loop.synthesize(body) type_env.join!([for_loop.type_env]) end typing.add_typing(node, AST::Builtin.any_type) end when :irange, :erange types = node.children.map {|n| synthesize(n) } type = AST::Builtin::Range.instance_type(union_type(*types)) typing.add_typing(node, type) when :regexp each_child_node(node) do |child| synthesize(child) end typing.add_typing(node, AST::Builtin::Regexp.instance_type) when :regopt # ignore typing.add_typing(node, AST::Builtin.any_type) when :nth_ref, :back_ref typing.add_typing(node, AST::Builtin::String.instance_type) when :or_asgn, :and_asgn yield_self do _, rhs = node.children rhs_type = synthesize(rhs) typing.add_typing(node, rhs_type) end when :defined? each_child_node(node) do |child| synthesize(child) end typing.add_typing(node, AST::Builtin.any_type) when :gvasgn yield_self do name, rhs = node.children type = checker.builder.absolute_type(checker.builder.signatures.find_gvar(name)&.type, current: AST::Namespace.root) if type check(rhs, type) do |_, rhs_type, result| typing.add_error(Errors::IncompatibleAssignment.new(node: node, lhs_type: type, rhs_type: rhs_type, result: result)) end else synthesize(rhs) fallback_to_any node end end when :gvar yield_self do name = node.children.first type = type_env.get(gvar: name) do typing.add_error Errors::FallbackAny.new(node: node) end typing.add_typing(node, type) end when :block_pass yield_self do value = node.children[0] if hint.is_a?(AST::Types::Proc) && value.type == :sym if hint.one_arg? # Assumes Symbol#to_proc implementation param_type = hint.params.required[0] interface = checker.resolve(param_type) method = interface.methods[value.children[0]] if method return_types = method.types.flat_map do |method_type| if method_type.params.each_type.count == 0 [method_type.return_type] end end if return_types.any? type = AST::Types::Proc.new(params: Interface::Params.empty.update(required: [param_type]), return_type: AST::Types::Union.build(types: return_types)) end end else Steep.logger.error "Passing multiple args through Symbol#to_proc is not supported yet" end end type ||= synthesize(node.children[0], hint: hint) typing.add_typing node, type end when :blockarg yield_self do each_child_node node do |child| synthesize(child) end typing.add_typing node, AST::Builtin.any_type end when :splat, :sclass yield_self do Steep.logger.error "Unsupported node #{node.type}" each_child_node node do |child| synthesize(child) end typing.add_typing node, AST::Builtin.any_type end else raise "Unexpected node: #{node.inspect}, #{node.location.expression}" end end end |
#test_literal_type(literal, hint) ⇒ Object
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 |
# File 'lib/steep/type_construction.rb', line 2613 def test_literal_type(literal, hint) case hint when AST::Types::Literal if hint.value == literal hint end when AST::Types::Union if hint.types.any? {|ty| ty.is_a?(AST::Types::Literal) && ty.value == literal } hint end end end |
#to_instance_type(type, args: nil) ⇒ Object
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 |
# File 'lib/steep/type_construction.rb', line 2647 def to_instance_type(type, args: nil) args = args || case type when AST::Types::Name::Class checker.builder.signatures.find_class(type.name).params&.variables || [] when AST::Types::Name::Module checker.builder.signatures.find_module(type.name).params&.variables || [] end AST::Types::Name::Instance.new(name: type.name, args: args) end |
#try_hash_type(node, hint) ⇒ Object
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 |
# File 'lib/steep/type_construction.rb', line 2658 def try_hash_type(node, hint) if hint.is_a?(AST::Types::Record) typing.new_child do |child_typing| new_construction = with_new_typing(child_typing) elements = {} each_child_node(node) do |child| case child.type when :pair key, value = child.children key_value = case key.type when :str, :int, :sym key.children[0] else return nil end key_hint = AST::Types::Literal.new(value: key_value) value_hint = hint.elements[key_value] value_type = new_construction.synthesize(value, hint: value_hint) if value_hint relation = Subtyping::Relation.new(sub_type: value_type, super_type: value_hint) if checker.check(relation, constraints: Subtyping::Constraints.empty).success? value_type = value_hint end end elements[key_value] = value_type else return nil end end child_typing.save! hash = AST::Types::Record.new(elements: elements) typing.add_typing(node, hash) end end end |
#try_method_type(node, receiver_type:, method_type:, args:, arg_pairs:, block_params:, block_body:, child_typing:) ⇒ Object
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 |
# File 'lib/steep/type_construction.rb', line 2070 def try_method_type(node, receiver_type:, method_type:, args:, arg_pairs:, block_params:, block_body:, child_typing:) fresh_types = method_type.type_params.map {|x| AST::Types::Var.fresh(x) } fresh_vars = Set.new(fresh_types.map(&:name)) instantiation = Interface::Substitution.build(method_type.type_params, fresh_types) construction = self.class.new( checker: checker, source: source, annotations: annotations, type_env: type_env, typing: child_typing, self_type: self_type, method_context: method_context, block_context: block_context, module_context: module_context, break_context: break_context ) method_type.instantiate(instantiation).yield_self do |method_type| constraints = Subtyping::Constraints.new(unknowns: fresh_types.map(&:name)) variance = Subtyping::VariableVariance.from_method_type(method_type) occurence = Subtyping::VariableOccurence.from_method_type(method_type) arg_pairs.each do |pair| case pair when Array (arg_node, param_type) = pair param_type = param_type.subst(instantiation) arg_type = if arg_node.type == :splat type = construction.synthesize(arg_node.children[0]) child_typing.add_typing(arg_node, type) else construction.synthesize(arg_node, hint: param_type) end relation = Subtyping::Relation.new( sub_type: arg_type, super_type: param_type ) checker.check(relation, constraints: constraints).else do |result| return Errors::ArgumentTypeMismatch.new( node: arg_node, receiver_type: receiver_type, expected: relation.super_type, actual: relation.sub_type ) end else # keyword result = check_keyword_arg(receiver_type: receiver_type, node: pair, method_type: method_type, constraints: constraints) if result.is_a?(Errors::Base) return result end end end if block_params && method_type.block block_annotations = source.annotations(block: node, builder: checker.builder, current_module: current_namespace) block_params_ = TypeInference::BlockParams.from_node(block_params, annotations: block_annotations) block_param_hint = block_params_.params_type(hint: method_type.block.type.params) relation = Subtyping::Relation.new( sub_type: AST::Types::Proc.new(params: block_param_hint, return_type: AST::Types::Any.new), super_type: method_type.block.type ) checker.check(relation, constraints: constraints).else do |result| return Errors::IncompatibleBlockParameters.new( node: node, method_type: method_type ) end end case when method_type.block && block_params Steep.logger.debug "block is okay: method_type=#{method_type}" Steep.logger.debug "Constraints = #{constraints}" begin method_type.subst(constraints.solution(checker, variance: variance, variables: occurence.params)).yield_self do |method_type| block_type = construction.type_block(block_param_hint: method_type.block.type.params, block_type_hint: method_type.block.type.return_type, node_type_hint: method_type.return_type, block_params: block_params_, block_body: block_body, block_annotations: block_annotations) relation = Subtyping::Relation.new(sub_type: block_type.return_type, super_type: method_type.block.type.return_type) result = checker.check(relation, constraints: constraints) case result when Subtyping::Result::Success method_type.return_type.subst(constraints.solution(checker, variance: variance, variables: fresh_vars)).yield_self do |ret_type| if block_annotations.break_type AST::Types::Union.new(types: [block_annotations.break_type, ret_type]) else ret_type end end when Subtyping::Result::Failure Errors::BlockTypeMismatch.new(node: node, expected: method_type.block.type, actual: block_type, result: result) end end rescue Subtyping::Constraints::UnsatisfiableConstraint => exn Errors::UnsatisfiableConstraint.new(node: node, method_type: method_type, var: exn.var, sub_type: exn.sub_type, super_type: exn.super_type, result: exn.result) end when method_type.block && args.block_pass_arg begin method_type.subst(constraints.solution(checker, variance: variance, variables: occurence.params)).yield_self do |method_type| block_type = synthesize(args.block_pass_arg, hint: method_type.block.type) relation = Subtyping::Relation.new(sub_type: block_type, super_type: method_type.block.yield_self do |expected_block| if expected_block.optional? AST::Builtin.optional(expected_block.type) else expected_block.type end end) result = checker.check(relation, constraints: constraints) case result when Subtyping::Result::Success method_type.return_type.subst(constraints.solution(checker, variance: variance, variables: fresh_vars)) when Subtyping::Result::Failure Errors::BlockTypeMismatch.new(node: node, expected: method_type.block.type, actual: block_type, result: result) end end rescue Subtyping::Constraints::UnsatisfiableConstraint => exn Errors::UnsatisfiableConstraint.new(node: node, method_type: method_type, var: exn.var, sub_type: exn.sub_type, super_type: exn.super_type, result: exn.result) end when (!method_type.block || method_type.block.optional?) && !block_params && !block_body && !args.block_pass_arg # OK, without block method_type.subst(constraints.solution(checker, variance: variance, variables: fresh_vars)).return_type when !method_type.block && (block_params || args.block_pass_arg) Errors::UnexpectedBlockGiven.new( node: node, method_type: method_type ) when method_type.block && !method_type.block.optional? && !block_params && !block_body && !args.block_pass_arg Errors::RequiredBlockMissing.new( node: node, method_type: method_type ) else raise "Unexpected case condition" end end end |
#type_assignment(var, rhs, node, hint: nil) ⇒ Object
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 |
# File 'lib/steep/type_construction.rb', line 1571 def type_assignment(var, rhs, node, hint: nil) if rhs (synthesize(rhs, hint: type_env.lvar_types[var.name] || hint)) do |rhs_type| node_type = assign_type_to_variable(var, rhs_type, node) typing.add_typing(node, node_type) end else raise lhs_type = variable_type(var) if lhs_type typing.add_typing(node, lhs_type) else fallback_to_any node end end end |
#type_block(block_param_hint:, block_type_hint:, node_type_hint:, block_params:, block_body:, block_annotations:) ⇒ Object
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 |
# File 'lib/steep/type_construction.rb', line 2254 def type_block(block_param_hint:, block_type_hint:, node_type_hint:, block_params:, block_body:, block_annotations:) block_param_pairs = block_param_hint && block_params.zip(block_param_hint) param_types_hash = {} if block_param_pairs block_param_pairs.each do |param, type| var_name = param.var.name param_types_hash[var_name] = type end else block_params.each do |param| var_name = param.var.name param_types_hash[var_name] = param.type || AST::Builtin.any_type end end block_type_env = type_env.dup.tap do |env| param_types_hash.each do |name, type| env.set(lvar: name, type: type) end block_annotations.lvar_types.each do |name, type| env.set(lvar: name, type: type) end end break_type = if block_annotations.break_type union_type(node_type_hint, block_annotations.break_type) else node_type_hint end Steep.logger.debug("return_type = #{break_type}") block_context = BlockContext.new(body_type: block_annotations.block_type) Steep.logger.debug("block_context { body_type: #{block_context.body_type} }") break_context = BreakContext.new( break_type: break_type, next_type: block_context.body_type ) Steep.logger.debug("break_context { type: #{break_context.break_type} }") for_block_body = self.class.new( checker: checker, source: source, annotations: annotations.merge_block_annotations(block_annotations), type_env: block_type_env, block_context: block_context, typing: typing, method_context: method_context, module_context: module_context, self_type: block_annotations.self_type || self_type, break_context: break_context ) if block_body return_type = if (body_type = block_context.body_type) for_block_body.check(block_body, body_type) do |expected, actual, result| typing.add_error Errors::BlockTypeMismatch.new(node: block_body, expected: expected, actual: actual, result: result) end body_type else for_block_body.synthesize(block_body, hint: block_type_hint) end else return_type = AST::Builtin.any_type end AST::Types::Proc.new( params: block_param_hint || block_params.params_type, return_type: return_type ).tap do |type| Steep.logger.debug "block_type == #{type}" end end |
#type_ivasgn(name, rhs, node) ⇒ Object
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 |
# File 'lib/steep/type_construction.rb', line 1600 def type_ivasgn(name, rhs, node) rhs_type = synthesize(rhs, hint: type_env.get(ivar: name) { fallback_to_any(node) }) ivar_type = type_env.assign(ivar: name, type: rhs_type) do |error| case error when Subtyping::Result::Failure type = type_env.get(ivar: name) typing.add_error(Errors::IncompatibleAssignment.new(node: node, lhs_type: type, rhs_type: rhs_type, result: error)) when nil fallback_to_any node end end typing.add_typing(node, ivar_type) end |
#type_lambda(node, block_params:, block_body:, type_hint:) ⇒ Object
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 |
# File 'lib/steep/type_construction.rb', line 1733 def type_lambda(node, block_params:, block_body:, type_hint:) block_annotations = source.annotations(block: node, builder: checker.builder, current_module: current_namespace) params = TypeInference::BlockParams.from_node(block_params, annotations: block_annotations) case type_hint when AST::Types::Proc params_hint = type_hint.params return_hint = type_hint.return_type end block_type = type_block(block_param_hint: params_hint, block_type_hint: return_hint, node_type_hint: nil, block_params: params, block_body: block_body, block_annotations: block_annotations) typing.add_typing node, block_type end |
#type_masgn(node) ⇒ Object
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 |
# File 'lib/steep/type_construction.rb', line 1617 def type_masgn(node) lhs, rhs = node.children rhs_original = synthesize(rhs) rhs_type = (rhs_original) case when rhs.type == :array && lhs.children.all? {|a| a.type == :lvasgn || a.type == :ivasgn } && lhs.children.size == rhs.children.size pairs = lhs.children.zip(rhs.children) pairs.each do |(l, r)| case when l.type == :lvasgn type_assignment(l.children.first, r, l) when l.type == :ivasgn type_ivasgn(l.children.first, r, l) end end typing.add_typing(node, rhs_type) when rhs_type.is_a?(AST::Types::Tuple) && lhs.children.all? {|a| a.type == :lvasgn || a.type == :ivasgn } lhs.children.each.with_index do |asgn, index| type = rhs_type.types[index] case when asgn.type == :lvasgn && asgn.children[0].name != :_ type ||= AST::Builtin.nil_type type_env.assign(lvar: asgn.children[0].name, type: type) do |result| var_type = type_env.get(lvar: asgn.children[0].name) typing.add_error(Errors::IncompatibleAssignment.new(node: node, lhs_type: var_type, rhs_type: type, result: result)) end when asgn.type == :ivasgn type ||= AST::Builtin.nil_type type_env.assign(ivar: asgn.children[0], type: type) do |result| var_type = type_env.get(ivar: asgn.children[0]) typing.add_error(Errors::IncompatibleAssignment.new(node: node, lhs_type: var_type, rhs_type: type, result: result)) end end end typing.add_typing(node, rhs_type) when rhs_type.is_a?(AST::Types::Any) fallback_to_any(node) when AST::Builtin::Array.instance_type?(rhs_type) element_type = rhs_type.args.first lhs.children.each do |assignment| case assignment.type when :lvasgn assign_type_to_variable(assignment.children.first, element_type, assignment) when :ivasgn assignment.children.first.yield_self do |ivar| type_env.assign(ivar: ivar, type: element_type) do |error| case error when Subtyping::Result::Failure type = type_env.get(ivar: ivar) typing.add_error(Errors::IncompatibleAssignment.new(node: assignment, lhs_type: type, rhs_type: element_type, result: error)) when nil fallback_to_any node end end end end end typing.add_typing node, rhs_type when rhs_type.is_a?(AST::Types::Union) && rhs_type.types.all? {|type| AST::Builtin::Array.instance_type?(type) } types = rhs_type.types.flat_map do |type| type.args.first end element_type = AST::Types::Union.build(types: types) lhs.children.each do |assignment| case assignment.type when :lvasgn assign_type_to_variable(assignment.children.first, element_type, assignment) when :ivasgn assignment.children.first.yield_self do |ivar| type_env.assign(ivar: ivar, type: element_type) do |error| case error when Subtyping::Result::Failure type = type_env.get(ivar: ivar) typing.add_error(Errors::IncompatibleAssignment.new(node: assignment, lhs_type: type, rhs_type: element_type, result: error)) when nil fallback_to_any node end end end end end typing.add_typing node, rhs_type else Steep.logger.error("Unsupported masgn: #{rhs.type} (#{rhs_type})") fallback_to_any(node) end end |
#type_method_call(node, receiver_type:, method:, args:, block_params:, block_body:) ⇒ Object
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 |
# File 'lib/steep/type_construction.rb', line 1881 def type_method_call(node, receiver_type:, method:, args:, block_params:, block_body:) results = method.types.map do |method_type| Steep.logger.tagged method_type.location&.source do zips = args.zips(method_type.params, method_type.block&.type) type_or_error, child_typing = if zips.any? zips.map do |arg_pairs| child_typing = typing.new_child result = self.with_new_typing(child_typing).try_method_type( node, receiver_type: receiver_type, method_type: method_type, args: args, arg_pairs: arg_pairs, block_params: block_params, block_body: block_body, child_typing: child_typing ) unless result.is_a?(Errors::Base) break [[result, child_typing]] end [result, child_typing] end.first else Steep.logger.debug(node.inspect) [ Errors::IncompatibleArguments.new(node: node, receiver_type: receiver_type, method_type: method_type), typing.new_child ] end [child_typing, type_or_error, method_type] end end if (success = results.find {|_, type_or_error| !type_or_error.is_a?(Errors::Base) }) child_typing, type, _ = success child_typing.save! type else child_typing, error, method_type = results.first typing.add_error error child_typing.save! case method_type.return_type when AST::Types::Var AST::Builtin.any_type else method_type.return_type end end end |
#type_send(node, send_node:, block_params:, block_body:, unwrap: false) ⇒ Object
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 |
# File 'lib/steep/type_construction.rb', line 1753 def type_send(node, send_node:, block_params:, block_body:, unwrap: false) receiver, method_name, *arguments = send_node.children receiver_type = receiver ? synthesize(receiver) : self_type if unwrap receiver_type = unwrap(receiver_type) end return_type = case receiver_type when AST::Types::Any typing.add_typing node, AST::Builtin.any_type when nil fallback_to_any node else begin interface = checker.resolve(receiver_type) method = interface.methods[method_name] if method args = TypeInference::SendArgs.from_nodes(arguments) return_type_or_error = type_method_call(node, method: method, args: args, block_params: block_params, block_body: block_body, receiver_type: receiver_type) if return_type_or_error.is_a?(Errors::Base) fallback_to_any node do return_type_or_error end else typing.add_typing node, return_type_or_error end else fallback_to_any node do Errors::NoMethod.new(node: node, method: method_name, type: receiver_type) end end rescue Subtyping::Check::CannotResolveError fallback_to_any node do Errors::NoMethod.new(node: node, method: method_name, type: receiver_type) end end end case return_type when nil, Errors::Base arguments.each do |arg| unless typing.has_type?(arg) if arg.type == :splat type = synthesize(arg.children[0]) typing.add_typing(arg, AST::Builtin::Array.instance_type(type)) else synthesize(arg) end end end if block_body && block_params unless typing.has_type?(block_body) block_annotations = source.annotations(block: node, builder: checker.builder, current_module: current_namespace) params = TypeInference::BlockParams.from_node(block_params, annotations: block_annotations) pairs = params.each.map {|param| [param, AST::Builtin.any_type] } for_block, _ = for_block(block_annotations: block_annotations, param_pairs: pairs, method_return_type: AST::Builtin.any_type, typing: typing) for_block.synthesize(block_body) end end else return_type end end |
#union_type(*types) ⇒ Object
2434 2435 2436 |
# File 'lib/steep/type_construction.rb', line 2434 def union_type(*types) AST::Types::Union.build(types: types) end |
#unwrap(type) ⇒ Object
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 |
# File 'lib/steep/type_construction.rb', line 2561 def unwrap(type) (type) do || case when .is_a?(AST::Types::Union) types = .types.reject {|type| type.is_a?(AST::Types::Nil) } AST::Types::Union.build(types: types) else type end end end |
#validate_method_definitions(node, module_name) ⇒ Object
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 |
# File 'lib/steep/type_construction.rb', line 2438 def validate_method_definitions(node, module_name) signature = checker.builder.signatures.find_class_or_module(module_name.name) expected_instance_method_names = Set.new() expected_module_method_names = Set.new signature.members.each do |member| case member when AST::Signature::Members::Method if member.instance_method? expected_instance_method_names << member.name end if member.module_method? expected_module_method_names << member.name end when AST::Signature::Members::Attr expected_instance_method_names << member.name expected_instance_method_names << "#{member.name}=".to_sym if member.accessor? end end expected_instance_method_names.each do |method_name| case when module_context.defined_instance_methods.include?(method_name) # ok when annotations.instance_dynamics.include?(method_name) # ok else typing.add_error Errors::MethodDefinitionMissing.new(node: node, module_name: module_name.name, kind: :instance, missing_method: method_name) end end expected_module_method_names.each do |method_name| case when module_context.defined_module_methods.include?(method_name) # ok when annotations.module_dynamics.include?(method_name) # ok else typing.add_error Errors::MethodDefinitionMissing.new(node: node, module_name: module_name.name, kind: :module, missing_method: method_name) end end annotations.instance_dynamics.each do |method_name| unless expected_instance_method_names.member?(method_name) typing.add_error Errors::UnexpectedDynamicMethod.new(node: node, module_name: module_name.name, method_name: method_name) end end annotations.module_dynamics.each do |method_name| unless expected_module_method_names.member?(method_name) typing.add_error Errors::UnexpectedDynamicMethod.new(node: node, module_name: module_name.name, method_name: method_name) end end end |
#with(annotations: NOTHING, type_env: NOTHING, method_context: NOTHING, block_context: NOTHING, module_context: NOTHING, self_type: NOTHING, break_context: NOTHING) ⇒ Object
489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/steep/type_construction.rb', line 489 def with(annotations: NOTHING, type_env: NOTHING, method_context: NOTHING, block_context: NOTHING, module_context: NOTHING, self_type: NOTHING, break_context: NOTHING) self.class.new( checker: checker, source: source, annotations: annotations.equal?(NOTHING) ? self.annotations : annotations, type_env: type_env.equal?(NOTHING) ? self.type_env : type_env, typing: typing, method_context: method_context.equal?(NOTHING) ? self.method_context : method_context, block_context: block_context.equal?(NOTHING) ? self.block_context : block_context, module_context: module_context.equal?(NOTHING) ? self.module_context : module_context, self_type: self_type.equal?(NOTHING) ? self.self_type : self_type, break_context: break_context.equal?(NOTHING) ? self.break_context : break_context ) end |
#with_new_typing(typing) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/steep/type_construction.rb', line 93 def with_new_typing(typing) self.class.new( checker: checker, source: source, annotations: annotations, type_env: type_env, typing: typing, self_type: self_type, method_context: method_context, block_context: block_context, module_context: module_context, break_context: break_context ) end |