Skip to content

[5.9🍒] prevent reinitialization of self after discard #66352

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 8 commits into from
Jun 6, 2023

Conversation

kavon
Copy link
Member

@kavon kavon commented Jun 6, 2023

• Description: Adds a missing piece of SE-390 where we want to prevent reinitialization of self after discarding it, since it doesn't make sense to permit that and can limit our ability to implement discard more generally in the future.
• Risk: Medium. Will introduce a source break in functions that are using discard self and reinitialize self at some point reachable afterwards. (see test/SILOptimizer/discard_checking.swift for examples). The set of programs using discard is probably quite small currently and we'd want to introduce this change early, though the error could be downgraded into a warning for 5.9 upon request.
• Original PR: #66351
• Reviewed By: @gottesmm
• Testing: regression tests included
• Resolves: rdar://106098163

eeckstein and others added 8 commits June 5, 2023 19:39
his instruction is a marker for a following destroy instruction to suppress the call of the move-only type's deinitializer.
As part of SE-390, you're required to write either:

  - `consume self`
  - pass self as a `consuming` parameter to a function
  - `discard self`

before the function ends in a context that contains a
`discard self` somewhere. This prevents people from accidentally
invoking the deinit due to implicit destruction of `self` before
exiting the function.

rdar://106099027
(cherry picked from commit 88d35a0)
The value `self` is mutable (i.e., var-bound) in
a `consuming` method. Since you're allowed to
reinitialize a var after consuming, that means
you were also naturally allowed to reinitialize
self after `discard self`. But that capability was
not intended; after you discard self you shouldn't
be reinitializing it, as that's probably a mistake.

This change makes reinitialization of `self`
reachable from a `discard self` statement an error.

rdar://106098163
(cherry picked from commit bd253c6)
@kavon kavon requested a review from a team as a code owner June 6, 2023 02:46
@kavon
Copy link
Member Author

kavon commented Jun 6, 2023

I rebased this on top of #65449 since it depends on those changes. Only the last commit is new.

@kavon
Copy link
Member Author

kavon commented Jun 6, 2023

@swift-ci please test

@kavon
Copy link
Member Author

kavon commented Jun 6, 2023

@swift-ci clean test macOS platform

@kavon kavon merged commit 507bd39 into swiftlang:release/5.9 Jun 6, 2023
@kavon kavon deleted the 5.9-use-after-discard branch June 6, 2023 15:38
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.

4 participants