@@ -39,6 +39,12 @@ public import SwiftSyntaxMacros
3939public protocol ConditionMacro : ExpressionMacro , Sendable {
4040 /// Whether or not the macro's expansion may throw an error.
4141 static var isThrowing : Bool { get }
42+
43+ /// The return type of the expansion's closure, if it can be statically
44+ /// determined.
45+ ///
46+ /// This property is ignored when a condition macro is closure-based.
47+ static var returnType : TypeSyntax ? { get }
4248}
4349
4450// MARK: -
@@ -67,6 +73,15 @@ extension ConditionMacro {
6773 . disabled
6874 }
6975
76+ public static var returnType : TypeSyntax ? {
77+ TypeSyntax (
78+ MemberTypeSyntax (
79+ baseType: IdentifierTypeSyntax ( name: . identifier( " Swift " ) ) ,
80+ name: . identifier( " Bool " )
81+ )
82+ )
83+ }
84+
7085 /// Perform the expansion of this condition macro.
7186 ///
7287 /// - Parameters:
@@ -179,6 +194,7 @@ extension ConditionMacro {
179194 for: macro,
180195 rootedAt: originalArgumentExpr,
181196 effectKeywordsToApply: effectKeywordsToApply,
197+ returnType: returnType,
182198 in: context
183199 )
184200 checkArguments. append ( Argument ( expression: closureExpr) )
@@ -316,6 +332,25 @@ public struct RequireMacro: ConditionMacro {
316332 }
317333}
318334
335+ /// A type describing the expansion of the `#require()` macro when it produces
336+ /// an optional value.
337+ public struct UnwrapMacro : ConditionMacro {
338+ public static var isThrowing : Bool {
339+ true
340+ }
341+
342+ public static var returnType : TypeSyntax ? {
343+ TypeSyntax (
344+ MemberTypeSyntax (
345+ baseType: IdentifierTypeSyntax ( name: . identifier( " Swift " ) ) ,
346+ name: . identifier( " Optional " )
347+ )
348+ )
349+ }
350+ }
351+
352+ // MARK: - Refined condition macros
353+
319354/// A protocol that can be used to create a condition macro that refines the
320355/// behavior of another previously-defined condition macro.
321356public protocol RefinedConditionMacro : ConditionMacro {
@@ -326,6 +361,10 @@ extension RefinedConditionMacro {
326361 public static var isThrowing : Bool {
327362 Base . isThrowing
328363 }
364+
365+ public static var returnType : TypeSyntax ? {
366+ Base . returnType
367+ }
329368}
330369
331370// MARK: - Diagnostics-emitting condition macros
@@ -335,7 +374,7 @@ extension RefinedConditionMacro {
335374///
336375/// This type is otherwise exactly equivalent to ``RequireMacro``.
337376public struct AmbiguousRequireMacro : RefinedConditionMacro {
338- public typealias Base = RequireMacro
377+ public typealias Base = UnwrapMacro
339378
340379 public static func expansion(
341380 of macro: some FreestandingMacroExpansionSyntax ,
@@ -346,7 +385,7 @@ public struct AmbiguousRequireMacro: RefinedConditionMacro {
346385 }
347386
348387 // Perform the normal macro expansion for #require().
349- return try RequireMacro . expansion ( of: macro, in: context)
388+ return try Base . expansion ( of: macro, in: context)
350389 }
351390
352391 /// Check for an ambiguous argument to the `#require()` macro and emit the
@@ -378,7 +417,7 @@ public struct AmbiguousRequireMacro: RefinedConditionMacro {
378417///
379418/// This type is otherwise exactly equivalent to ``RequireMacro``.
380419public struct NonOptionalRequireMacro : RefinedConditionMacro {
381- public typealias Base = RequireMacro
420+ public typealias Base = UnwrapMacro
382421
383422 public static func expansion(
384423 of macro: some FreestandingMacroExpansionSyntax ,
@@ -389,7 +428,7 @@ public struct NonOptionalRequireMacro: RefinedConditionMacro {
389428 }
390429
391430 // Perform the normal macro expansion for #require().
392- return try RequireMacro . expansion ( of: macro, in: context)
431+ return try Base . expansion ( of: macro, in: context)
393432 }
394433}
395434
@@ -418,7 +457,7 @@ public struct RequireThrowsMacro: RefinedConditionMacro {
418457 }
419458
420459 // Perform the normal macro expansion for #require().
421- return try RequireMacro . expansion ( of: macro, in: context)
460+ return try Base . expansion ( of: macro, in: context)
422461 }
423462}
424463
@@ -438,7 +477,7 @@ public struct RequireThrowsNeverMacro: RefinedConditionMacro {
438477 }
439478
440479 // Perform the normal macro expansion for #require().
441- return try RequireMacro . expansion ( of: macro, in: context)
480+ return try Base . expansion ( of: macro, in: context)
442481 }
443482}
444483
0 commit comments