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-15668] Initializer delegation allows 'let' properties to be reinitialized #57947

Open
WowbaggersLiquidLunch opened this issue Dec 30, 2021 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@WowbaggersLiquidLunch
Copy link
Collaborator

Previous ID SR-15668
Radar None
Original Reporter @WowbaggersLiquidLunch
Type Bug
Environment

macOS 12.2 Beta (21D5025f)
2021-12-23 trunk snapshot

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

md5: af38010f2cae0298846cbc04e355a443

Issue Description:

The following code currently produces no error:

struct S {
    let x: Int
}

extension S {
    init(a: Int) {
        self.init(x:  a); print(x)
        self.init(x: -a); print(x)
    }
}

let s = S(a: 100)
print(s.x)

// output:
// 100
// -100
// -100

The initializer in the extension above first initialises x to 100, then reinitialises it to -100, even though x is a let property.

In my opinion, this is an incorrect behaviour. Even if it is correct, it is inconsistent with reinitializing let properties directly:

extension S {
    init(b: Int) {
        x = b
        x = b // error: immutable value 'self.x' may only be initialized once
              // fix-it: change 'let' to 'var' to make it mutable
    }
}

extension S {
    init(c: Int) {
        self.init(x: a)
        x = a // error: 'let' property 'x' may not be initialized directly; use "self.init(...)" or "self = ..." instead
        self.init(x: -a)
    }
}
@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

1 participant