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-15669] Diagnostics for reinitialization of ‘let’ property due to initilizer delegation is misleading #57948

Open
WowbaggersLiquidLunch opened this issue Dec 30, 2021 · 0 comments

Comments

@WowbaggersLiquidLunch
Copy link
Collaborator

@WowbaggersLiquidLunch WowbaggersLiquidLunch commented Dec 30, 2021

Previous ID SR-15669
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, DiagnosticsQoI
Assignee None
Priority Medium

md5: cb0ffaf2974505a5d24b0f2262b94fe7

relates to:

  • SR-15670 'let' property reinitialization checking is flow-insensitive

Issue Description:

This is the current behaviour:

struct S {
    let x: Int
}

extension S {
    init(a: Int) {
        x = a // error: 'let' property ‘x’ may not be initialized directly; use "self.init(...)" or "self = ..." instead
        self.init(x: a)
    }
    
    init(b: Int) {
        self.init(x: b)
        x = b // error: 'let' property ‘x’ may not be initialized directly; use "self.init(...)" or "self = ..." instead
    }
}

This error message does not provide context, and misleads people to think that let properties may not be initialized directly with or without delegation.

I think the error message should be something like “immutable value 'self.x' may only be initialized once”, similar to the message given to direct reinitialization of let properties:

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

Or, even better if it marks where the property was/will be initialized via delegation:

extension S {
    init(d: Int) {
        x = d // error: immutable value 'self.x' may only be initialized once
        self.init(x: d) // info: immutable value 'self.x' will be reinitialized here
    }
    
    init(e: Int) {
        self.init(x: e) // info: immutable value 'self.x' has been initialized here
        x = e // error: immutable value 'self.x' may only be initialized once
    }
}
@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
Projects
None yet
Development

No branches or pull requests

1 participant