Skip to content

[5.9][move-only] Ban partial reinitialization after consuming a value. #67146

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

Merged
merged 2 commits into from
Jul 6, 2023

Conversation

gottesmm
Copy link
Contributor

@gottesmm gottesmm commented Jul 6, 2023

• Description: [5.9][move-only] Ban partial reinitialization after consuming a value. The language impact of this is that we want to be sure that partially reinitializing after consuming a value, results in an error:

struct X : ~Copyable { var i = 5, var i2 = Klass() }
var x = X()
_ = consume x
x.i = 5 // Error! Can only fully init

The reason why we want to do this is that the original proposal said values were never partially initialized. We already have banned partial consumption, but we realized that this was another way to create a partially alive value. Given that, we want to go through a round of forum discussion at least before we put this out there. This closes the hole in the mean time.
• Risk: This is low risk since it only affects a corner case in a specific part of the noncopyable checker. So it cannot affect anything but noncopyable code.
• Original PR: #67145
• Reviewed By: @jckarter
• Testing: Added Swift tests
• Resolves: rdar://111498740

@gottesmm gottesmm requested a review from a team as a code owner July 6, 2023 01:55
@gottesmm
Copy link
Contributor Author

gottesmm commented Jul 6, 2023

Original: #67145

@gottesmm
Copy link
Contributor Author

gottesmm commented Jul 6, 2023

@swift-ci test

Copy link
Contributor

@jckarter jckarter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. There's a decent amount of new code, but it's in service to the new diagnostic, so it should be self-contained. We do want this diagnostic so we don't end up having to support code that does partial initializations before we hammer the semantics out.

gottesmm added 2 commits July 5, 2023 20:28
This is similar to our ban on partial consuming a value for this release. The
reason for this is that, one can achieve a similar affect as partial consumption
via a consumption of the entire value and then a partial reinitialization. Example:

```swift
struct X : ~Copyable { var i = 5, var i2 = Klass() }
var x = X()
_ = consume x
x.i = 5
```

in the case above, we now have a value that is in a partially initialized state.

We still allow for move only types to have their fields initialized as long as
there is an intervening init.

rdar://111498740
(cherry picked from commit 69a03c9)
@gottesmm gottesmm force-pushed the release-5.9-111498740 branch from 1889f6c to aab189d Compare July 6, 2023 03:28
@gottesmm
Copy link
Contributor Author

gottesmm commented Jul 6, 2023

@swift-ci test

@gottesmm gottesmm merged commit efe20be into swiftlang:release/5.9 Jul 6, 2023
@gottesmm gottesmm deleted the release-5.9-111498740 branch July 6, 2023 12:24
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

Successfully merging this pull request may close these issues.

3 participants