Skip to content

Commit

Permalink
Mark generate dateFormatter storage as nonisolated(unsafe)
Browse files Browse the repository at this point in the history
Same rules as elsewhere.
Also add the supporting AST generation infra.
  • Loading branch information
helje5 committed Apr 7, 2024
1 parent 3387585 commit 26e2c0d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 28 deletions.
82 changes: 60 additions & 22 deletions Plugins/Libraries/LighterCodeGenAST/Generation/GenStructures.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Created by Helge Heß.
// Copyright © 2022 ZeeZide GmbH.
// Copyright © 2022-2024 ZeeZide GmbH.
//

public extension CodeGenerator {
Expand All @@ -12,31 +12,69 @@ public extension CodeGenerator {
omitPublic : Bool = false)
{
assert(value.type != nil || value.value != nil)

func writeBody() {
if value.public && !omitPublic { append("public ") }
if `static` { append("static ") }
append(value.let ? "let " : "var ")
append(tickedWhenReserved(value.name))
if let type = value.type {
append(configuration.propertyTypeSeparator) // " : "
append(string(for: type))
}
if let value = value.value {
append(configuration.propertyValueSeparator) // " = "
append(string(for: value))
}
appendEOL()
}

func writePlain() {
if let ( major, minor ) = value.minimumSwiftVersion {
assert(major >= 5)
writeln("#if swift(>=\(major).\(minor))")
}

writePropertyComment(value.comment)
appendIndent()
writeBody()

if let ( major, minor ) = value.minimumSwiftVersion {
assert(major >= 5)
writeln("#if swift(>=\(major).\(minor))")
if let ( major, minor ) = value.minimumSwiftVersion {
assert(major >= 5)
writeln("#endif // swift(>=\(major).\(minor))")
}
}

writePropertyComment(value.comment)
appendIndent()
if value.public && !omitPublic { append("public ") }
if `static` { append("static ") }
append(value.let ? "let " : "var ")
append(tickedWhenReserved(value.name))
if let type = value.type {
append(configuration.propertyTypeSeparator) // " : "
append(string(for: type))
}
if let value = value.value {
append(configuration.propertyValueSeparator) // " = "
append(string(for: value))
}
appendEOL()
if value.nonIsolatedUnsafe {
if let ( major, minor ) = value.minimumSwiftVersion,
(major > 5) || (major >= 5 && minor >= 10)
{
writePlain()
}
else {
writeln("#if swift(>=5.10)")

if let ( major, minor ) = value.minimumSwiftVersion {
assert(major >= 5)
writeln("#endif // swift(>=\(major).\(minor))")
writePropertyComment(value.comment)
appendIndent()
append("nonisolated(unsafe) ")
writeBody()

if let ( major, minor ) = value.minimumSwiftVersion {
assert(major >= 5)
writeln("#elseif swift(>=\(major).\(minor))")
}
else {
writeln("#else")
}

writePropertyComment(value.comment)
appendIndent()
writeBody()
writeln("#endif")
}
}
else {
writePlain()
}
}

Expand Down
17 changes: 12 additions & 5 deletions Plugins/Libraries/LighterCodeGenAST/Nodes/Struct.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Created by Helge Heß.
// Copyright © 2022 ZeeZide GmbH.
// Copyright © 2022-2024 ZeeZide GmbH.
//

/**
Expand All @@ -11,8 +11,10 @@ public struct Struct {
/**
* An instance variable of a ``Struct``.
*/
public struct InstanceVariable {
public struct InstanceVariable: Sendable {

/// Whether the definition is `nonisolated(unsafe)`, requires Swift 5.10+
public var nonIsolatedUnsafe : Bool
/// Is the property public?
public var `public` : Bool
/// Is the property readonly.
Expand All @@ -31,7 +33,9 @@ public struct Struct {
public var minimumSwiftVersion : ( major: Int, minor: Int )?

/// Initialize a new instance variable node.
public init(public: Bool = true, `let`: Bool = true, _ name: String,
public init(nonIsolatedUnsafe: Bool = false,
public: Bool = true, `let`: Bool = true,
_ name: String,
type: TypeReference? = nil, value: Expression? = nil,
minimumSwiftVersion : ( major: Int, minor: Int )? = nil,
comment: String? = nil)
Expand All @@ -43,6 +47,7 @@ public struct Struct {
self.value = value
self.minimumSwiftVersion = minimumSwiftVersion
self.comment = comment
self.nonIsolatedUnsafe = nonIsolatedUnsafe
}
}

Expand Down Expand Up @@ -159,11 +164,13 @@ public extension Struct.InstanceVariable {
}

/// Initialize a new instance variable node for a `var`.
static func `var`(public: Bool = true, _ name: String, _ type: TypeReference,
static func `var`(nonIsolatedUnsafe: Bool = false,
public: Bool = true, _ name: String, _ type: TypeReference,
comment: String? = nil)
-> Self
{
.init(public: `public`, let: false, name, type: type, value: nil,
.init(nonIsolatedUnsafe: nonIsolatedUnsafe, public: `public`,
let: false, name, type: type, value: nil,
comment: comment)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ extension EnlighterASTGenerator {
let comment = "The `DateFormatter` used for parsing string date values."
if options.useLighter {
typeVariables.append(
.var(public: false, "_\(name)", type, comment: comment)
.var(nonIsolatedUnsafe: true,
public: false, "_\(name)", type, comment: comment)
)
computedTypeProperties.append(
.var(public: options.public, inlinable: false,
Expand Down
4 changes: 4 additions & 0 deletions Tests/EntityGenTests/ASTDatabaseStructGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ final class ASTDatabaseStructGenerationTests: XCTestCase {
//print("GOT:\n-----\n\(source)\n-----")

XCTAssertTrue(source.contains("static var _dateFormatter : DateFormatter?"))
XCTAssertTrue(source.contains(
"nonisolated(unsafe) static var _dateFormatter : DateFormatter?"))
XCTAssertTrue(source.contains("#if swift(>=5.10)"))

XCTAssertTrue(source.contains(
"public static var dateFormatter : DateFormatter? {"))
XCTAssertTrue(source.contains(
Expand Down

0 comments on commit 26e2c0d

Please sign in to comment.