Skip to content

Use a consistent definition for unreachable code in macro expansions. #1166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Testing/ExitTests/ExitTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ extension ExitTest {
asTypeAt typeAddress: UnsafeRawPointer,
withHintAt hintAddress: UnsafeRawPointer? = nil
) -> CBool {
fatalError("Unimplemented")
swt_unreachable()
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/TestingMacros/ConditionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,9 @@ extension ExitTestConditionMacro {
// early if found.
guard _diagnoseIssues(with: macro, body: bodyArgumentExpr, in: context) else {
if Self.isThrowing {
return #"{ () async throws -> Testing.ExitTest.Result in Swift.fatalError("Unreachable") }()"#
return #"{ () async throws -> Testing.ExitTest.Result in \#(ExprSyntax.unreachable) }()"#
} else {
return #"{ () async -> Testing.ExitTest.Result in Swift.fatalError("Unreachable") }()"#
return #"{ () async -> Testing.ExitTest.Result in \#(ExprSyntax.unreachable) }()"#
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/TestingMacros/ExitTestCapturedValueMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public struct ExitTestBadCapturedValueMacro: ExpressionMacro, Sendable {
// Diagnose that the type of 'expr' is invalid.
context.diagnose(.capturedValueMustBeSendableAndCodable(expr, name: nameExpr))

return #"Swift.fatalError("Unsupported")"#
return .unreachable
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,15 @@ extension FunctionParameterSyntax {
return baseType.trimmedDescription
}
}

// MARK: -

extension ExprSyntax {
/// An expression representing an unreachable code path.
///
/// Use this expression when a macro will emit an error diagnostic but the
/// compiler still requires us to produce a valid expression.
static var unreachable: Self {
#"Swift.fatalError("Unreachable")"#
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct CapturedValueInfo {

init(_ capture: ClosureCaptureSyntax, in context: some MacroExpansionContext) {
self.capture = capture
self.expression = #"Swift.fatalError("Unsupported")"#
self.expression = .unreachable
self.type = "Swift.Never"

// We don't support capture specifiers at this time.
Expand Down
2 changes: 1 addition & 1 deletion Sources/TestingMacros/TagMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct TagMacro: PeerMacro, AccessorMacro, Sendable {
/// This property is used rather than simply returning the empty array in
/// order to suppress a compiler diagnostic about not producing any accessors.
private static var _fallbackAccessorDecls: [AccessorDeclSyntax] {
[#"get { Swift.fatalError("Unreachable") }"#]
[#"get { \#(ExprSyntax.unreachable) }"#]
}

public static func expansion(
Expand Down