Skip to content

Commit b081d41

Browse files
committed
Use a consistent definition for unreachable code in macro expansions.
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.)
1 parent e63d542 commit b081d41

File tree

6 files changed

+20
-6
lines changed

6 files changed

+20
-6
lines changed

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ extension ExitTest {
384384
asTypeAt typeAddress: UnsafeRawPointer,
385385
withHintAt hintAddress: UnsafeRawPointer? = nil
386386
) -> CBool {
387-
fatalError("Unimplemented")
387+
swt_unreachable()
388388
}
389389
}
390390

Sources/TestingMacros/ConditionMacro.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public import SwiftSyntax
1313
import SwiftSyntaxBuilder
1414
public import SwiftSyntaxMacros
1515

16+
private import MachO.dyld
17+
1618
#if !hasFeature(SymbolLinkageMarkers) && SWT_NO_LEGACY_TEST_DISCOVERY
1719
#error("Platform-specific misconfiguration: either SymbolLinkageMarkers or legacy test discovery is required to expand #expect(processExitsWith:)")
1820
#endif
@@ -441,9 +443,9 @@ extension ExitTestConditionMacro {
441443
// early if found.
442444
guard _diagnoseIssues(with: macro, body: bodyArgumentExpr, in: context) else {
443445
if Self.isThrowing {
444-
return #"{ () async throws -> Testing.ExitTest.Result in Swift.fatalError("Unreachable") }()"#
446+
return #"{ () async throws -> Testing.ExitTest.Result in \#(ExprSyntax.unreachable) }()"#
445447
} else {
446-
return #"{ () async -> Testing.ExitTest.Result in Swift.fatalError("Unreachable") }()"#
448+
return #"{ () async -> Testing.ExitTest.Result in \#(ExprSyntax.unreachable) }()"#
447449
}
448450
}
449451

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
@@ -178,3 +178,15 @@ extension FunctionParameterSyntax {
178178
return baseType.trimmedDescription
179179
}
180180
}
181+
182+
// MARK: -
183+
184+
extension ExprSyntax {
185+
/// An expression representing an unreachable code path.
186+
///
187+
/// Use this expression when a macro will emit an error diagnostic but the
188+
/// compiler still requires us to produce a valid expression.
189+
static var unreachable: Self {
190+
#"Swift.fatalError("Unreachable")"#
191+
}
192+
}

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)