Skip to content

Commit

Permalink
Showing 9 changed files with 78 additions and 194 deletions.
15 changes: 6 additions & 9 deletions src/compiler/crystal/semantic/call.cr
Original file line number Diff line number Diff line change
@@ -350,7 +350,7 @@ class Crystal::Call
lookup_arg_types = match.arg_types
end
match_owner = match.context.instantiated_type
def_instance_owner = self_type || match_owner
def_instance_owner = (self_type || match_owner).as(DefInstanceContainer)

def_instance_key = DefInstanceKey.new(match.def.object_id, lookup_arg_types, block_type, named_args_types)
typed_def = def_instance_owner.lookup_def_instance def_instance_key if use_cache
@@ -411,12 +411,6 @@ class Crystal::Call
end

def check_return_type(typed_def, typed_def_return_type, match, match_owner)
if match.def.owner == program.class_type
root_type = program.class_type
else
self_type = match_owner.instance_type
root_type = self_type.ancestors.find(&.instance_of?(match.def.owner.instance_type)) || self_type
end
return_type = lookup_node_type(match.context, typed_def_return_type)
return_type = program.nil if return_type.void?
typed_def.freeze_type = return_type
@@ -1049,8 +1043,11 @@ class Crystal::Call
{typed_def, args}
end

def attach_subclass_observer(type : ModuleType)
@subclass_notifier.try &.remove_subclass_observer(self)
def attach_subclass_observer(type : SubclassObservable)
if (subclass_notifier = @subclass_notifier).is_a?(SubclassObservable)
subclass_notifier.remove_subclass_observer(self)
end

type.add_subclass_observer(self)
@subclass_notifier = type
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
@@ -2312,7 +2312,7 @@ module Crystal
types = node_types.map do |type|
type.accept self
instance_type = type.type.instance_type
unless instance_type.subclass_of?(@program.exception)
unless instance_type.implements?(@program.exception)
type.raise "#{type} is not a subclass of Exception"
end
instance_type
8 changes: 7 additions & 1 deletion src/compiler/crystal/semantic/method_missing.cr
Original file line number Diff line number Diff line change
@@ -71,7 +71,13 @@ module Crystal

owner = self
owner = owner.base_type if owner.is_a?(VirtualType)
owner.add_def(a_def) if owner.is_a?(ModuleType)

if owner.is_a?(ModuleType)
owner.add_def(a_def)
true
else
false
end
end
end

2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/new.cr
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ module Crystal
end
end
else
type.lookup_new_in_ancestors = true
type.as(ClassType).lookup_new_in_ancestors = true
end
end
end
10 changes: 5 additions & 5 deletions src/compiler/crystal/semantic/restrictions.cr
Original file line number Diff line number Diff line change
@@ -309,7 +309,7 @@ module Crystal
end

def restrict(other : VirtualType, context)
subclass_of?(other.base_type) ? self : nil
implements?(other.base_type) ? self : nil
end

def restrict(other : Union, context)
@@ -394,7 +394,7 @@ module Crystal
end

def restriction_of?(other : VirtualType, owner)
subclass_of? other.base_type
implements? other.base_type
end

def restriction_of?(other : Type, owner)
@@ -773,7 +773,7 @@ module Crystal
class VirtualType
def restriction_of?(other : Type, owner)
other = other.base_type if other.is_a?(VirtualType)
base_type.subclass_of?(other) || other.subclass_of?(base_type)
base_type.implements?(other) || other.implements?(base_type)
end

def restrict(other : Type, context)
@@ -789,9 +789,9 @@ module Crystal
elsif other.is_a?(VirtualType)
result = base_type.restrict(other.base_type, context) || other.base_type.restrict(base_type, context)
result ? result.virtual_type : nil
elsif other.subclass_of?(self.base_type)
elsif other.implements?(self.base_type)
other.virtual_type
elsif self.base_type.subclass_of?(other)
elsif self.base_type.implements?(other)
self
elsif other.module?
if base_type.implements?(other)
23 changes: 10 additions & 13 deletions src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
@@ -745,21 +745,18 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
end

def run_hooks(type_with_hooks, current_type, kind, node, call = nil)
hooks = type_with_hooks.hooks
if hooks
hooks.each do |hook|
next if hook.kind != kind

expansion = expand_macro(hook.macro, node) do
if call
@program.expand_macro hook.macro, call, current_type.instance_type
else
@program.expand_macro hook.macro.body, current_type.instance_type
end
end
type_with_hooks.as?(ModuleType).try &.hooks.try &.each do |hook|
next if hook.kind != kind

node.add_hook_expansion(expansion)
expansion = expand_macro(hook.macro, node) do
if call
@program.expand_macro hook.macro, call, current_type.instance_type
else
@program.expand_macro hook.macro.body, current_type.instance_type
end
end

node.add_hook_expansion(expansion)
end

if kind == :inherited && (superclass = type_with_hooks.instance_type.superclass)
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/type_declaration_visitor.cr
Original file line number Diff line number Diff line change
@@ -142,7 +142,7 @@ class Crystal::TypeDeclarationVisitor < Crystal::SemanticVisitor

if current_type.is_a?(Program)
key = DefInstanceKey.new external.object_id, external.args.map(&.type), nil, nil
current_type.add_def_instance key, external
program.add_def_instance key, external
end

node.type = @program.nil
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/doc/type.cr
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ class Crystal::Doc::Type
when VoidType
"Void"
when InheritedGenericClass
type.extended_class.name
type.extended_class.as(NamedType).name
when IncludedGenericModule
type.module.name
when Const
208 changes: 46 additions & 162 deletions src/compiler/crystal/types.cr

Large diffs are not rendered by default.

0 comments on commit cde8131

Please sign in to comment.