GiumaSoft
(Giuseppe Mazzilli)
October 18, 2023, 11:49am
1
It seem that Xcode preview compiler fail correctly parse generic and produce a "Failed to build" error:
Proposed example:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
import CoreBluetooth
import SwiftUI
/// Not working in preview
struct NotWorkingView: View {
var body: some View {
InitialView<String>()
}
struct InitialView<S>: View where S: StringProtocol {
var body: some View {
NestedView()
}
struct NestedView: View {
var body: some View {
Text("Hello")
}
}
}
}
struct ContentView_PreviewProviders: PreviewProvider {
static var previews: some View {
NotWorkingView()
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^
investigating the SampleView.1.preview-thunk.swift
from ~/Library/Developer/Xcode/DerivedData/.../ directory, I found that compiler translate the above code into pre-compiled stage using typealias without considering the Generic
condition-
^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
extension NotWorking.InitialView.NestedView {
typealias InitialView = NotWorking.InitialView
typealias NestedView = NotWorking.InitialView.NestedView
@_dynamicReplacement(for: body) private var __preview__body: some View {
#sourceLocation(file: "/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift", line: 40)
Text(__designTimeString("#32812.[2].[1].[1].[0].property.[0].[0].arg[0].value", fallback: "Hello"))
#sourceLocation()
}
}
extension NotWorking.InitialView {
typealias InitialView = NotWorking.InitialView
typealias NestedView = NotWorking.InitialView.NestedView
@_dynamicReplacement(for: body) private var __preview__body: some View {
#sourceLocation(file: "/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift", line: 35)
NestedView()
#sourceLocation()
}
}
extension NotWorking {
@_dynamicReplacement(for: body) private var __preview__body: some View {
#sourceLocation(file: "/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift", line: 30)
InitialView<String>()
#sourceLocation()
}
}
...
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Should be considered a compiler bug or I missed something in my code?
GiumaSoft
(Giuseppe Mazzilli)
October 18, 2023, 12:01pm
2
Here the Preview reported error:
== PREVIEW UPDATE ERROR:
CompileDylibError: Failed to build SampleView.swift
Compiling failed: reference to generic type 'NotWorking.InitialView' requires arguments in <...>
/Users/giuseppe/Library/Developer/Xcode/DerivedData/MyPlayground-avscbrxoycipyhbsfadustywnrmd/Build/Intermediates.noindex/> Previews/MyPlayground/Intermediates.noindex/MyPlayground.build/Debug-iphonesimulator/MyPlayground.build/Objects-normal/x86_64/> SampleView.1.preview-thunk.swift:35:35: error: reference to generic type 'NotWorking.InitialView' requires arguments in <...>
typealias NestedView = NotWorking.InitialView.NestedView
^
<<#S: StringProtocol#>>
/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift:33:12: note: generic type 'InitialView' > declared here
struct InitialView<S>: View where S: StringProtocol {
^
/Users/giuseppe/Library/Developer/Xcode/DerivedData/MyPlayground-avscbrxoycipyhbsfadustywnrmd/Build/Intermediates.noindex/> Previews/MyPlayground/Intermediates.noindex/MyPlayground.build/Debug-iphonesimulator/MyPlayground.build/Objects-normal/x86_64/> SampleView.1.preview-thunk.swift:21:34: error: ambiguous type name 'NestedView' in 'NotWorking.InitialView'
extension NotWorking.InitialView.NestedView {
~~~~~~~~~~~~~~~~~~~~~~ ^
/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift:38:16: note: found candidate with type > 'NotWorking.InitialView.NestedView'
struct NestedView: View {
^
/Users/giuseppe/Library/Developer/Xcode/DerivedData/MyPlayground-avscbrxoycipyhbsfadustywnrmd/Build/Intermediates.noindex/> Previews/MyPlayground/Intermediates.noindex/MyPlayground.build/Debug-iphonesimulator/MyPlayground.build/Objects-normal/x86_64/> SampleView.1.preview-thunk.swift:35:11: note: found candidate with type '<<error type>>'
typealias NestedView = NotWorking.InitialView.NestedView
^
/Users/giuseppe/Library/Developer/Xcode/DerivedData/MyPlayground-avscbrxoycipyhbsfadustywnrmd/Build/Intermediates.noindex/> Previews/MyPlayground/Intermediates.noindex/MyPlayground.build/Debug-iphonesimulator/MyPlayground.build/Objects-normal/x86_64/> SampleView.1.preview-thunk.swift:23:47: error: ambiguous type name 'NestedView' in 'NotWorking.InitialView'
typealias NestedView = NotWorking.InitialView.NestedView
~~~~~~~~~~~~~~~~~~~~~~ ^
/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift:38:16: note: found candidate with type > 'NotWorking.InitialView.NestedView'
struct NestedView: View {
^
/Users/giuseppe/Library/Developer/Xcode/DerivedData/MyPlayground-avscbrxoycipyhbsfadustywnrmd/Build/Intermediates.noindex/> Previews/MyPlayground/Intermediates.noindex/MyPlayground.build/Debug-iphonesimulator/MyPlayground.build/Objects-normal/x86_64/> SampleView.1.preview-thunk.swift:35:11: note: found candidate with type '<<error type>>'
typealias NestedView = NotWorking.InitialView.NestedView
^
This is indeed a bug! Could you please file a report with this reproducer? (feedback.apple.com )
GiumaSoft
(Giuseppe Mazzilli)
October 18, 2023, 3:47pm
4