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

[SR-12942] In-Out documentation is misleading or wrong. #291

Open
swift-ci opened this issue Jun 5, 2020 · 1 comment
Open

[SR-12942] In-Out documentation is misleading or wrong. #291

swift-ci opened this issue Jun 5, 2020 · 1 comment

Comments

@swift-ci
Copy link

swift-ci commented Jun 5, 2020

Previous ID SR-12942
Radar None
Original Reporter erickpc (JIRA User)
Type Bug

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, Documentation
Assignee None
Priority Medium

md5: 40885081a51d49565b9dd89413fc2e4b

Issue Description:

The documentation here https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID545 states:

In-out parameters are passed as follows:

  1. When the function is called, the value of the argument is copied.

  2. In the body of the function, the copy is modified.

  3. When the function returns, the copy’s value is assigned to the original argument.

By reading that, if any changes are made to the argument outside of the function between. step 1 and 3, those changes would be lost when the function returns. The scenario I'm describing would be:

  1. Pass array a `[1, 2, 3]` as an In-Out argument to function `f()`

  2. While `f()` is executing, outside the function, `a` gets modified to add another value.

  3. At this point `a` is `[1, 2, 3, 4]`

  4. When `f()` finishes executing the copy of `a` copied back to the calling context, losing the last value 4.

That's the expected behavior according to the documentation. I have this test case showing it doesn't work that way. Down below the documentation mentions an "optimization" that "satisfies all of the requirements of the copy-in copy-out model while removing the overhead of copying." That last part is misleading. This "optimization" explains the behavior I'm seeing, but if I were to "Write your code using the model given by copy-in copy-out, without depending on the call-by-reference optimization" I would be surprised the system is not working as expected.

Attached is the test case

PS: The double quotes above are to show what's in the documentation verbatim.

@theblixguy
Copy link

cc @natecook1000 amartini (JIRA User)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@amartini51 amartini51 transferred this issue from apple/swift Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants