@@ -39,6 +39,12 @@ public import SwiftSyntaxMacros
39
39
public protocol ConditionMacro : ExpressionMacro , Sendable {
40
40
/// Whether or not the macro's expansion may throw an error.
41
41
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 }
42
48
}
43
49
44
50
// MARK: -
@@ -67,6 +73,15 @@ extension ConditionMacro {
67
73
. disabled
68
74
}
69
75
76
+ public static var returnType : TypeSyntax ? {
77
+ TypeSyntax (
78
+ MemberTypeSyntax (
79
+ baseType: IdentifierTypeSyntax ( name: . identifier( " Swift " ) ) ,
80
+ name: . identifier( " Bool " )
81
+ )
82
+ )
83
+ }
84
+
70
85
/// Perform the expansion of this condition macro.
71
86
///
72
87
/// - Parameters:
@@ -179,6 +194,7 @@ extension ConditionMacro {
179
194
for: macro,
180
195
rootedAt: originalArgumentExpr,
181
196
effectKeywordsToApply: effectKeywordsToApply,
197
+ returnType: returnType,
182
198
in: context
183
199
)
184
200
checkArguments. append ( Argument ( expression: closureExpr) )
@@ -316,6 +332,25 @@ public struct RequireMacro: ConditionMacro {
316
332
}
317
333
}
318
334
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
+
319
354
/// A protocol that can be used to create a condition macro that refines the
320
355
/// behavior of another previously-defined condition macro.
321
356
public protocol RefinedConditionMacro : ConditionMacro {
@@ -326,6 +361,10 @@ extension RefinedConditionMacro {
326
361
public static var isThrowing : Bool {
327
362
Base . isThrowing
328
363
}
364
+
365
+ public static var returnType : TypeSyntax ? {
366
+ Base . returnType
367
+ }
329
368
}
330
369
331
370
// MARK: - Diagnostics-emitting condition macros
@@ -335,7 +374,7 @@ extension RefinedConditionMacro {
335
374
///
336
375
/// This type is otherwise exactly equivalent to ``RequireMacro``.
337
376
public struct AmbiguousRequireMacro : RefinedConditionMacro {
338
- public typealias Base = RequireMacro
377
+ public typealias Base = UnwrapMacro
339
378
340
379
public static func expansion(
341
380
of macro: some FreestandingMacroExpansionSyntax ,
@@ -346,7 +385,7 @@ public struct AmbiguousRequireMacro: RefinedConditionMacro {
346
385
}
347
386
348
387
// 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)
350
389
}
351
390
352
391
/// Check for an ambiguous argument to the `#require()` macro and emit the
@@ -378,7 +417,7 @@ public struct AmbiguousRequireMacro: RefinedConditionMacro {
378
417
///
379
418
/// This type is otherwise exactly equivalent to ``RequireMacro``.
380
419
public struct NonOptionalRequireMacro : RefinedConditionMacro {
381
- public typealias Base = RequireMacro
420
+ public typealias Base = UnwrapMacro
382
421
383
422
public static func expansion(
384
423
of macro: some FreestandingMacroExpansionSyntax ,
@@ -389,7 +428,7 @@ public struct NonOptionalRequireMacro: RefinedConditionMacro {
389
428
}
390
429
391
430
// 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)
393
432
}
394
433
}
395
434
@@ -418,7 +457,7 @@ public struct RequireThrowsMacro: RefinedConditionMacro {
418
457
}
419
458
420
459
// 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)
422
461
}
423
462
}
424
463
@@ -438,7 +477,7 @@ public struct RequireThrowsNeverMacro: RefinedConditionMacro {
438
477
}
439
478
440
479
// 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)
442
481
}
443
482
}
444
483
0 commit comments