Skip to content

Commit d1997b2

Browse files
committed
Avoid 'capturing' accessor function references
1 parent 119d10f commit d1997b2

File tree

4 files changed

+17
-43
lines changed

4 files changed

+17
-43
lines changed

Sources/TestingMacros/ConditionMacro.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,15 @@ extension ExitTestConditionMacro {
487487
named: .identifier("testContentRecord"),
488488
in: TypeSyntax(IdentifierTypeSyntax(name: enumName)),
489489
ofKind: .exitTest,
490-
accessingWith: .identifier("accessor")
490+
accessingWith: """
491+
unsafe Testing.ExitTest.__store(
492+
\(idExpr),
493+
\(bodyThunkName),
494+
into: outValue,
495+
asTypeAt: type,
496+
withHintAt: hint
497+
)
498+
"""
491499
)
492500

493501
// Create another local type for legacy test discovery.
@@ -507,16 +515,6 @@ extension ExitTestConditionMacro {
507515
"""
508516
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
509517
enum \(enumName) {
510-
private nonisolated static let accessor: Testing.__TestContentRecordAccessor = { outValue, type, hint, _ in
511-
Testing.ExitTest.__store(
512-
\(idExpr),
513-
\(bodyThunkName),
514-
into: outValue,
515-
asTypeAt: type,
516-
withHintAt: hint
517-
)
518-
}
519-
520518
\(testContentRecordDecl)
521519
522520
\(recordDecl)

Sources/TestingMacros/SuiteDeclarationMacro.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,15 @@ public struct SuiteDeclarationMacro: MemberMacro, PeerMacro, Sendable {
141141
"""
142142
)
143143

144-
let accessorName = context.makeUniqueName("accessor")
145-
result.append(
146-
"""
147-
@available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.")
148-
private nonisolated static let \(accessorName): Testing.__TestContentRecordAccessor = { outValue, type, _, _ in
149-
Testing.Test.__store(\(generatorName), into: outValue, asTypeAt: type)
150-
}
151-
"""
152-
)
153-
154144
let testContentRecordName = context.makeUniqueName("testContentRecord")
155145
result.append(
156146
makeTestContentRecordDecl(
157147
named: testContentRecordName,
158148
in: declaration.type,
159149
ofKind: .testDeclaration,
160-
accessingWith: accessorName,
150+
accessingWith: """
151+
unsafe Testing.Test.__store(\(generatorName), into: outValue, asTypeAt: type)
152+
""",
161153
context: attributeInfo.testContentRecordFlags
162154
)
163155
)

Sources/TestingMacros/Support/TestContentGeneration.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,21 @@ enum TestContentKind: UInt32 {
4747
/// - typeName: The name of the type enclosing the resulting declaration, or
4848
/// `nil` if it will not be emitted into a type's scope.
4949
/// - kind: The kind of test content record being emitted.
50-
/// - accessorName: The Swift name of an `@convention(c)` function to emit
51-
/// into the resulting record.
50+
/// - accessorExpr: The accessor function call expression.
5251
/// - context: A value to emit as the `context` field of the test content
5352
/// record.
5453
///
5554
/// - Returns: A variable declaration that, when emitted into Swift source, will
5655
/// cause the linker to emit data in a location that is discoverable at
5756
/// runtime.
58-
func makeTestContentRecordDecl(named name: TokenSyntax, in typeName: TypeSyntax? = nil, ofKind kind: TestContentKind, accessingWith accessorName: TokenSyntax, context: UInt32 = 0) -> DeclSyntax {
57+
func makeTestContentRecordDecl(named name: TokenSyntax, in typeName: TypeSyntax? = nil, ofKind kind: TestContentKind, accessingWith accessorExpr: ExprSyntax, context: UInt32 = 0) -> DeclSyntax {
5958
let kindExpr = IntegerLiteralExprSyntax(kind.rawValue, radix: .hex)
6059
let contextExpr = if context == 0 {
6160
IntegerLiteralExprSyntax(0)
6261
} else {
6362
IntegerLiteralExprSyntax(context, radix: .binary)
6463
}
6564

66-
var accessorExpr: ExprSyntax = if let typeName {
67-
"\(typeName.trimmed).\(accessorName)"
68-
} else {
69-
"\(accessorName)"
70-
}
71-
accessorExpr = "{ unsafe \(accessorExpr)($0, $1, $2, $3) }"
72-
7365
var result: DeclSyntax = """
7466
@available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.")
7567
private nonisolated \(staticKeyword(for: typeName)) let \(name): Testing.__TestContentRecord = (

Sources/TestingMacros/TestDeclarationMacro.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -468,23 +468,15 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
468468
"""
469469
)
470470

471-
let accessorName = context.makeUniqueName(thunking: functionDecl, withPrefix: "accessor")
472-
result.append(
473-
"""
474-
@available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.")
475-
private \(staticKeyword(for: typeName)) nonisolated let \(accessorName): Testing.__TestContentRecordAccessor = { outValue, type, _, _ in
476-
Testing.Test.__store(\(generatorName), into: outValue, asTypeAt: type)
477-
}
478-
"""
479-
)
480-
481471
let testContentRecordName = context.makeUniqueName(thunking: functionDecl, withPrefix: "testContentRecord")
482472
result.append(
483473
makeTestContentRecordDecl(
484474
named: testContentRecordName,
485475
in: typeName,
486476
ofKind: .testDeclaration,
487-
accessingWith: accessorName,
477+
accessingWith: """
478+
unsafe Testing.Test.__store(\(generatorName), into: outValue, asTypeAt: type)
479+
""",
488480
context: attributeInfo.testContentRecordFlags
489481
)
490482
)

0 commit comments

Comments
 (0)