Skip to content

Commit

Permalink
Merge pull request #53 from tompng/remove_unused_proc_type
Browse files Browse the repository at this point in the history
Remove unused ProcType
  • Loading branch information
tompng authored Oct 30, 2023
2 parents 69616f5 + 446d697 commit 12deaf6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
5 changes: 1 addition & 4 deletions lib/katakata_irb/completor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ def self.setup
return KatakataIrb::Types.class_name_of(t.module_or_class)
when KatakataIrb::Types::InstanceType
return KatakataIrb::Types.class_name_of(t.klass)
when KatakataIrb::Types::ProcType
return 'Proc'
end
end
nil
Expand Down Expand Up @@ -290,8 +288,7 @@ def self.find_target(node, position)
)
return [node] if location&.start_offset == position

node.child_nodes.each do |n|
next unless n.is_a? Prism::Node
node.compact_child_nodes.each do |n|
match = find_target(n, position)
next unless match
match.unshift node
Expand Down
2 changes: 1 addition & 1 deletion lib/katakata_irb/type_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def evaluate_lambda_node(node, scope)
end
block_scope.merge_jumps
scope.update block_scope
KatakataIrb::Types::ProcType.new
KatakataIrb::Types::PROC
end

def evaluate_reference_write(node, scope)
Expand Down
31 changes: 3 additions & 28 deletions lib/katakata_irb/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def self.rbs_search_method(klass, method_name, singleton)
def self.method_return_type(type, method_name)
receivers = type.types.map do |t|
case t
in ProcType
[t, Proc, false]
in SingletonType
[t, t.module_or_class, true]
in InstanceType
Expand All @@ -70,8 +68,6 @@ def self.rbs_methods(type, method_name, args_types, kwargs_type, has_block)

receivers = type.types.map do |t|
case t
in ProcType
[t, Proc, false]
in SingletonType
[t, t.module_or_class, true]
in InstanceType
Expand Down Expand Up @@ -125,7 +121,6 @@ def self.rbs_methods(type, method_name, args_types, kwargs_type, has_block)
def self.intersect?(a, b)
atypes = a.types.group_by(&:class)
btypes = b.types.group_by(&:class)
return true if atypes[ProcType] && btypes[ProcType]
if atypes[SingletonType] && btypes[SingletonType]
aa, bb = [atypes, btypes].map {|types| types[SingletonType].map(&:module_or_class) }
return true if (aa & bb).any?
Expand Down Expand Up @@ -233,23 +228,6 @@ def inspect_without_params
end
end

class ProcType
attr_reader :params, :kwparams, :return_type
def initialize(params = [], kwparams = {}, return_type = NIL)
@params = params
@kwparams = kwparams
@return_type = return_type
end
def transform() = yield(self)
def methods() = Proc.instance_methods
def all_methods() = Proc.instance_methods | Proc.private_instance_methods
def constants() = []
def types() = [self]
def nillable?() = (@klass == NilClass)
def nonnillable() = self
def inspect() = 'Proc'
end

NIL = InstanceType.new NilClass
OBJECT = InstanceType.new Object
TRUE = InstanceType.new TrueClass
Expand All @@ -266,7 +244,7 @@ def inspect() = 'Proc'
HASH = InstanceType.new Hash
CLASS = InstanceType.new Class
MODULE = InstanceType.new Module
PROC = ProcType.new
PROC = InstanceType.new Proc

class UnionType
attr_reader :types
Expand All @@ -275,7 +253,6 @@ def initialize(*types)
@types = []
singletons = []
instances = {}
procs = []
collect = -> type do
case type
in UnionType
Expand All @@ -287,12 +264,10 @@ def initialize(*types)
end
in SingletonType
singletons << type
in ProcType
procs << type
end
end
types.each(&collect)
@types = procs.uniq + singletons.uniq + instances.map do |klass, params|
@types = singletons.uniq + instances.map do |klass, params|
InstanceType.new(klass, params.transform_values { |v| UnionType[*v] })
end
end
Expand Down Expand Up @@ -364,7 +339,7 @@ def self.from_rbs_type(return_type, self_type, extra_vars = {})
when RBS::Types::Union
UnionType[*return_type.types.map { from_rbs_type _1, self_type, extra_vars }]
when RBS::Types::Proc
InstanceType.new Proc
PROC
when RBS::Types::Tuple
elem = UnionType[*return_type.types.map { from_rbs_type _1, self_type, extra_vars }]
InstanceType.new Array, Elem: elem
Expand Down
9 changes: 9 additions & 0 deletions test/test_type_analyze.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,15 @@ def test_conditional_assign
RUBY
end

def test_block
assert_call('nil.then{1}.', include: Integer, exclude: NilClass)
assert_call('nil.then(&:to_s).', include: String, exclude: NilClass)
end

def test_block_break
assert_call('1.tap{}.', include: [Integer], exclude: NilClass)
assert_call('1.tap{break :a}.', include: [Symbol, Integer], exclude: NilClass)
assert_call('1.tap{break :a, :b}[0].', include: Symbol)
assert_call('1.tap{break :a; break "a"}.', include: [Symbol, Integer], exclude: [NilClass, String])
assert_call('1.tap{break :a if b}.', include: [Symbol, Integer], exclude: NilClass)
assert_call('1.tap{break :a; break "a" if b}.', include: [Symbol, Integer], exclude: [NilClass, String])
Expand All @@ -186,6 +193,8 @@ def test_instance_eval

def test_block_next
assert_call('nil.then{1}.', include: Integer, exclude: [NilClass, Object])
assert_call('nil.then{next 1}.', include: Integer, exclude: [NilClass, Object])
assert_call('nil.then{next :a, :b}[0].', include: Symbol)
assert_call('nil.then{next 1; 1.0}.', include: Integer, exclude: [Float, NilClass, Object])
assert_call('nil.then{next 1; next 1.0}.', include: Integer, exclude: [Float, NilClass, Object])
assert_call('nil.then{1 if cond}.', include: [Integer, NilClass], exclude: Object)
Expand Down

0 comments on commit 12deaf6

Please sign in to comment.