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

Compiler: don't say "undefined method" on abstract leaf types #8870

Merged
merged 2 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 62 additions & 0 deletions spec/compiler/semantic/class_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1091,4 +1091,66 @@ describe "Semantic: class" do
),
"can't read instance variables of union types (@x of (Bar | Foo))"
end

it "types as no return if calling method on abstract class with all abstract subclasses (#6996)" do
assert_type(%(
require "prelude"

abstract class Foo
abstract def foo?
end

abstract class Bar < Foo
end

Pointer(Foo).malloc(1_u64).value.foo?
)) { no_return }
end

it "types as no return if calling method on abstract class with generic subclasses but no instances (#6996)" do
assert_type(%(
require "prelude"

abstract class Foo
abstract def foo?
end

class Bar(T) < Foo
def foo?
true
end
end

Pointer(Foo).malloc(1_u64).value.foo?
)) { no_return }
end

it "types as no return if calling method on abstract generic class (#6996)" do
assert_type(%(
require "prelude"

abstract class Foo(T)
abstract def foo?
end

Pointer(Foo(Int32)).malloc(1_u64).value.foo?
)) { no_return }
end

it "types as no return if calling method on generic class with subclasses (#6996)" do
assert_type(%(
require "prelude"

abstract class Foo(T)
abstract def foo?
end

abstract class Bar(T) < Foo(T)
end

Bar(Int32)

Pointer(Foo(Int32)).malloc(1_u64).value.foo?
)) { no_return }
end
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class Crystal::Call
# don't give error. This is to allow small code comments without giving
# compile errors, which will anyway appear once you add concrete
# subclasses and instances.
if def_name == "new" || !(!owner.metaclass? && owner.abstract? && (owner.leaf? || owner.is_a?(GenericClassInstanceType)))
if def_name == "new" || !(!owner.metaclass? && owner.abstract_leaf?)
raise_matches_not_found(matches.owner || owner, def_name, arg_types, named_args_types, matches, with_literals: with_literals)
end
end
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ module Crystal
false
end

# Returns `true` if this type is abstract and and it has no
asterite marked this conversation as resolved.
Show resolved Hide resolved
# concrete subclasses. This can happen if this type has abstract
# subclasses, or non-abstract generic subclasses without instantiations.
def abstract_leaf?
type = self.devirtualize

case type
when GenericType
(type.abstract? && type.generic_types.empty?) ||
(type.generic_types.all? { |name, type| type.abstract_leaf? })
when GenericClassInstanceType
type.abstract? && type.subclasses.all?(&.abstract_leaf?)
when ClassType
type.abstract? && type.subclasses.all?(&.abstract_leaf?)
else
false
end
end

# Returns `true` if this type is a struct.
def struct?
false
Expand Down