Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverting #12405 Compiler: don't always use Array for node dependencies and observers #12849

Merged
merged 1 commit into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 0 additions & 166 deletions spec/compiler/crystal/zero_one_or_many_spec.cr

This file was deleted.

4 changes: 2 additions & 2 deletions src/compiler/crystal/codegen/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Crystal::CodeGenVisitor
end

target_defs = node.target_defs
if target_defs.empty?
unless target_defs
node.raise "BUG: no target defs"
end

Expand Down Expand Up @@ -394,7 +394,7 @@ class Crystal::CodeGenVisitor
position_at_end current_def_label

# Prepare this specific call
call.target_defs = ZeroOneOrMany.new(a_def)
call.target_defs = [a_def] of Def
call.obj.try &.set_type(a_def.owner)
call.args.zip(a_def.args) do |call_arg, a_def_arg|
call_arg.set_type(a_def_arg.type)
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/interpreter/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
end

target_defs = node.target_defs
if target_defs.empty?
unless target_defs
node.raise "BUG: no target defs"
end

Expand Down Expand Up @@ -2458,7 +2458,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
end

target_defs = call.target_defs
if target_defs.empty?
unless target_defs
call.raise "BUG: no target defs"
end

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/crystal/interpreter/multidispatch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require "../semantic/main_visitor"
# end
# ```
module Crystal::Repl::Multidispatch
def self.create_def(context : Context, node : Call, target_defs : ZeroOneOrMany(Def))
def self.create_def(context : Context, node : Call, target_defs : Array(Def))
if node.block
a_def = create_def_uncached(context, node, target_defs)

Expand Down Expand Up @@ -70,7 +70,7 @@ module Crystal::Repl::Multidispatch
a_def
end

private def self.create_def_uncached(context : Context, node : Call, target_defs : ZeroOneOrMany(Def))
private def self.create_def_uncached(context : Context, node : Call, target_defs : Array(Def))
autocast_types = nil

# The generated multidispatch method should handle autocasted
Expand Down Expand Up @@ -200,7 +200,7 @@ module Crystal::Repl::Multidispatch
end

call = Call.new(call_obj, node.name, call_args)
call.target_defs = ZeroOneOrMany(Def).new(target_def)
call.target_defs = [target_def]
call.type = target_def.type

if block
Expand Down
47 changes: 24 additions & 23 deletions src/compiler/crystal/semantic/bindings.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Crystal
class ASTNode
property dependencies = ZeroOneOrMany(ASTNode).new
property observers = ZeroOneOrMany(ASTNode).new
property! dependencies : Array(ASTNode)
property observers : Array(ASTNode)?
property enclosing_call : Call?

@dirty = false
Expand Down Expand Up @@ -107,17 +107,17 @@ module Crystal
end

def bind_to(node : ASTNode) : Nil
bind(node) do
@dependencies.push node
bind(node) do |dependencies|
dependencies.push node
node.add_observer self
end
end

def bind_to(nodes : Indexable(ASTNode)) : Nil
def bind_to(nodes : Indexable) : Nil
return if nodes.empty?

bind do
@dependencies.concat nodes
bind do |dependencies|
dependencies.concat nodes
nodes.each &.add_observer self
end
end
Expand All @@ -130,7 +130,9 @@ module Crystal
raise_frozen_type freeze_type, from_type, from
end

yield
dependencies = @dependencies ||= [] of ASTNode

yield dependencies

new_type = type_from_dependencies
new_type = map_type(new_type) if new_type
Expand All @@ -151,28 +153,27 @@ module Crystal
Type.merge dependencies
end

def unbind_from(nodes : Nil) : Nil
def unbind_from(nodes : Nil)
# Nothing to do
end

def unbind_from(node : ASTNode) : Nil
@dependencies.reject! &.same?(node)
def unbind_from(node : ASTNode)
@dependencies.try &.reject! &.same?(node)
node.remove_observer self
end

def unbind_from(nodes : Enumerable(ASTNode)) : Nil
@dependencies.reject! { |dependency|
nodes.any? &.same?(dependency)
}
def unbind_from(nodes : Array(ASTNode))
@dependencies.try &.reject! { |dep| nodes.any? &.same?(dep) }
nodes.each &.remove_observer self
end

def add_observer(observer : ASTNode) : Nil
@observers.push observer
def add_observer(observer)
observers = @observers ||= [] of ASTNode
observers.push observer
end

def remove_observer(observer : ASTNode) : Nil
@observers.reject! &.same?(observer)
def remove_observer(observer)
@observers.try &.reject! &.same?(observer)
end

def set_enclosing_call(enclosing_call)
Expand All @@ -194,9 +195,9 @@ module Crystal
end

def notify_observers
@observers.each &.update self
@observers.try &.each &.update self
@enclosing_call.try &.recalculate
@observers.each &.propagate
@observers.try &.each &.propagate
@enclosing_call.try &.propagate
end

Expand Down Expand Up @@ -268,8 +269,8 @@ module Crystal
visited = Set(ASTNode).new.compare_by_identity
owner_trace << node if node.type?.try &.includes_type?(owner)
visited.add node
until node.dependencies.empty?
dependencies = node.dependencies.select { |dep| dep.type? && dep.type.includes_type?(owner) && !visited.includes?(dep) }
while deps = node.dependencies?
dependencies = deps.select { |dep| dep.type? && dep.type.includes_type?(owner) && !visited.includes?(dep) }
if dependencies.size > 0
node = dependencies.first
nil_reason = node.nil_reason if node.is_a?(MetaTypeVar)
Expand Down
Loading