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

Interpreter: fix .class for modules and unions #12205

Merged
merged 1 commit into from
Jul 5, 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
29 changes: 29 additions & 0 deletions spec/compiler/interpreter/types_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,35 @@ describe Crystal::Repl::Interpreter do
CODE
end

it "interprets class for module type (#12203)" do
interpret(<<-CODE).should eq("A")
class Class
def name : String
{{ @type.name.stringify }}
end
end

module M
end

class E
def initialize(@base : M)
end
end

abstract class P
include M
end

class A < P
end

e = E.new(A.new)
base = e.@base
base.class.name
CODE
end

it "interprets crystal_type_id for nil" do
interpret("nil.crystal_type_id").should eq(0)
end
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/interpreter/instructions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1248,11 +1248,11 @@ require "./repl"
end,
},
put_metaclass: {
operands: [size : Int32, struct_type : Bool],
operands: [size : Int32, union_type : Bool],
push: true,
code: begin
type_id =
if struct_type
if union_type
(stack - size).as(Int32*).value
else
(stack - size).as(Void**).value.as(Int32*).value
Expand Down
12 changes: 9 additions & 3 deletions src/compiler/crystal/interpreter/primitives.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,19 @@ class Crystal::Repl::Compiler
pointer_add(inner_sizeof_type(obj.not_nil!.type.as(PointerInstanceType).element_type), node: node)
when "class"
obj = obj.not_nil!
type = obj.type
type = obj.type.remove_indirection

case type
when VirtualType
obj.accept self
return unless @wants_value

if type.is_a?(VirtualType)
put_metaclass aligned_sizeof_type(type), false, node: node
when UnionType
obj.accept self
return unless @wants_value

put_metaclass aligned_sizeof_type(type), type.struct?, node: node
put_metaclass aligned_sizeof_type(type), true, node: node
else
discard_value obj
return unless @wants_value
Expand Down