Skip to content
17 changes: 17 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,18 @@ let package = Package(
"Testing",
"_Testing_CoreGraphics",
"_Testing_Foundation",
"MemorySafeTestingTests",
],
swiftSettings: .packageSettings
),
.target(
name: "MemorySafeTestingTests",
dependencies: [
"Testing",
],
path: "Tests/_MemorySafeTestingTests",
swiftSettings: .packageSettings + .strictMemorySafety
),

.macro(
name: "TestingMacros",
Expand Down Expand Up @@ -355,6 +364,14 @@ extension Array where Element == PackageDescription.SwiftSetting {

return result
}

/// Settings necessary to enable Strict Memory Safety, introduced in
/// [SE-0458: Opt-in Strict Memory Safety Checking](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0458-strict-memory-safety.md#swiftpm-integration).
static var strictMemorySafety: Self {
// FIXME: Adopt official `.strictMemorySafety()` condition once the minimum
// supported toolchain is 6.2.
[.unsafeFlags(["-strict-memory-safety"])]
}
}

extension Array where Element == PackageDescription.CXXSetting {
Expand Down
4 changes: 2 additions & 2 deletions Sources/TestingMacros/ConditionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ extension ExitTestConditionMacro {
recordDecl = """
enum \(legacyEnumName): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
\(enumName).testContentRecord
unsafe \(enumName).testContentRecord
}
}
"""
Expand All @@ -510,7 +510,7 @@ extension ExitTestConditionMacro {
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName) {
private nonisolated static let accessor: Testing.__TestContentRecordAccessor = { outValue, type, hint, _ in
Testing.ExitTest.__store(
unsafe Testing.ExitTest.__store(
\(idExpr),
\(bodyThunkName),
into: outValue,
Expand Down
4 changes: 2 additions & 2 deletions Sources/TestingMacros/SuiteDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public struct SuiteDeclarationMacro: MemberMacro, PeerMacro, Sendable {
"""
@available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.")
private nonisolated static let \(accessorName): Testing.__TestContentRecordAccessor = { outValue, type, _, _ in
Testing.Test.__store(\(generatorName), into: outValue, asTypeAt: type)
unsafe Testing.Test.__store(\(generatorName), into: outValue, asTypeAt: type)
}
"""
)
Expand All @@ -174,7 +174,7 @@ public struct SuiteDeclarationMacro: MemberMacro, PeerMacro, Sendable {
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
\(testContentRecordName)
unsafe \(testContentRecordName)
}
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func makeTestContentRecordDecl(named name: TokenSyntax, in typeName: TypeSyntax?
private nonisolated \(staticKeyword(for: typeName)) let \(name): Testing.__TestContentRecord = (
\(kindExpr), \(kind.commentRepresentation)
0,
\(accessorName),
unsafe \(accessorName),
\(contextExpr),
0
)
Expand Down
4 changes: 2 additions & 2 deletions Sources/TestingMacros/TestDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
"""
@available(*, deprecated, message: "This property is an implementation detail of the testing library. Do not use it directly.")
private \(staticKeyword(for: typeName)) nonisolated let \(accessorName): Testing.__TestContentRecordAccessor = { outValue, type, _, _ in
Testing.Test.__store(\(generatorName), into: outValue, asTypeAt: type)
unsafe Testing.Test.__store(\(generatorName), into: outValue, asTypeAt: type)
}
"""
)
Expand All @@ -499,7 +499,7 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
\(testContentRecordName)
unsafe \(testContentRecordName)
}
}
"""
Expand Down
27 changes: 27 additions & 0 deletions Tests/_MemorySafeTestingTests/MemorySafeTestDecls.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

@testable import Testing

#if !hasFeature(StrictMemorySafety)
#error("This file requires strict memory safety to be enabled")
#endif

@Test(.hidden)
func exampleTestFunction() {}

@Suite(.hidden)
struct ExampleSuite {
@Test func example() {}
}

func exampleExitTest() async {
await #expect(processExitsWith: .success) {}
}