Skip to content

Commit 551a699

Browse files
committed
Simplify __ExpressionID a bit
1 parent 67d757e commit 551a699

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

Sources/Testing/ExitTests/ExitTest.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ func callExitTest(
294294

295295
// Plumb the exit test's result through the general expectation machinery.
296296
var expectationContext = __ExpectationContext()
297-
expectationContext.sourceCode[""] = sourceCode
298-
expectationContext.runtimeValues[""] = { Expression.Value(reflecting: actualExitCondition) }
297+
expectationContext.sourceCode[.root] = sourceCode
298+
expectationContext.runtimeValues[.root] = { Expression.Value(reflecting: actualExitCondition) }
299299
return check(
300300
expectedExitCondition == actualExitCondition,
301301
expectationContext: expectationContext,

Sources/Testing/Expectations/ExpectationChecking+Macro.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public func __checkEscapedCondition(
237237
sourceLocation: SourceLocation
238238
) -> Result<Void, any Error> {
239239
var expectationContext = __ExpectationContext()
240-
expectationContext.sourceCode[""] = sourceCode
240+
expectationContext.sourceCode[.root] = sourceCode
241241

242242
return check(
243243
condition,
@@ -266,7 +266,7 @@ public func __checkEscapedCondition<T>(
266266
sourceLocation: SourceLocation
267267
) -> Result<T, any Error> where T: ~Copyable {
268268
var expectationContext = __ExpectationContext()
269-
expectationContext.sourceCode[""] = sourceCode
269+
expectationContext.sourceCode[.root] = sourceCode
270270

271271
let result = check(
272272
optionalValue != nil,
@@ -393,7 +393,7 @@ public func __checkClosureCall(
393393
}
394394

395395
var expectationContext = __ExpectationContext()
396-
expectationContext.sourceCode[""] = sourceCode
396+
expectationContext.sourceCode[.root] = sourceCode
397397
return check(
398398
success,
399399
expectationContext: expectationContext,
@@ -433,7 +433,7 @@ public func __checkClosureCall(
433433
}
434434

435435
var expectationContext = __ExpectationContext()
436-
expectationContext.sourceCode[""] = sourceCode
436+
expectationContext.sourceCode[.root] = sourceCode
437437
return check(
438438
success,
439439
expectationContext: expectationContext,
@@ -519,7 +519,7 @@ public func __checkClosureCall<R>(
519519
sourceLocation: SourceLocation
520520
) -> Result<Void, any Error> {
521521
var expectationContext = __ExpectationContext()
522-
expectationContext.sourceCode[""] = sourceCode
522+
expectationContext.sourceCode[.root] = sourceCode
523523

524524
var errorMatches = false
525525
var mismatchExplanationValue: String? = nil
@@ -532,7 +532,7 @@ public func __checkClosureCall<R>(
532532
}
533533
mismatchExplanationValue = explanation
534534
} catch {
535-
expectationContext.runtimeValues[""] = { Expression.Value(reflecting: error) }
535+
expectationContext.runtimeValues[.root] = { Expression.Value(reflecting: error) }
536536
let secondError = Issue.withErrorRecording(at: sourceLocation) {
537537
errorMatches = try errorMatcher(error)
538538
}
@@ -571,7 +571,7 @@ public func __checkClosureCall<R>(
571571
sourceLocation: SourceLocation
572572
) async -> Result<Void, any Error> {
573573
var expectationContext = __ExpectationContext()
574-
expectationContext.sourceCode[""] = sourceCode
574+
expectationContext.sourceCode[.root] = sourceCode
575575

576576
var errorMatches = false
577577
var mismatchExplanationValue: String? = nil
@@ -584,7 +584,7 @@ public func __checkClosureCall<R>(
584584
}
585585
mismatchExplanationValue = explanation
586586
} catch {
587-
expectationContext.runtimeValues[""] = { Expression.Value(reflecting: error) }
587+
expectationContext.runtimeValues[.root] = { Expression.Value(reflecting: error) }
588588
let secondError = await Issue.withErrorRecording(at: sourceLocation) {
589589
errorMatches = try await errorMatcher(error)
590590
}

Sources/Testing/SourceAttribution/ExpressionID.swift

+5-17
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,18 @@
1717
/// - Warning: This type is used to implement the `#expect()` and `#require()`
1818
/// macros. Do not use it directly.
1919
public struct __ExpressionID: Sendable {
20+
/// The ID of the root node in an expression graph.
21+
static var root: Self {
22+
""
23+
}
24+
2025
/// The string produced at compile time that encodes the unique identifier of
2126
/// the represented expression.
2227
var stringValue: String
2328

2429
/// The number of bits in a nybble.
2530
private static var _bitsPerNybble: Int { 4 }
2631

27-
/// The ID of the syntax node represented by this instance.
28-
var nodeID: UInt32 {
29-
// The ID is encoded as the highest non-zero bit in the string, so that's
30-
// nybbles.first.highestBit + ((nybbles.count - 1) * bitsPerNybble)
31-
if stringValue.isEmpty {
32-
return 0
33-
}
34-
35-
let stringNybble = stringValue[stringValue.startIndex ... stringValue.startIndex]
36-
guard let nybble = UInt8(stringNybble, radix: 16) else {
37-
return 0
38-
}
39-
40-
let highestBit = UInt8.bitWidth - nybble.leadingZeroBitCount
41-
return UInt32(highestBit) + UInt32((stringValue.count - 1) * Self._bitsPerNybble)
42-
}
43-
4432
/// A representation of this instance suitable for use as a key path in an
4533
/// instance of `Graph` where the key type is `UInt32`.
4634
///

0 commit comments

Comments
 (0)