@@ -20,7 +20,7 @@ private import _TestingInternals
20
20
///
21
21
/// - Warning: This type is used to implement the `#expect()` and `#require()`
22
22
/// macros. Do not use it directly.
23
- public struct __ExpectationContext {
23
+ public struct __ExpectationContext : ~ Copyable {
24
24
/// The source code of any captured expressions.
25
25
var sourceCode : [ __ExpressionID : String ]
26
26
@@ -49,7 +49,7 @@ public struct __ExpectationContext {
49
49
/// - Returns: An array of expressions under the root node of
50
50
/// `expressionGraph`. The expression at the root of the graph is not
51
51
/// 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 ] {
53
53
var result = [ __Expression] ( )
54
54
55
55
let childGraphs = expressionGraph. children. sorted { $0. key < $1. key }
@@ -80,7 +80,10 @@ public struct __ExpectationContext {
80
80
///
81
81
/// - Returns: An expression value representing the condition expression that
82
82
/// 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 {
84
87
// Construct a graph containing the source code for all the subexpressions
85
88
// we've captured during evaluation.
86
89
var expressionGraph = Graph < UInt32 , __Expression ? > ( )
@@ -123,7 +126,13 @@ public struct __ExpectationContext {
123
126
124
127
#if !SWT_FIXED_122011759
125
128
/// 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
+ }
127
136
#endif
128
137
}
129
138
@@ -227,19 +236,6 @@ extension __ExpectationContext {
227
236
// MARK: - String-to-C-string handling
228
237
229
238
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
-
243
239
/// Convert a string to a C string and capture information about it for use if
244
240
/// the expectation currently being evaluated fails.
245
241
///
@@ -275,10 +271,10 @@ extension __ExpectationContext {
275
271
276
272
// Store the C string pointer so we can free it later when this context is
277
273
// torn down.
278
- if _transformedCStrings == nil {
279
- _transformedCStrings = _TransformedCStrings ( )
274
+ if _transformedCStrings. capacity == 0 {
275
+ _transformedCStrings. reserveCapacity ( 2 )
280
276
}
281
- _transformedCStrings? . values . append ( resultCString)
277
+ _transformedCStrings. append ( resultCString)
282
278
283
279
// Return the C string as whatever pointer type the caller wants.
284
280
return U ( bitPattern: Int ( bitPattern: resultCString) ) . unsafelyUnwrapped
0 commit comments