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-7584] Seemingly incorrect "Constant 'self.X' captured before being initialized" compiler error in struct initializer #50126

Closed
swift-ci opened this issue May 2, 2018 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself definite initialization

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 2, 2018

Previous ID SR-7584
Radar rdar://problem/39978220
Original Reporter marcpalmer (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode 9.3

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

md5: 60d14ae5dbd4b9021601b3eb3efd7a0d

duplicates:

  • SR-944 Cannot use 'self' in RHS of && before all properties are initialized

Issue Description:

I encountered this strange error with Swift 4.1.

public enum FeaturePrecondition: Hashable {
    case something
}

public enum SystemPermission: Hashable {
    case camera
}

public struct FeatureConstraints  {
    let preconditions: Set<FeaturePrecondition>
    let permissions: Set<SystemPermission>
    let isEmpty: Bool
    
    public init(preconditions: Set<FeaturePrecondition>, permissions: Set<SystemPermission>) {
        self.preconditions = preconditions
        self.permissions = permissions
        // This works...
        isEmpty = preconditions.isEmpty && permissions.isEmpty
    }

    public init(_ preconditions: Set<FeaturePrecondition>) {
        self.preconditions = preconditions
        permissions = []
        // This has compiler error "Constant 'self.isEmpty' captured by a closure before being initialized
        isEmpty = preconditions.isEmpty && permissions.isEmpty
        // This works however...
        // isEmpty = permissions.isEmpty
        // This also works...
        // isEmpty = preconditions.isEmpty
    }
}

Note that the line it chokes on is identical to the line in the preceding constructor, all that is different is the source of one of the sets. Individually the sets can be used, only when together in an expression is it a problem. Bizarre.

@belkadan
Copy link
Contributor

belkadan commented May 2, 2018

This is expected behavior, if not really desirable. In the first initializer, you're only accessing parameters; in the second, you're accessing properties of the not-yet-fully-initialized struct. Swift normally does let you access such properties as long as they're stored properties, but the problem is compounded by the presence of &&, which is implemented using a closure. Because of that, the access to (self.)permissions is treated as suspect.

@slavapestov, do you know if we have a dup for this?

@belkadan
Copy link
Contributor

belkadan commented May 4, 2018

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Steven Van Impe (JIRA)

@belkadan This is most likely a duplicate of https://bugs.swift.org/browse/SR-944? (I ran into this issue myself just now).

@belkadan
Copy link
Contributor

Ah, yep. Thanks, Steven!

@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself definite initialization
Projects
None yet
Development

No branches or pull requests

2 participants