Skip to content

Commit

Permalink
Interpreter: handle inlined call that returns self for structs (#12259)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Jul 12, 2022
1 parent 0953fda commit e116912
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
25 changes: 25 additions & 0 deletions spec/compiler/interpreter/structs_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -338,5 +338,30 @@ describe Crystal::Repl::Interpreter do
f.x + f.y + f.z
CODE
end

it "inlines struct method that returns self (#12253)" do
interpret(<<-CODE).should eq(42)
struct Foo
def initialize(@x : Int32)
end
def x
@x
end
def foo
me
end
def me
self
end
end
a = Foo.new(42)
b = a.foo
b.x
CODE
end
end
end
7 changes: 6 additions & 1 deletion src/compiler/crystal/interpreter/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,12 @@ class Crystal::Repl::Compiler < Crystal::Visitor
if obj
request_value(obj)
else
put_self(node: node)
if scope.struct? && scope.passed_by_value?
# Load the entire self from the pointer that's self
get_self_ivar 0, aligned_sizeof_type(scope), node: node
else
put_self(node: node)
end
end
end

Expand Down

0 comments on commit e116912

Please sign in to comment.