Skip to content

Commit

Permalink
Generate var getter for reuseIdentifier, fixes concurrentcy-safe error.
Browse files Browse the repository at this point in the history
With `let reuseIdentifier = reuseIdentifier()`, Swift 6 would generate this error:

    Let 'R' is not concurrency-safe because non-'Sendable' type '_R' may have shared mutable state

By changing to `var reuseIdentifier: reuseIdentifier { .init() }`, the error goes away.

To be extra safe, made two additional changes (that aren't strictly needed to fix the error):
 - changed all other top-level lets to vars,
 - changed the lets in the reuseIdentifier struct to vars
  • Loading branch information
tomlokhorst committed Sep 19, 2024
1 parent 1365194 commit 1965acc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Sources/RswiftGenerators/ReuseIdentifier+Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension Reusable {
groupedReusables.reportWarningsForDuplicatesAndEmpties(source: "reuseIdentifier", result: "reuse identifier", warning: warning)

let letbindings = groupedReusables.uniques
.map { $0.generateLetBinding() }
.map { $0.generateVarGetter() }
.sorted { $0.name < $1.name }

let comments = ["This `\(qualifiedName.value)` struct is generated, and contains static references to \(letbindings.count) reuse identifiers."]
Expand Down Expand Up @@ -82,8 +82,8 @@ extension Reusable {
)
}

func generateLetBinding() -> LetBinding {
LetBinding(
func generateVarGetter() -> VarGetter {
VarGetter(
comments: ["Reuse identifier `\(identifier)`."],
name: SwiftIdentifier(name: identifier),
typeReference: genericTypeReference,
Expand Down
6 changes: 3 additions & 3 deletions Sources/RswiftGenerators/StringsTable+Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import RswiftResources


extension Struct {
public func generateBundleVarGetterForString(name: String) -> VarGetter {
public func generateBundleVarGetterForString() -> VarGetter {
VarGetter(
deploymentTarget: deploymentTarget,
name: SwiftIdentifier(name: name),
typeReference: TypeReference(module: .host, rawName: self.name.value),
name: name,
typeReference: TypeReference(module: .host, rawName: name.value),
valueCodeString: ".init(bundle: bundle, preferredLanguages: nil, locale: nil)"
)
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/RswiftGenerators/SwiftSyntax/Struct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,15 @@ extension Struct {
)
}

public func generateVarGetter() -> VarGetter {
VarGetter(
deploymentTarget: deploymentTarget,
name: name,
typeReference: TypeReference(module: .host, rawName: self.name.value),
valueCodeString: ".init()"
)
}

public func generateBundleVarGetter(name: String) -> VarGetter {
VarGetter(
deploymentTarget: deploymentTarget,
Expand Down
10 changes: 5 additions & 5 deletions Sources/rswift/RswiftCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public struct RswiftCore {
}

if generators.contains(.string), !stringStruct.isEmpty {
stringStruct.generateBundleVarGetterForString(name: "string")
stringStruct.generateBundleVarGetterForString()
stringStruct.generateBundleFunctionForString(name: "string")
stringStruct.generateLocaleFunctionForString(name: "string")
stringStruct.generatePreferredLanguagesFunctionForString(name: "string")
Expand Down Expand Up @@ -241,7 +241,7 @@ public struct RswiftCore {
}

if generators.contains(.entitlements), !entitlementsStruct.isEmpty {
entitlementsStruct.generateLetBinding()
entitlementsStruct.generateVarGetter()
entitlementsStruct
}

Expand All @@ -258,12 +258,12 @@ public struct RswiftCore {
}

if generators.contains(.segue), !segueStruct.isEmpty {
segueStruct.generateLetBinding()
segueStruct.generateVarGetter()
segueStruct
}

if generators.contains(.id), !idStruct.isEmpty {
idStruct.generateLetBinding()
idStruct.generateVarGetter()
idStruct
}

Expand All @@ -274,7 +274,7 @@ public struct RswiftCore {
}

if generators.contains(.reuseIdentifier), !reuseIdentifierStruct.isEmpty {
reuseIdentifierStruct.generateLetBinding()
reuseIdentifierStruct.generateVarGetter()
reuseIdentifierStruct
}

Expand Down

0 comments on commit 1965acc

Please sign in to comment.