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: inline ivar access for virtual call with a single child #12321

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
32 changes: 32 additions & 0 deletions spec/compiler/interpreter/classes_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,36 @@ describe Crystal::Repl::Interpreter do
CODE
end
end

it "inlines instance var access from virtual type with a single type (#39520)" do
interpret(<<-CODE).should eq(1)
struct Int32
def foo
1
end
end

struct Char
def foo
2
end
end

abstract class Expression
end

class ValueExpression < Expression
def initialize
@value = 1 || 'a'
end

def value
@value
end
end

expression = ValueExpression.new.as(Expression)
expression.value.foo
CODE
end
end
18 changes: 8 additions & 10 deletions src/compiler/crystal/interpreter/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1189,16 +1189,14 @@ class Crystal::Repl::Compiler < Crystal::Visitor
false
end

private def compile_pointerof_read_instance_var(obj, name)
type = obj.type

ivar = type.lookup_instance_var(name)
ivar_offset = ivar_offset(type, name)
private def compile_pointerof_read_instance_var(obj, obj_type, name)
ivar = obj_type.lookup_instance_var(name)
ivar_offset = ivar_offset(obj_type, name)
ivar_size = inner_sizeof_type(ivar)

# Get a pointer to the object
if type.passed_by_value?
compile_pointerof_node(obj, obj.type)
if obj_type.passed_by_value?
compile_pointerof_node(obj, obj_type)
else
request_value(obj)
end
Expand Down Expand Up @@ -1469,7 +1467,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
when ClassVar
compile_pointerof_class_var(node, exp)
when ReadInstanceVar
compile_pointerof_read_instance_var(exp.obj, exp.name)
compile_pointerof_read_instance_var(exp.obj, exp.obj.type, exp.name)
when Call
# lib external var
external = exp.dependencies.first.as(External)
Expand Down Expand Up @@ -2388,7 +2386,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
end

private def compile_pointerof_node(obj : ReadInstanceVar, owner : Type) : Nil
compile_pointerof_read_instance_var(obj.obj, obj.name)
compile_pointerof_read_instance_var(obj.obj, obj.obj.type, obj.name)
end

private def compile_pointerof_node(call : Call, owner : Type) : Nil
Expand Down Expand Up @@ -2427,7 +2425,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor
# Inline the call, so that it also works fine when wanting to
# take a pointer through things (this is how compiled Crystal works too
if call_obj
compile_pointerof_read_instance_var(call_obj, body.name)
compile_pointerof_read_instance_var(call_obj, target_def.owner, body.name)
else
compile_pointerof_ivar(body, body.name)
end
Expand Down