Skip to content

Commit b92851b

Browse files
committed
Reduce the number of (explicit) copies we need
1 parent da3ebda commit b92851b

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

Sources/Testing/Expectations/ExpectationContext.swift

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ extension __ExpectationContext {
167167
///
168168
/// This function helps overloads of `callAsFunction(_:_:)` disambiguate
169169
/// themselves and avoid accidental recursion.
170-
@usableFromInline func captureValue<T>(_ value: consuming T, _ id: __ExpressionID) -> T {
170+
@usableFromInline func captureValue<T>(_ value: borrowing T, _ id: __ExpressionID) -> T {
171171
let value = copy value
172172
runtimeValues[id] = { Expression.Value(reflecting: value) }
173173
return value
@@ -186,7 +186,7 @@ extension __ExpectationContext {
186186
/// - Warning: This function is used to implement the `#expect()` and
187187
/// `#require()` macros. Do not call it directly.
188188
@_disfavoredOverload
189-
@inlinable public func callAsFunction<T>(_ value: consuming T, _ id: __ExpressionID) -> T {
189+
@inlinable public func callAsFunction<T>(_ value: borrowing T, _ id: __ExpressionID) -> T {
190190
captureValue(value, id)
191191
}
192192

@@ -204,7 +204,7 @@ extension __ExpectationContext {
204204
/// - Warning: This function is used to implement the `#expect()` and
205205
/// `#require()` macros. Do not call it directly.
206206
@_disfavoredOverload
207-
public func callAsFunction<T>(_ value: consuming T, _ id: __ExpressionID) -> T where T: ~Copyable {
207+
public func callAsFunction<T>(_ value: borrowing T, _ id: __ExpressionID) -> T where T: ~Copyable {
208208
// TODO: add support for borrowing non-copyable expressions (need @lifetime)
209209
return value
210210
}
@@ -345,8 +345,6 @@ extension __ExpectationContext {
345345
_ rhs: borrowing U,
346346
_ rhsID: __ExpressionID
347347
) rethrows -> Bool {
348-
let lhs = copy lhs
349-
let rhs = copy rhs
350348
let result = try captureValue(op(captureValue(lhs, lhsID), captureValue(rhs, rhsID)), opID)
351349

352350
if !result {
@@ -378,8 +376,7 @@ extension __ExpectationContext {
378376
///
379377
/// - Warning: This function is used to implement the `#expect()` and
380378
/// `#require()` macros. Do not call it directly.
381-
@inlinable public func __as<T, U>(_ value: consuming T, _ valueID: __ExpressionID, _ type: U.Type, _ typeID: __ExpressionID) -> U? {
382-
let value = copy value
379+
@inlinable public func __as<T, U>(_ value: borrowing T, _ valueID: __ExpressionID, _ type: U.Type, _ typeID: __ExpressionID) -> U? {
383380
let result = captureValue(value, valueID) as? U
384381

385382
if result == nil {
@@ -409,14 +406,6 @@ extension __ExpectationContext {
409406
/// - Warning: This function is used to implement the `#expect()` and
410407
/// `#require()` macros. Do not call it directly.
411408
@inlinable public func __is<T, U>(_ value: borrowing T, _ valueID: __ExpressionID, _ type: U.Type, _ typeID: __ExpressionID) -> Bool {
412-
let value = copy value
413-
let result = captureValue(value, valueID) is U
414-
415-
if !result {
416-
let correctType = Swift.type(of: value as Any)
417-
_ = captureValue(correctType, typeID)
418-
}
419-
420-
return result
409+
__as(value, valueID, type, typeID) != nil
421410
}
422411
}

0 commit comments

Comments
 (0)