Segmentation fault: 11 when trying to resolve generic function with KeyPath

Originator:anton.siliuk
Number:rdar://50271944 Date Originated:April 27 2019
Status:Open Resolved:-
Product:Xcode, swift Product Version:10.2.1, 5.0
Classification:Bug Reproducible:Always
 
Summary:
In comment you can see an example of generic function with KeyPath. And Compiler fails to compile it.
This reproduces on lates swift 5 and on older swift 4.2 and Xcode 10.1

Can be fixed by making `label` a `var` property

Steps to Reproduce:
1. Open example project
2. Try to run it

Expected Results:
Example project compiles

Actual Results:
Compilation segfaults with error

Version/Build:
Xcode 10.2.1 swift 5

Comments

Code that produces compile error

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    Style<ViewController>.myStyle.apply(self)

    label.text = "Hello world"
    label.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
    ])
}

fileprivate let label = UILabel()

}

struct Style { let apply: (V) -> V }

extension Style where V: UILabel {

static func textColor(_ color: UIColor) -> Style {
    return Style { $0.textColor = color; return $0 }
}

}

extension Style {

static func substyle<V2: AnyObject>(_ keyPath: KeyPath<V, V2>, style: Style<V2>) -> Style {
    return Style { _ = style.apply($0[keyPath: keyPath]); return $0 }
}

}

extension Style where V: ViewController {

static var myStyle: Style {
    return substyle(\.label, style: .textColor(.red))
}

}

By anton.siliuk at April 27, 2019, 6:44 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!