Skip to content

Commit

Permalink
copy values on return
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Aug 28, 2020
1 parent 82669f5 commit 25897c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,8 @@ func (interpreter *Interpreter) VisitReturnStatement(statement *ast.ReturnStatem
valueType := interpreter.Checker.Elaboration.ReturnStatementValueTypes[statement]
returnType := interpreter.Checker.Elaboration.ReturnStatementReturnTypes[statement]

value = interpreter.convertAndBox(value, valueType, returnType)
// NOTE: copy on return
value = interpreter.copyAndConvert(value, valueType, returnType)

return functionReturn{value}
})
Expand Down
28 changes: 28 additions & 0 deletions runtime/tests/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8175,3 +8175,31 @@ func TestInterpretInternalAssignment(t *testing.T) {
value,
)
}

func TestInterpretCopyOnReturn(t *testing.T) {

t.Parallel()

inter := parseCheckAndInterpret(t,
`
let xs: {String: String} = {}
fun returnXS(): {String: String} {
return xs
}
fun test(): {String: String} {
returnXS().insert(key: "foo", "bar")
return xs
}
`,
)

value, err := inter.Invoke("test")
require.NoError(t, err)

assert.Equal(t,
interpreter.NewDictionaryValueUnownedNonCopying(),
value,
)
}

0 comments on commit 25897c7

Please sign in to comment.