@@ -463,72 +463,9 @@ extension __ExpectationContext {
463463 ///
464464 /// - Warning: This function is used to implement the `#expect()` and
465465 /// `#require()` macros. Do not call it directly.
466- @_disfavoredOverload
467- public mutating func callAsFunction< P, T> ( _ value: P , _ id: __ExpressionID ) -> UnsafePointer < T > where P: _Pointer , P. Pointee == T {
468- self ( value as P ? , id) !
469- }
470-
471- /// Convert some pointer to an immutable one and capture information about it
472- /// for use if the expectation currently being evaluated fails.
473- ///
474- /// - Parameters:
475- /// - value: The pointer to make immutable.
476- /// - id: A value that uniquely identifies the represented expression in the
477- /// context of the expectation currently being evaluated.
478- ///
479- /// - Returns: `value`, cast to an immutable pointer.
480- ///
481- /// This overload of `callAsFunction(_:_:)` handles the implicit conversions
482- /// between various pointer types that are normally provided by the compiler.
483- ///
484- /// - Warning: This function is used to implement the `#expect()` and
485- /// `#require()` macros. Do not call it directly.
486- @_disfavoredOverload
487- public mutating func callAsFunction< P, T> ( _ value: P ? , _ id: __ExpressionID ) -> UnsafePointer < T > ? where P: _Pointer , P. Pointee == T {
488- value. flatMap { value in
489- UnsafePointer < T > ( bitPattern: Int ( bitPattern: self ( value, id) as P ) )
490- }
491- }
492-
493- /// Convert some pointer to an immutable one and capture information about it
494- /// for use if the expectation currently being evaluated fails.
495- ///
496- /// - Parameters:
497- /// - value: The pointer to make immutable.
498- /// - id: A value that uniquely identifies the represented expression in the
499- /// context of the expectation currently being evaluated.
500- ///
501- /// - Returns: `value`, cast to an immutable pointer.
502- ///
503- /// This overload of `callAsFunction(_:_:)` handles the implicit conversions
504- /// between various pointer types that are normally provided by the compiler.
505- ///
506- /// - Warning: This function is used to implement the `#expect()` and
507- /// `#require()` macros. Do not call it directly.
508- @_disfavoredOverload
509- public mutating func callAsFunction< P> ( _ value: P , _ id: __ExpressionID ) -> UnsafeRawPointer where P: _Pointer {
510- self ( value as P ? , id) !
511- }
512-
513- /// Convert some pointer to an immutable one and capture information about it
514- /// for use if the expectation currently being evaluated fails.
515- ///
516- /// - Parameters:
517- /// - value: The pointer to make immutable.
518- /// - id: A value that uniquely identifies the represented expression in the
519- /// context of the expectation currently being evaluated.
520- ///
521- /// - Returns: `value`, cast to an immutable pointer.
522- ///
523- /// This overload of `callAsFunction(_:_:)` handles the implicit conversions
524- /// between various pointer types that are normally provided by the compiler.
525- ///
526- /// - Warning: This function is used to implement the `#expect()` and
527- /// `#require()` macros. Do not call it directly.
528- @_disfavoredOverload
529- public mutating func callAsFunction< P> ( _ value: P ? , _ id: __ExpressionID ) -> UnsafeRawPointer ? where P: _Pointer {
530- value. flatMap { value in
531- UnsafeRawPointer ( bitPattern: Int ( bitPattern: self ( value, id) as P ) )
466+ public mutating func callAsFunction< P1, P2> ( _ value: P1 ? , _ id: __ExpressionID ) -> P2 ! where P1: _Pointer , P2: _Pointer {
467+ self ( value as P1 ? , id) . flatMap { value in
468+ P2 ( bitPattern: Int ( bitPattern: value) )
532469 }
533470 }
534471}
@@ -550,35 +487,50 @@ extension __ExpectationContext {
550487 /// context is destroyed.
551488 ///
552489 /// This overload of `callAsFunction(_:_:)` is necessary because Swift allows
553- /// passing string literals directly to functions that take C strings. At
554- /// compile time, the compiler generates code that makes a temporary UTF-8
555- /// copy of the string, then frees that copy on return. That logic does not
556- /// work correctly when strings are passed to intermediate functions such as
557- /// this one, and the compiler will fail to extend the lifetime of the C
558- /// strings to the appropriate point. ([122011759](rdar://122011759))
490+ /// passing string literals directly to functions that take C strings. The
491+ /// default overload of `callAsFunction(_:_:)` does not trigger this implicit
492+ /// cast and causes a compile-time error. ([122011759](rdar://122011759))
559493 ///
560494 /// - Warning: This function is used to implement the `#expect()` and
561495 /// `#require()` macros. Do not call it directly.
562- public mutating func callAsFunction< P> ( _ value: String , _ id: __ExpressionID ) -> P where P: _Pointer , P . Pointee == CChar {
496+ public mutating func callAsFunction< P> ( _ value: String , _ id: __ExpressionID ) -> P where P: _Pointer {
563497 // Perform the normal value capture.
564- let result = self ( value, id ) as String
498+ let value = self ( value as String , id )
565499
566500 // Create a C string copy of `value`.
501+ let valueCString = value. withCString { value in
567502#if os(Windows)
568- let resultCString = _strdup ( result ) !
503+ _strdup ( value )
569504#else
570- let resultCString = strdup ( result ) !
505+ strdup ( value )
571506#endif
507+ }
508+
509+ let result = valueCString. flatMap { valueCString in
510+ // Store the C string pointer so we can free it later when this context is
511+ // torn down.
512+ if _transformedCStrings. capacity == 0 {
513+ _transformedCStrings. reserveCapacity ( 2 )
514+ }
515+ _transformedCStrings. append ( valueCString)
572516
573- // Store the C string pointer so we can free it later when this context is
574- // torn down.
575- if _transformedCStrings. capacity == 0 {
576- _transformedCStrings. reserveCapacity ( 2 )
517+ // Return the C string as whatever pointer type the caller wants.
518+ return P ( bitPattern: Int ( bitPattern: valueCString) )
577519 }
578- _transformedCStrings. append ( resultCString)
579520
580- // Return the C string as whatever pointer type the caller wants.
581- return P ( bitPattern: Int ( bitPattern: resultCString) ) . unsafelyUnwrapped
521+ return result!
522+ }
523+
524+ /// Capture information about a value for use if the expectation currently
525+ /// being evaluated fails.
526+ ///
527+ /// This overload of `callAsFunction(_:_:)` helps the compiler disambiguate
528+ /// optional string values.
529+ ///
530+ /// - Warning: This function is used to implement the `#expect()` and
531+ /// `#require()` macros. Do not call it directly.
532+ public mutating func callAsFunction( _ value: String ? , _ id: __ExpressionID ) -> String ! {
533+ self ( value as String ? , id)
582534 }
583535}
584536#endif
0 commit comments