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

Fix codegen when instantiating class methods of typedefs #11636

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
18 changes: 18 additions & 0 deletions spec/compiler/codegen/pointer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,24 @@ describe "Code gen: pointer" do
))
end

it "passes arguments correctly for typedef metaclass (#8544)" do
run <<-CR
lib LibFoo
type Foo = Void*
end

class Class
def foo(x)
x
end
end

x = 1
LibFoo::Foo.foo(x)
Pointer(Void).foo(x)
CR
end

it "generates correct code for Pointer.malloc(0) (#2905)" do
run(%(
require "prelude"
Expand Down
10 changes: 8 additions & 2 deletions src/compiler/crystal/codegen/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ module Crystal
class Type
# Returns `true` if this type is passed as a `self` argument
# in the codegen phase. For example a method whose receiver is
# the Program, or a Metaclass, doesn't have a `self` argument.
# the Program, or a non-generic metaclass, doesn't have a `self` argument.
def passed_as_self?
case self
when Program, FileModule, LibType, MetaclassType
when Program, FileModule, LibType
false
when MetaclassType
# Given `type T = Void*`, `T.class` is not necessarily a non-generic
# metaclass, so we must resolve any typedefs here (we don't need to
# check for the cases above because `#remove_typedef` must return a
# metaclass)
!self.remove_typedef.is_a?(MetaclassType)
else
true
end
Expand Down