Skip to content

Commit

Permalink
Generate functions instead of anonymous closures to work around Swift…
Browse files Browse the repository at this point in the history
… Concurrency bug (#99)
  • Loading branch information
dfed committed Jul 7, 2024
1 parent c6da5d6 commit 90c8b98
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 99 deletions.
23 changes: 16 additions & 7 deletions Sources/SafeDICore/Generators/ScopeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ actor ScopeGenerator: CustomStringConvertible, Sendable {
"let \(property.asSource)"
}

// Ideally we would be able to use an anonymous closure rather than a named function here.
// Unfortunately, there's a bug in Swift Concurrency that prevents us from doing this: https://github.com/swiftlang/swift/issues/75003
let functionName = self.functionName(toBuild: property)
let functionDeclaration = if generatedProperties.isEmpty {
""
} else {
"""
func \(functionName)() -> \(concreteTypeName) {
\(generatedProperties.joined(separator: "\n"))
\(Self.standardIndent)\(generatedProperties.isEmpty ? "" : "return ")\(returnLineSansReturn)
}
"""
}
let returnLineSansReturn = if erasedToConcreteExistential {
"\(property.typeDescription.asSource)(\(returnLineSansReturn))"
} else {
Expand All @@ -253,14 +267,9 @@ actor ScopeGenerator: CustomStringConvertible, Sendable {
let initializer = if generatedProperties.isEmpty {
returnLineSansReturn
} else {
"""
{
\(generatedProperties.joined(separator: "\n"))
\(Self.standardIndent)\(generatedProperties.isEmpty ? "" : "return ")\(returnLineSansReturn)
}()
"""
"\(functionName)()"
}
return "\(propertyDeclaration) = \(initializer)\n"
return "\(functionDeclaration)\(propertyDeclaration) = \(initializer)\n"
}
case let .alias(property, fulfillingProperty, erasedToConcreteExistential):
if erasedToConcreteExistential {
Expand Down
Loading

0 comments on commit 90c8b98

Please sign in to comment.