File tree Expand file tree Collapse file tree 3 files changed +59
-5
lines changed Expand file tree Collapse file tree 3 files changed +59
-5
lines changed Original file line number Diff line number Diff line change @@ -1347,11 +1347,7 @@ Optional<unsigned> swift::expandAccessors(
1347
1347
// declaration, so there is nothing further to do.
1348
1348
bool foundNonObservingAccessor = false ;
1349
1349
bool foundInitAccessor = false ;
1350
- for (auto decl : macroSourceFile->getTopLevelItems ()) {
1351
- auto accessor = dyn_cast_or_null<AccessorDecl>(decl.dyn_cast <Decl *>());
1352
- if (!accessor)
1353
- continue ;
1354
-
1350
+ for (auto accessor : storage->getAllAccessors ()) {
1355
1351
if (accessor->isInitAccessor ())
1356
1352
foundInitAccessor = true ;
1357
1353
Original file line number Diff line number Diff line change @@ -467,6 +467,55 @@ extension PropertyWrapperMacro: PeerMacro {
467
467
}
468
468
}
469
469
470
+ extension PatternBindingSyntax . Accessor {
471
+ var hasGetter : Bool {
472
+ switch self {
473
+ case . accessors( let accessors) :
474
+ for accessor in accessors. accessors {
475
+ if accessor. accessorKind. text == " get " {
476
+ return true
477
+ }
478
+ }
479
+
480
+ return false
481
+ case . getter:
482
+ return true
483
+ }
484
+ }
485
+ }
486
+
487
+ public struct PropertyWrapperSkipsComputedMacro { }
488
+
489
+ extension PropertyWrapperSkipsComputedMacro : AccessorMacro , Macro {
490
+ public static func expansion(
491
+ of node: AttributeSyntax ,
492
+ providingAccessorsOf declaration: some DeclSyntaxProtocol ,
493
+ in context: some MacroExpansionContext
494
+ ) throws -> [ AccessorDeclSyntax ] {
495
+ guard let varDecl = declaration. as ( VariableDeclSyntax . self) ,
496
+ let binding = varDecl. bindings. first,
497
+ let identifier = binding. pattern. as ( IdentifierPatternSyntax . self) ? . identifier, !( binding. accessor? . hasGetter ?? false )
498
+ else {
499
+ return [ ]
500
+ }
501
+
502
+ return [
503
+ """
504
+
505
+ get {
506
+ _ \( identifier) .wrappedValue
507
+ }
508
+ """ ,
509
+ """
510
+
511
+ set {
512
+ _ \( identifier) .wrappedValue = newValue
513
+ }
514
+ """ ,
515
+ ]
516
+ }
517
+ }
518
+
470
519
public struct WrapAllProperties : MemberAttributeMacro {
471
520
public static func expansion(
472
521
of node: AttributeSyntax ,
Original file line number Diff line number Diff line change 23
23
macro myPropertyWrapper( ) =
24
24
#externalMacro( module: " MacroDefinition " , type: " PropertyWrapperMacro " )
25
25
26
+ @attached ( accessor)
27
+ macro myPropertyWrapperSkipsComputed( ) =
28
+ #externalMacro( module: " MacroDefinition " , type: " PropertyWrapperSkipsComputedMacro " )
29
+
26
30
struct Date { }
27
31
28
32
struct MyWrapperThingy < T> {
@@ -64,6 +68,11 @@ struct MyStruct {
64
68
// CHECK-DUMP: set {
65
69
// CHECK-DUMP: _birthDate.wrappedValue = newValue
66
70
// CHECK-DUMP: }
71
+
72
+ @myPropertyWrapperSkipsComputed
73
+ var age : Int ? {
74
+ get { nil }
75
+ }
67
76
}
68
77
69
78
// Test that the fake-property-wrapper-introduced accessors execute properly at
You can’t perform that action at this time.
0 commit comments