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-944] Cannot use 'self' in RHS of && before all properties are initialized #43556

Open
swift-ci opened this issue Mar 14, 2016 · 3 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-944
Radar rdar://problem/39978220
Original Reporter dfirsht (JIRA User)
Type Bug
Environment

Apple Swift version 3.0-dev (LLVM b361b0fc05, Clang 11493b0f62, Swift 24a0c3d)
Target: x86_64-apple-macosx10.9
(03/01 snapshot)

Additional Detail from JIRA
Votes 6
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 60d55ae3d3ed11203ed60b6986237ce0

is duplicated by:

  • SR-4395 Incomplete initialization of self results in confusing error messages with @autoclosure functions
  • SR-7584 Seemingly incorrect "Constant 'self.X' captured before being initialized" compiler error in struct initializer
  • SR-7990 Using && on instance property in initializer results in confusing/incorrect error message
  • SR-11493 Can't use property of self before initialization when using the && operator

Issue Description:

When checking a conditional for an if statement in a initializer, specifying a member variable in the second or subsequent component will cause the compiler to complain that self was captured by a closure before all members were initialized.

The following code snippet demonstrates the problem:

class A {
    var a: Int?
    var b: Int
    init() {
        a = 4
        var c: String?
        if c == nil && a == nil {
            print("in")
        }
        b = 4
    }
}

Interestingly, switching the ordering of the c and a nil checks will cause the error to go away.

I created a snapshot of the issue in the IBM Sandbox: http://swiftlang.ng.bluemix.net/#/repl/fc77261825ff05d35c3b3dbde1259ed9c9c0366688b3dc1f87216ba6ec4e75ae

@belkadan
Copy link
Contributor

This is true since && is implemented using @autoclosure, but it's certainly suboptimal.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 2, 2019

Comment by Graham Perks (JIRA)

`||` is in the same boat, and optionals are not required. Quite confusing!

class A {
    var a: Int
    var b: Int
    init() {
        a = 4 // likely actually set to some init parameter

        if a == 4 {
            print("OK")
        }
        
        if a == 4 || a == 5 { // "self captured by a closure" 
            print("not OK")
        }
        
        b = 4
    }
}

@swift-ci
Copy link
Collaborator Author

Comment by Steven Van Impe (JIRA)

I ran into this issue myself just now.

Test case:

struct Test {
    
    let bool1: Bool
    let bool2: Bool
    
    init(value: Int, collection: [Int]) {
        bool1 = value > 0
        bool2 = !collection.contains(value) && !bool1
    }
}

The actual code in my initializer was:

userIsHost = activity.host == user
userIsPending = activity.pendingRegistrations.contains { $0.player == user }
userHasAutoApprove = !hostsUserHasJoined.contains(activity.host) && !userIsHost && !userIsPending

where all three properties are Bools.

I really wasn't expecting this to fail. The error confused me as well. I kept staring at my code, wondering where the closure was. I assumed I got the error because I was using a property to initialize another one, but the error didn't mention the previously initialized property (bool1 or userIsHost), only the current one (bool2 or userHasAutoApprove)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

2 participants