Why is this ok …
let noCrash = controller.composer
let noCrashExtraStep = noCrash.document.title
But this is not? (Causes compiler crash — nonzero exit)
let crash = controller.composer.document.title
Is this a bug in the compiler or am I missing some logic about protocols and associated types?
Many thanks for any advice and also to the people who work on the Swift compiler.
Cheers
Ventura / Xcode 14.1 beta 2
import SwiftUI
// VIEWS
@main
struct CrashingApp: App {
@StateObject var controller = ComposerController(composer: ConcreteComposer())
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(controller)
}
}
}
struct ContentView: View {
@EnvironmentObject var controller: ComposerController
var body: some View {
// This line cannot be built without a crash
let crash = controller.composer.document.title
// If it's broken in two it works fine
let noCrash = controller.composer
let noCrashExtraStep = noCrash.document.title
Text(noCrashExtraStep)
}
}
// CONTROLLER
class ComposerController: ObservableObject {
var composer: any ComposerProtocol
init(composer: any ComposerProtocol) {self.composer = composer }
}
// PROTOCOLS
protocol ComposerProtocol {
associatedtype DocumentType: DocumentProtocol
var document: DocumentType {get set}
}
protocol DocumentProtocol {
var title: String { get set }
}
// CONCRETE TYPES
struct ConcreteComposer: ComposerProtocol {
init() {document = ConcreteDocument()}
var document: ConcreteDocument
}
struct ConcreteDocument: DocumentProtocol {
var title: String = "Title"
}