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-12430] self in Property Wrapper Using Enclosing Self Escapes Before Initialization Completed #54869

Closed
swift-ci opened this issue Mar 27, 2020 · 6 comments
Labels
accepts invalid Bug: Accepts invalid bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself property wrappers Feature: property wrappers

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12430
Radar None
Original Reporter briantkelley (JIRA User)
Type Bug
Status Closed
Resolution Done

Attachment: Download

Environment

macOS 10.15.4/Xcode 10.4

Also reproduces from trunk build at 3b3eade

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AcceptsInvalid, PropertyWrappers
Assignee briantkelley (JIRA)
Priority Medium

md5: b9f06e7a94b959281ac36fd796a2e6c3

duplicates:

Issue Description:

In the attached sample code, self can be used to access uninitialized memory.

init(value: String) {
    self.value = value
}

@Notifying({ (p: Publisher, l: Listener) in l.propertyChanged(p) })
var value: String = ""

Where @Notifying is a property wrapper that uses the static subscript to access the enclosing self.

As a work around, the user can explicitly insert super.init():

init(value: String) {
    super.init()
    self.value = value
}

The Swift compiler should diagnose this scenario and emit an error diagnostic.

@swift-ci
Copy link
Collaborator Author

Comment by Brian T. Kelley (JIRA)

It looks like there's a special case for property wrappers in ElementUseCollector::collectClassSelfUses():

if (auto *PAI = dyn_cast<PartialApplyInst>(User)) {
  if (onlyUsedByAssignByWrapper(PAI))
    continue;
  Kind = DIUseKind::Escape;
}

Where onlyUsedByAssignByWrapper() doesn't consider if the property wrapper is using the wrappedValue computed property or static subscript which access to self.

@swift-ci
Copy link
Collaborator Author

Comment by Brian T. Kelley (JIRA)

PR: #30715

@swift-ci
Copy link
Collaborator Author

Comment by Brian T. Kelley (JIRA)

The issue was fixed by 3aabdb5 in in PR #31248.

@swift-ci
Copy link
Collaborator Author

Comment by Brian T. Kelley (JIRA)

There are still some corner cases where the compiler accepts invalid code so I'm re-opening.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Sep 4, 2020

Comment by Brian T. Kelley (JIRA)

With the Swift compiler available in Xcode 12, Beta 6, I can no longer cause undefined behavior by accessing an object before it's initialized. However, now the class initializer does not differentiate between uninitialized and initialized so setting the property via the wrapper multiple times in an initializer can leak resources. That's a separate bug, however, so I'm closing this one.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Sep 4, 2020

Comment by Brian T. Kelley (JIRA)

New issue documented in SR-13495.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepts invalid Bug: Accepts invalid bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself property wrappers Feature: property wrappers
Projects
None yet
Development

No branches or pull requests

1 participant