Skip to content

Commit a4c0226

Browse files
committed
More pointer finagling
1 parent 73b833f commit a4c0226

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

Sources/Testing/Expectations/ExpectationContext.swift

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ extension __ExpectationContext {
450450
/// - Warning: This function is used to implement the `#expect()` and
451451
/// `#require()` macros. Do not call it directly.
452452
@_disfavoredOverload
453-
public mutating func callAsFunction<PFrom, PTo>(_ value: PFrom, _ id: __ExpressionID) -> PTo where PFrom: _Pointer, PTo: _Pointer {
454-
self(value as PFrom?, id) as! PTo
453+
public mutating func callAsFunction<P, T>(_ value: P, _ id: __ExpressionID) -> UnsafePointer<T> where P: _Pointer, P.Pointee == T {
454+
self(value as P?, id)!
455455
}
456456

457457
/// Convert some pointer to an immutable one and capture information about it
@@ -470,9 +470,51 @@ extension __ExpectationContext {
470470
/// - Warning: This function is used to implement the `#expect()` and
471471
/// `#require()` macros. Do not call it directly.
472472
@_disfavoredOverload
473-
public mutating func callAsFunction<PFrom, PTo>(_ value: PFrom?, _ id: __ExpressionID) -> PTo? where PFrom: _Pointer, PTo: _Pointer {
473+
public mutating func callAsFunction<P, T>(_ value: P?, _ id: __ExpressionID) -> UnsafePointer<T>? where P: _Pointer, P.Pointee == T {
474474
value.flatMap { value in
475-
PTo(bitPattern: Int(bitPattern: self(value, id) as PFrom))
475+
UnsafePointer<T>(bitPattern: Int(bitPattern: self(value, id) as P))
476+
}
477+
}
478+
479+
/// Convert some pointer to an immutable one and capture information about it
480+
/// for use if the expectation currently being evaluated fails.
481+
///
482+
/// - Parameters:
483+
/// - value: The pointer to make immutable.
484+
/// - id: A value that uniquely identifies the represented expression in the
485+
/// context of the expectation currently being evaluated.
486+
///
487+
/// - Returns: `value`, cast to an immutable pointer.
488+
///
489+
/// This overload of `callAsFunction(_:_:)` handles the implicit conversions
490+
/// between various pointer types that are normally provided by the compiler.
491+
///
492+
/// - Warning: This function is used to implement the `#expect()` and
493+
/// `#require()` macros. Do not call it directly.
494+
@_disfavoredOverload
495+
public mutating func callAsFunction<P>(_ value: P, _ id: __ExpressionID) -> UnsafeRawPointer where P: _Pointer {
496+
self(value as P?, id)!
497+
}
498+
499+
/// Convert some pointer to an immutable one and capture information about it
500+
/// for use if the expectation currently being evaluated fails.
501+
///
502+
/// - Parameters:
503+
/// - value: The pointer to make immutable.
504+
/// - id: A value that uniquely identifies the represented expression in the
505+
/// context of the expectation currently being evaluated.
506+
///
507+
/// - Returns: `value`, cast to an immutable pointer.
508+
///
509+
/// This overload of `callAsFunction(_:_:)` handles the implicit conversions
510+
/// between various pointer types that are normally provided by the compiler.
511+
///
512+
/// - Warning: This function is used to implement the `#expect()` and
513+
/// `#require()` macros. Do not call it directly.
514+
@_disfavoredOverload
515+
public mutating func callAsFunction<P>(_ value: P?, _ id: __ExpressionID) -> UnsafeRawPointer? where P: _Pointer {
516+
value.flatMap { value in
517+
UnsafeRawPointer(bitPattern: Int(bitPattern: self(value, id) as P))
476518
}
477519
}
478520
}

Sources/TestingMacros/ConditionMacro.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,15 @@ extension ConditionMacro {
160160
expandedFunctionName = .identifier("__checkEscapedCondition")
161161

162162
} else {
163-
// BUG: We should use a unique name here. SEE: swift-syntax-#2256
164-
let expressionContextName = TokenSyntax.identifier("__ec")
163+
var expressionContextName = TokenSyntax.identifier("__ec")
164+
let isNameUsed = originalArgumentExpr.tokens(viewMode: .sourceAccurate).lazy
165+
.map(\.tokenKind)
166+
.contains(expressionContextName.tokenKind)
167+
if isNameUsed {
168+
// BUG: We should use the unique name directly. SEE: swift-syntax-#2256
169+
let uniqueName = context.makeUniqueName("")
170+
expressionContextName = .identifier("\(expressionContextName)\(uniqueName)")
171+
}
165172
let (rewrittenArgumentExpr, rewrittenNodes) = insertCalls(
166173
toExpressionContextNamed: expressionContextName,
167174
into: originalArgumentExpr,

Tests/TestingTests/MiscellaneousTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ struct MultiLineSuite {
261261
_ = try #require(x?[...].last)
262262
}
263263

264+
@Test(.hidden) func canHaveVariableNamed__ec() throws {
265+
let __ec = 1
266+
#expect(__ec == 1)
267+
}
268+
264269
@Suite("Miscellaneous tests")
265270
struct MiscellaneousTests {
266271
@Test("Free function's name")

0 commit comments

Comments
 (0)