Skip to content

Commit 3965d35

Browse files
committed
Make __ExpectationContext move-only
1 parent 47bf7e8 commit 3965d35

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

Sources/Testing/Expectations/ExpectationContext.swift

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private import _TestingInternals
2020
///
2121
/// - Warning: This type is used to implement the `#expect()` and `#require()`
2222
/// macros. Do not use it directly.
23-
public struct __ExpectationContext {
23+
public struct __ExpectationContext: ~Copyable {
2424
/// The source code of any captured expressions.
2525
var sourceCode: [__ExpressionID: String]
2626

@@ -49,7 +49,7 @@ public struct __ExpectationContext {
4949
/// - Returns: An array of expressions under the root node of
5050
/// `expressionGraph`. The expression at the root of the graph is not
5151
/// included in the result.
52-
private func _squashExpressionGraph(_ expressionGraph: Graph<UInt32, __Expression?>, depth: Int) -> [__Expression] {
52+
private borrowing func _squashExpressionGraph(_ expressionGraph: Graph<UInt32, __Expression?>, depth: Int) -> [__Expression] {
5353
var result = [__Expression]()
5454

5555
let childGraphs = expressionGraph.children.sorted { $0.key < $1.key }
@@ -80,7 +80,10 @@ public struct __ExpectationContext {
8080
///
8181
/// - Returns: An expression value representing the condition expression that
8282
/// was evaluated.
83-
consuming func finalize(successfully: Bool) -> __Expression {
83+
///
84+
/// This function should ideally be `consuming`, but because it is used in a
85+
/// `lazy var` declaration, the compiler currently disallows it.
86+
borrowing func finalize(successfully: Bool) -> __Expression {
8487
// Construct a graph containing the source code for all the subexpressions
8588
// we've captured during evaluation.
8689
var expressionGraph = Graph<UInt32, __Expression?>()
@@ -123,7 +126,13 @@ public struct __ExpectationContext {
123126

124127
#if !SWT_FIXED_122011759
125128
/// Storage for any locally-created C strings.
126-
private var _transformedCStrings: _TransformedCStrings?
129+
private var _transformedCStrings = [UnsafeMutablePointer<CChar>]()
130+
131+
deinit {
132+
for cString in _transformedCStrings {
133+
free(cString)
134+
}
135+
}
127136
#endif
128137
}
129138

@@ -227,19 +236,6 @@ extension __ExpectationContext {
227236
// MARK: - String-to-C-string handling
228237

229238
extension __ExpectationContext {
230-
/// A class that manages the lifetimes of any temporary C strings created in
231-
/// the context of an expectation.
232-
private final class _TransformedCStrings {
233-
/// The set of temporary C strings managed by this instance.
234-
var values = [UnsafeMutablePointer<CChar>]()
235-
236-
deinit {
237-
for cString in values {
238-
free(cString)
239-
}
240-
}
241-
}
242-
243239
/// Convert a string to a C string and capture information about it for use if
244240
/// the expectation currently being evaluated fails.
245241
///
@@ -275,10 +271,10 @@ extension __ExpectationContext {
275271

276272
// Store the C string pointer so we can free it later when this context is
277273
// torn down.
278-
if _transformedCStrings == nil {
279-
_transformedCStrings = _TransformedCStrings()
274+
if _transformedCStrings.capacity == 0 {
275+
_transformedCStrings.reserveCapacity(2)
280276
}
281-
_transformedCStrings?.values.append(resultCString)
277+
_transformedCStrings.append(resultCString)
282278

283279
// Return the C string as whatever pointer type the caller wants.
284280
return U(bitPattern: Int(bitPattern: resultCString)).unsafelyUnwrapped

Sources/TestingMacros/Support/ConditionArgumentParsing.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ private final class _ContextInserter<C, M>: SyntaxRewriter where C: MacroExpansi
318318
override func visit(_ node: InfixOperatorExprSyntax) -> ExprSyntax {
319319
_rewrite(
320320
node
321-
.with(\.leftOperand, visit(node.leftOperand).trimmed)
322-
.with(\.rightOperand, visit(node.rightOperand).trimmed),
321+
.with(\.leftOperand, visit(node.leftOperand))
322+
.with(\.rightOperand, visit(node.rightOperand)),
323323
originalWas: node
324324
)
325325
}

0 commit comments

Comments
 (0)