Skip to content

Commit f58834a

Browse files
committed
[Macros] Drop the initializer when an accessor macro is applied
1 parent 882ef91 commit f58834a

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,24 @@ void swift::expandAccessors(
882882
// Trigger parsing of the sequence of accessor declarations. This has the
883883
// side effect of registering those accessor declarations with the storage
884884
// declaration, so there is nothing further to do.
885-
(void)macroSourceFile->getTopLevelItems();
885+
for (auto decl : macroSourceFile->getTopLevelItems()) {
886+
auto accessor = dyn_cast_or_null<AccessorDecl>(decl.dyn_cast<Decl *>());
887+
if (!accessor)
888+
continue;
889+
890+
if (accessor->isObservingAccessor())
891+
continue;
892+
893+
// If any non-observing accessor was added, remove the initializer if
894+
// there is one.
895+
if (auto var = dyn_cast<VarDecl>(storage)) {
896+
if (auto binding = var->getParentPatternBinding()) {
897+
unsigned index = binding->getPatternEntryIndexForVarDecl(var);
898+
binding->setInit(index, nullptr);
899+
break;
900+
}
901+
}
902+
}
886903
}
887904

888905
// FIXME: Almost entirely duplicated code from `expandAccessors`.

test/Macros/accessor_macros.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %target-build-swift -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
33

44
// First check for no errors.
5-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s
5+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir
66

77
// Check that the expansion buffer are as expected.
88
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -dump-macro-expansions > %t/expansions-dump.txt 2>&1
@@ -53,7 +53,7 @@ struct MyStruct {
5353
// CHECK-DUMP: }
5454

5555
@myPropertyWrapper
56-
var birthDate: Date?
56+
var birthDate: Date? = nil
5757
// CHECK-DUMP: macro:birthDate@myPropertyWrapper
5858
// CHECK-DUMP: get {
5959
// CHECK-DUMP: _birthDate.wrappedValue

0 commit comments

Comments
 (0)