Skip to content

Commit

Permalink
Move type_env to Context
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Feb 12, 2020
1 parent 579010b commit 053373d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/steep/project/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def type_check(subtyping, env_updated_at)
method_context: nil,
break_context: nil,
self_type: AST::Builtin::Object.instance_type,
type_env: type_env
),
typing: typing,
type_env: type_env
typing: typing
)

construction.synthesize(source.node)
Expand Down
50 changes: 26 additions & 24 deletions lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@ def self_type
context.self_type
end

def initialize(checker:, source:, annotations:, type_env:, typing:, context:)
def type_env
context.type_env
end

def initialize(checker:, source:, annotations:, typing:, context:)
@checker = checker
@source = source
@annotations = annotations
@typing = typing
@context = context
@type_env = type_env
end

def with_new_typing(typing)
self.class.new(
checker: checker,
source: source,
annotations: annotations,
type_env: type_env,
typing: typing,
context: context
)
Expand Down Expand Up @@ -144,13 +146,13 @@ def for_new_method(method_name, node, args:, self_type:, definition:)
checker: checker,
source: source,
annotations: annots,
type_env: type_env,
context: TypeInference::Context.new(
method_context: method_context,
module_context: module_context,
block_context: nil,
break_context: nil,
self_type: annots.self_type || self_type
self_type: annots.self_type || self_type,
type_env: type_env
),
typing: typing,
)
Expand Down Expand Up @@ -244,14 +246,14 @@ def for_module(node)
checker: checker,
source: source,
annotations: annots,
type_env: module_type_env,
typing: typing,
context: TypeInference::Context.new(
method_context: nil,
block_context: nil,
break_context: nil,
module_context: module_context_,
self_type: module_context_.module_type
self_type: module_context_.module_type,
type_env: module_type_env
)
)
end
Expand Down Expand Up @@ -344,31 +346,30 @@ def for_class(node)
checker: checker,
source: source,
annotations: annots,
type_env: class_type_env,
typing: typing,
context: TypeInference::Context.new(
method_context: nil,
block_context: nil,
module_context: module_context,
break_context: nil,
self_type: module_context.module_type
self_type: module_context.module_type,
type_env: class_type_env
)
)
end

def for_branch(node, truthy_vars: Set.new, type_case_override: nil)
def for_branch(node, truthy_vars: Set.new, type_case_override: nil, break_context: context.break_context)
annots = source.annotations(block: node, factory: checker.factory, 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, self_type: self_type) do |var, relation, result|

type_env = self.type_env.with_annotations(lvar_types: lvar_types, self_type: self_type) do |var, relation, result|
raise "Unexpected annotate failure: #{relation}"
end

Expand Down Expand Up @@ -398,17 +399,16 @@ def for_branch(node, truthy_vars: Set.new, type_case_override: nil)
)
end

with(type_env: type_env)
with(context: context.with(type_env: type_env, break_context: break_context))
end

NOTHING = ::Object.new

def with(annotations: NOTHING, type_env: NOTHING, context: NOTHING)
def with(annotations: NOTHING, 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,
context: context.equal?(NOTHING) ? self.context : context
)
Expand Down Expand Up @@ -1345,9 +1345,12 @@ def synthesize(node, hint: nil)
truthy_vars = node.type == :while ? TypeConstruction.truthy_variables(cond) : Set.new

if body
for_loop = for_branch(body, truthy_vars: truthy_vars).with(
context: context.with(break_context: TypeInference::Context::BreakContext.new(break_type: nil, next_type: nil))
)
for_loop = for_branch(body,
truthy_vars: truthy_vars,
break_context: TypeInference::Context::BreakContext.new(
break_type: nil,
next_type: nil
))
for_loop.synthesize(body)
type_env.join!([for_loop.type_env])
end
Expand Down Expand Up @@ -1799,14 +1802,14 @@ def for_block(block_annotations:, param_pairs:, method_return_type:, typing:)
checker: checker,
source: source,
annotations: annotations.merge_block_annotations(block_annotations),
type_env: block_type_env,
typing: typing,
context: TypeInference::Context.new(
block_context: block_context,
method_context: method_context,
module_context: module_context,
break_context: break_context,
self_type: block_annotations.self_type || self_type
self_type: block_annotations.self_type || self_type,
type_env: block_type_env
)
), return_type]
end
Expand Down Expand Up @@ -2104,7 +2107,6 @@ def try_method_type(node, receiver_type:, method_type:, args:, arg_pairs:, block
checker: checker,
source: source,
annotations: annotations,
type_env: type_env,
typing: child_typing,
context: context
)
Expand Down Expand Up @@ -2318,14 +2320,14 @@ def type_block(block_param_hint:, block_type_hint:, node_type_hint:, block_param
checker: checker,
source: source,
annotations: annotations.merge_block_annotations(block_annotations),
type_env: block_type_env,
typing: typing,
context: TypeInference::Context.new(
block_context: block_context,
method_context: method_context,
module_context: module_context,
break_context: break_context,
self_type: block_annotations.self_type || self_type
self_type: block_annotations.self_type || self_type,
type_env: block_type_env
)
)

Expand Down
10 changes: 7 additions & 3 deletions lib/steep/type_inference/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,30 @@ def const_context
attr_reader :break_context
attr_reader :module_context
attr_reader :self_type
attr_reader :type_env

def initialize(method_context:, block_context:, break_context:, module_context:, self_type:)
def initialize(method_context:, block_context:, break_context:, module_context:, self_type:, type_env:)
@method_context = method_context
@block_context = block_context
@break_context = break_context
@module_context = module_context
@self_type = self_type
@type_env = type_env
end

def with(method_context: self.method_context,
block_context: self.block_context,
break_context: self.break_context,
module_context: self.module_context,
self_type: self.self_type)
self_type: self.self_type,
type_env: self.type_env)
self.class.new(
method_context: method_context,
block_context: block_context,
break_context: break_context,
module_context: module_context,
self_type: self_type
self_type: self_type,
type_env: type_env
)
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ def with_standard_construction(checker, source)
construction = TypeConstruction.new(checker: checker,
source: source,
annotations: annotations,
type_env: type_env,
context: Context.new(
block_context: nil,
method_context: nil,
module_context: nil,
break_context: nil,
self_type: parse_type("::Object")
self_type: parse_type("::Object"),
type_env: type_env
),
typing: typing)

Expand Down Expand Up @@ -952,13 +952,13 @@ class Steep::Names::Module end
construction = TypeConstruction.new(checker: checker,
source: source,
annotations: annotations,
type_env: type_env,
context: Context.new(
block_context: nil,
method_context: nil,
module_context: module_context,
break_context: nil,
self_type: nil
self_type: nil,
type_env: type_env
),
typing: typing)

Expand Down Expand Up @@ -1043,13 +1043,13 @@ module Steep::Printable end
construction = TypeConstruction.new(checker: checker,
source: source,
annotations: annotations,
type_env: type_env,
context: Context.new(
block_context: nil,
method_context: nil,
module_context: module_context,
break_context: nil,
self_type: nil
self_type: nil,
type_env: type_env
),
typing: typing)

Expand Down

0 comments on commit 053373d

Please sign in to comment.