|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +public import SwiftSyntax |
| 12 | +import SwiftSyntaxBuilder |
| 13 | +public import SwiftSyntaxMacros |
| 14 | + |
| 15 | +/// The implementation of the `#__capturedValue()` macro when the value conforms |
| 16 | +/// to the necessary protocols. |
| 17 | +/// |
| 18 | +/// This type is used to implement the `#__capturedValue()` macro. Do not use it |
| 19 | +/// directly. |
| 20 | +public struct ExitTestCapturedValueMacro: ExpressionMacro, Sendable { |
| 21 | + public static func expansion( |
| 22 | + of macro: some FreestandingMacroExpansionSyntax, |
| 23 | + in context: some MacroExpansionContext |
| 24 | + ) throws -> ExprSyntax { |
| 25 | + let arguments = Array(macro.arguments) |
| 26 | + let expr = arguments[0].expression |
| 27 | + |
| 28 | + // No additional processing is required as this expression's type meets our |
| 29 | + // requirements. |
| 30 | + |
| 31 | + return expr |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// The implementation of the `#__capturedValue()` macro when the value does |
| 36 | +/// _not_ conform to the necessary protocols. |
| 37 | +/// |
| 38 | +/// This type is used to implement the `#__capturedValue()` macro. Do not use it |
| 39 | +/// directly. |
| 40 | +public struct ExitTestBadCapturedValueMacro: ExpressionMacro, Sendable { |
| 41 | + public static func expansion( |
| 42 | + of macro: some FreestandingMacroExpansionSyntax, |
| 43 | + in context: some MacroExpansionContext |
| 44 | + ) throws -> ExprSyntax { |
| 45 | + let arguments = Array(macro.arguments) |
| 46 | + let expr = arguments[0].expression |
| 47 | + let nameExpr = arguments[1].expression.cast(StringLiteralExprSyntax.self) |
| 48 | + |
| 49 | + // Diagnose that the type of 'expr' is invalid. |
| 50 | + context.diagnose(.capturedValueMustBeSendableAndCodable(expr, name: nameExpr)) |
| 51 | + |
| 52 | + return #"Swift.fatalError("Unsupported")"# |
| 53 | + } |
| 54 | +} |
0 commit comments