Compiling function generic over a protocol, "replaced function… could not be found" in Xcode

I'm not sure if I'm doing something wrong or if this is a bug (in Xcode?) I should file! I have a small SwiftUI function which is designed to be generic over its content:

func field<Content: View>(titled title: String, content: Content) -> some View {
    VStack(alignment: .leading) {
        Text(title).bold().foregroundColor(.gray)
        content
    }.padding()
}

This compiles, and it runs in the Simulator, but when I try to use the live preview, I get this error report (which makes me suspect an Xcode issue):

replaced function 'field(titled:content:)' of type '<τ_0_0 where τ_0_0 : View> (titled: String, content: τ_0_0) -> some View' could not be found

----------------------------------------

failedToBuildDylib: /Users/chris/Library/Developer/Xcode/DerivedData/rewrite-ferbskkukujftxgzlltnkibexrjz/Build/Intermediates.noindex/Previews/rewrite/Intermediates.noindex/rewrite.build/Debug-iphonesimulator/rewrite.build/Objects-normal/x86_64/ReferenceDetail.2.preview-thunk.swift:118:2: error: replaced function 'field(titled:content:)' of type '<τ_0_0 where τ_0_0 : View> (titled: String, content: τ_0_0) -> some View' could not be found
@_dynamicReplacement(for: field(titled:content:)) private func __preview__field<Content>(titled title: String, content: Content) -> some View  where Content: View {
 ^
<unknown>:0: note: found function 'field(titled:content:)' of type '<τ_0_0 where τ_0_0 : View> (titled: String, content: τ_0_0) -> some View'

I recognize, having done some poking around while working on this, that perhaps what I should be doing is letting the caller supply a closure there, the same way the SwiftUI building blocks do, so content: () -> Content instead of content: Content, and possibly making this a struct Field which is a View (and indeed, doing that latter bit works!), but I'm trying to understand why this would be giving me this error as well. Is this just an Xcode bug?

3 Likes