Skip to content

Commit e2574fb

Browse files
committed
Drop workaround for 122011759, we can just pass strings around
1 parent 6523aae commit e2574fb

File tree

1 file changed

+39
-54
lines changed

1 file changed

+39
-54
lines changed

Sources/Testing/Expectations/ExpectationContext.swift

+39-54
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,6 @@ public struct __ExpectationContext: ~Copyable {
143143

144144
return expression
145145
}
146-
147-
#if !SWT_FIXED_122011759
148-
/// Storage for any locally-created C strings.
149-
private var _transformedCStrings = [UnsafeMutablePointer<CChar>]()
150-
151-
deinit {
152-
for cString in _transformedCStrings {
153-
free(cString)
154-
}
155-
}
156-
#endif
157146
}
158147

159148
@available(*, unavailable)
@@ -464,15 +453,15 @@ extension __ExpectationContext {
464453
// MARK: - Implicit pointer conversion
465454

466455
extension __ExpectationContext {
467-
/// Convert some pointer to an immutable one and capture information about it
468-
/// for use if the expectation currently being evaluated fails.
456+
/// Convert some pointer to another pointer type and capture information about
457+
/// it for use if the expectation currently being evaluated fails.
469458
///
470459
/// - Parameters:
471-
/// - value: The pointer to make immutable.
460+
/// - value: The pointer to cast.
472461
/// - id: A value that uniquely identifies the represented expression in the
473462
/// context of the expectation currently being evaluated.
474463
///
475-
/// - Returns: `value`, cast to an immutable pointer.
464+
/// - Returns: `value`, cast to another type of pointer.
476465
///
477466
/// This overload of `callAsFunction(_:_:)` handles the implicit conversions
478467
/// between various pointer types that are normally provided by the compiler.
@@ -486,67 +475,63 @@ extension __ExpectationContext {
486475
}
487476
}
488477

489-
#if !SWT_FIXED_122011759
490478
// MARK: - String-to-C-string handling
491479

492480
extension __ExpectationContext {
493-
/// Convert a string to a C string and capture information about it for use if
494-
/// the expectation currently being evaluated fails.
481+
/// Capture information about a value for use if the expectation currently
482+
/// being evaluated fails.
495483
///
496484
/// - Parameters:
497-
/// - value: The string value that should be transformed into a C string.
485+
/// - value: The value to pass through.
498486
/// - id: A value that uniquely identifies the represented expression in the
499487
/// context of the expectation currently being evaluated.
500488
///
501-
/// - Returns: `value`, transformed into a pointer to a C string. The caller
502-
/// should _not_ free this string; it will be freed when the expectation
503-
/// context is destroyed.
489+
/// - Returns: `value`, verbatim.
504490
///
505-
/// This overload of `callAsFunction(_:_:)` is necessary because Swift allows
506-
/// passing string literals directly to functions that take C strings. The
507-
/// default overload of `callAsFunction(_:_:)` does not trigger this implicit
508-
/// cast and causes a compile-time error. ([122011759](rdar://122011759))
491+
/// This overload of `callAsFunction(_:_:)` helps the compiler disambiguate
492+
/// string values when they need to be implicitly cast to C strings.
509493
///
510494
/// - Warning: This function is used to implement the `#expect()` and
511495
/// `#require()` macros. Do not call it directly.
512-
public mutating func callAsFunction<P>(_ value: String, _ id: __ExpressionID) -> P where P: _Pointer {
513-
// Perform the normal value capture.
514-
let value = _captureValue(value, id)
515-
516-
// Create a C string copy of `value`.
517-
let valueCString = value.withCString { value in
518-
#if os(Windows)
519-
_strdup(value)
520-
#else
521-
strdup(value)
522-
#endif
523-
}
524-
525-
let result = valueCString.flatMap { valueCString in
526-
// Store the C string pointer so we can free it later when this context is
527-
// torn down.
528-
if _transformedCStrings.capacity == 0 {
529-
_transformedCStrings.reserveCapacity(2)
530-
}
531-
_transformedCStrings.append(valueCString)
532-
533-
// Return the C string as whatever pointer type the caller wants.
534-
return P(bitPattern: Int(bitPattern: valueCString))
535-
}
496+
public mutating func callAsFunction(_ value: String, _ id: __ExpressionID) -> String {
497+
_captureValue(value, id)
498+
}
536499

537-
return result!
500+
/// Capture information about a value for use if the expectation currently
501+
/// being evaluated fails.
502+
///
503+
/// - Parameters:
504+
/// - value: The value to pass through.
505+
/// - id: A value that uniquely identifies the represented expression in the
506+
/// context of the expectation currently being evaluated.
507+
///
508+
/// - Returns: An optional value containing a copy of `value`.
509+
///
510+
/// This overload of `callAsFunction(_:_:)` helps the compiler disambiguate
511+
/// string values when they need to be implicitly cast to C strings.
512+
///
513+
/// - Warning: This function is used to implement the `#expect()` and
514+
/// `#require()` macros. Do not call it directly.
515+
public mutating func callAsFunction(_ value: String, _ id: __ExpressionID) -> String? {
516+
_captureValue(value, id)
538517
}
539518

540519
/// Capture information about a value for use if the expectation currently
541520
/// being evaluated fails.
542521
///
522+
/// - Parameters:
523+
/// - value: The value to pass through.
524+
/// - id: A value that uniquely identifies the represented expression in the
525+
/// context of the expectation currently being evaluated.
526+
///
527+
/// - Returns: An optional value containing a copy of `value`.
528+
///
543529
/// This overload of `callAsFunction(_:_:)` helps the compiler disambiguate
544-
/// optional string values.
530+
/// string values when they need to be implicitly cast to C strings.
545531
///
546532
/// - Warning: This function is used to implement the `#expect()` and
547533
/// `#require()` macros. Do not call it directly.
548-
public mutating func callAsFunction(_ value: String?, _ id: __ExpressionID) -> String! {
534+
public mutating func callAsFunction(_ value: String?, _ id: __ExpressionID) -> String? {
549535
_captureValue(value, id)
550536
}
551537
}
552-
#endif

0 commit comments

Comments
 (0)