Skip to content

Commit f9fb1ec

Browse files
authored
Use a consistent definition for unreachable code in macro expansions. (#1166)
This PR defines a single `ExprSyntax` instance we can use for unreachable code paths in macro expansions (where the compiler/swift-syntax requires us to produce an expression but we know we're going to emit an error anyway.) ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent c3da189 commit f9fb1ec

File tree

6 files changed

+18
-6
lines changed

6 files changed

+18
-6
lines changed

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ extension ExitTest {
399399
asTypeAt typeAddress: UnsafeRawPointer,
400400
withHintAt hintAddress: UnsafeRawPointer? = nil
401401
) -> CBool {
402-
fatalError("Unimplemented")
402+
swt_unreachable()
403403
}
404404
}
405405

Sources/TestingMacros/ConditionMacro.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,9 @@ extension ExitTestConditionMacro {
454454
// early if found.
455455
guard _diagnoseIssues(with: macro, body: bodyArgumentExpr, in: context) else {
456456
if Self.isThrowing {
457-
return #"{ () async throws -> Testing.ExitTest.Result in Swift.fatalError("Unreachable") }()"#
457+
return #"{ () async throws -> Testing.ExitTest.Result in \#(ExprSyntax.unreachable) }()"#
458458
} else {
459-
return #"{ () async -> Testing.ExitTest.Result in Swift.fatalError("Unreachable") }()"#
459+
return #"{ () async -> Testing.ExitTest.Result in \#(ExprSyntax.unreachable) }()"#
460460
}
461461
}
462462

Sources/TestingMacros/ExitTestCapturedValueMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public struct ExitTestBadCapturedValueMacro: ExpressionMacro, Sendable {
5050
// Diagnose that the type of 'expr' is invalid.
5151
context.diagnose(.capturedValueMustBeSendableAndCodable(expr, name: nameExpr))
5252

53-
return #"Swift.fatalError("Unsupported")"#
53+
return .unreachable
5454
}
5555
}
5656

Sources/TestingMacros/Support/Additions/FunctionDeclSyntaxAdditions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,15 @@ extension FunctionParameterSyntax {
188188
return baseType.trimmedDescription
189189
}
190190
}
191+
192+
// MARK: -
193+
194+
extension ExprSyntax {
195+
/// An expression representing an unreachable code path.
196+
///
197+
/// Use this expression when a macro will emit an error diagnostic but the
198+
/// compiler still requires us to produce a valid expression.
199+
static var unreachable: Self {
200+
#"Swift.fatalError("Unreachable")"#
201+
}
202+
}

Sources/TestingMacros/Support/ClosureCaptureListParsing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct CapturedValueInfo {
4141

4242
init(_ capture: ClosureCaptureSyntax, in context: some MacroExpansionContext) {
4343
self.capture = capture
44-
self.expression = #"Swift.fatalError("Unsupported")"#
44+
self.expression = .unreachable
4545
self.type = "Swift.Never"
4646

4747
// We don't support capture specifiers at this time.

Sources/TestingMacros/TagMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public struct TagMacro: PeerMacro, AccessorMacro, Sendable {
2222
/// This property is used rather than simply returning the empty array in
2323
/// order to suppress a compiler diagnostic about not producing any accessors.
2424
private static var _fallbackAccessorDecls: [AccessorDeclSyntax] {
25-
[#"get { Swift.fatalError("Unreachable") }"#]
25+
[#"get { \#(ExprSyntax.unreachable) }"#]
2626
}
2727

2828
public static func expansion(

0 commit comments

Comments
 (0)