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 crystal_type_id for virtual metaclass type #12246

Merged
merged 1 commit into from
Jul 11, 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
13 changes: 13 additions & 0 deletions spec/compiler/interpreter/types_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ describe Crystal::Repl::Interpreter do
repl_value.value.should eq(context.type_id(context.program.int32))
end

it "interprets crystal_type_id for virtual metaclass type (#12228)" do
interpret(<<-CODE).should eq(true)
class P
end

class A < P
end

p = A.as(P.class)
p.crystal_type_id == A.crystal_type_id
CODE
end

it "interprets class_crystal_instance_type_id" do
interpret(<<-CODE, prelude: "prelude").should eq("true")
class Foo
Expand Down
23 changes: 15 additions & 8 deletions src/compiler/crystal/interpreter/primitives.cr
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,24 @@ class Crystal::Repl::Compiler
put_type type, node: node
end
when "object_crystal_type_id"
type =
type = obj.try(&.type) || scope

unless @wants_value
discard_value obj if obj
return
end

if type.is_a?(VirtualMetaclassType)
# For a virtual metaclass type, the value is already an int
# that's exactly the crystal_type_id, so there's nothing else to do.
if obj
discard_value(obj)
obj.type
obj.accept self
else
scope
put_self node: node
end

return unless @wants_value

put_i32 type_id(type), node: node
else
put_i32 type_id(type), node: node
end
when "class_crystal_instance_type_id"
type =
if obj
Expand Down