Skip to content

prevent reinitialization of self after discard #66351

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 1 commit into from
Jun 6, 2023

Conversation

kavon
Copy link
Member

@kavon kavon commented Jun 6, 2023

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

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
@kavon
Copy link
Member Author

kavon commented Jun 6, 2023

@swift-ci please smoke test

@kavon kavon marked this pull request as ready for review June 6, 2023 02:31
@kavon kavon requested review from jckarter, gottesmm and atrick June 6, 2023 02:31
Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

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

LGTM. All my comments are little things for future reference.

if (dropDeinits.empty() || reinits.empty())
return;

using BasicBlockMap = llvm::DenseMap<SILBasicBlock *,
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't worry about making this change in this patch. But for future reference you can use a Small FrozenMultiMap in situations like this. It is less heavy weight than an llvm DenseMap (which always heap allocates 64 entries). A small frozen multi-map is an array that you initialize with key value pairs that is when it is frozen stable sorted by the first element. So you get a map from Key -> [Value] and it can be in small form.

Copy link
Contributor

Choose a reason for hiding this comment

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

@kavon here is a link to FrozenMultiMap for your perusal.

swift/include/swift/Basic/FrozenMultiMap.h
Line 15 in

@kavon kavon merged commit c368418 into swiftlang:main Jun 6, 2023
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.

2 participants