Skip to content

[SR-2145] Incorrect fix-it application when correcting optionals as booleans in a ternary in a lazy-var initializer #44753

@swift-ci

Description

@swift-ci
Previous ID SR-2145
Radar None
Original Reporter fbartho (JIRA User)
Type Bug
Environment
  • Xcode: Version 8.0 beta 3 (8S174q)

  • Commandline tools: Xcode 8.0 (8S174q)

  • macOS: 10.11.6 (15G31)

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

md5: c713c996341946e49c76cdb770caa2f1

Issue Description:

1. Write a simple class that has optional member vars.
2. Add a lazy var, and prepare an initializer for a lazy member variable.
Example:

class Original {
    var opt1: String? = nil
    var opt2: String? = nil
    enum Foo : String {
        case unknown
        case one
        case two
    }
    lazy var type: Foo = {
        return self.opt1 ? .one
            : self.opt2 ? .two
            : .unknown
    }()
}

3. Notice that there's a compile error with a fix-it on the return line suggesting to test for != nil instead
4. Click the fix-it to apply the suggested correction:
This produces the following output:

    lazy var type: Foo = ({
        return self.opt1 ? .one
            : self.opt2 ? .two
            : .unknown
        }() != nil) /// Now a compiler error highlighting this line

Expected Result:

    lazy var type: Foo = {
        return (nil != self.opt1) ? .one
            : (nil != self.opt2) ? .two
            : .unknown
    }()

Reproduces in an iOS project & in a playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of Implementationgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions