Skip to content

Commit ba2499c

Browse files
cyangleysbaddaden
andauthored
Fix: interpreter handles self in inlined method with arguments (#16307)
We must handle args first, and then leave self on the stack, instead of handling args last and have them maybe mess with the stack. Co-authored-by: Julien Portalier <[email protected]>
1 parent a374dfc commit ba2499c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

spec/compiler/interpreter/bugs_spec.cr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,19 @@ describe Crystal::Repl::Interpreter do
210210
bar.is_a?(Bar)
211211
CRYSTAL
212212
end
213+
214+
it "handles self in inlined method with arguments (#16210)" do
215+
interpret(<<-CRYSTAL, prelude: "prelude").should eq(%("hello"))
216+
class Foo
217+
property x : String?
218+
219+
def initialize(@x : String? = nil)
220+
end
221+
end
222+
223+
foo = Foo.new("hello")
224+
foo.x.not_nil!("test")
225+
CRYSTAL
226+
end
213227
end
214228
end

src/compiler/crystal/interpreter/compiler.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,9 @@ class Crystal::Repl::Compiler < Crystal::Visitor
18721872
if body.is_a?(Var) && body.name == "self"
18731873
# We also inline calls that simply return "self"
18741874

1875+
# We still have to accept the call arguments, but discard their values
1876+
node.args.each { |arg| discard_value(arg) }
1877+
18751878
if @wants_value
18761879
if obj
18771880
request_value(obj)
@@ -1885,9 +1888,6 @@ class Crystal::Repl::Compiler < Crystal::Visitor
18851888
end
18861889
end
18871890

1888-
# We still have to accept the call arguments, but discard their values
1889-
node.args.each { |arg| discard_value(arg) }
1890-
18911891
return false
18921892
end
18931893

0 commit comments

Comments
 (0)