From c5fbb4210e390257725a4d1da0f906ade6521647 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Thu, 13 Jul 2023 14:30:26 -0500 Subject: [PATCH 1/3] [Obs] Switch to peer instead of arbitrary macros This adopts `@attached(peer)` for generating an observable type's underscored storage when expanding the `@ObservationTracked` macro, instead of using `arbitrary` with the member macro on the observable type. --- .../Sources/ObservationMacros/ObservableMacro.swift | 9 --------- .../Observation/Sources/Observation/Observable.swift | 6 ------ test/stdlib/Observation/Observable.swift | 6 +++++- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/lib/Macros/Sources/ObservationMacros/ObservableMacro.swift b/lib/Macros/Sources/ObservationMacros/ObservableMacro.swift index 306c577018e59..f530b0c228f56 100644 --- a/lib/Macros/Sources/ObservationMacros/ObservableMacro.swift +++ b/lib/Macros/Sources/ObservationMacros/ObservableMacro.swift @@ -225,15 +225,6 @@ extension ObservableMacro: MemberMacro { declaration.addIfNeeded(ObservableMacro.accessFunction(observableType), to: &declarations) declaration.addIfNeeded(ObservableMacro.withMutationFunction(observableType), to: &declarations) -#if !OBSERVATION_SUPPORTS_PEER_MACROS - let storedInstanceVariables = declaration.definedVariables.filter { $0.isValidForObservation } - for property in storedInstanceVariables { - if property.hasMacroApplication(ObservableMacro.ignoredMacroName) { continue } - let storage = DeclSyntax(property.privatePrefixed("_", addingAttribute: ObservableMacro.ignoredAttribute)) - declaration.addIfNeeded(storage, to: &declarations) - } -#endif - return declarations } } diff --git a/stdlib/public/Observation/Sources/Observation/Observable.swift b/stdlib/public/Observation/Sources/Observation/Observable.swift index 6c0e067b441d9..87fa76f260692 100644 --- a/stdlib/public/Observation/Sources/Observation/Observable.swift +++ b/stdlib/public/Observation/Sources/Observation/Observable.swift @@ -40,11 +40,7 @@ public protocol Observable { } /// } /// } @available(SwiftStdlib 5.9, *) -#if OBSERVATION_SUPPORTS_PEER_MACROS @attached(member, names: named(_$observationRegistrar), named(access), named(withMutation)) -#else -@attached(member, names: named(_$observationRegistrar), named(access), named(withMutation), arbitrary) -#endif @attached(memberAttribute) @attached(extension, conformances: Observable) public macro Observable() = @@ -56,9 +52,7 @@ public macro Observable() = /// framework isn't necessary. @available(SwiftStdlib 5.9, *) @attached(accessor, names: named(init), named(get), named(set)) -#if OBSERVATION_SUPPORTS_PEER_MACROS @attached(peer, names: prefixed(_)) -#endif public macro ObservationTracked() = #externalMacro(module: "ObservationMacros", type: "ObservationTrackedMacro") diff --git a/test/stdlib/Observation/Observable.swift b/test/stdlib/Observation/Observable.swift index 328a90e2f6c8c..3ac9e97385bab 100644 --- a/test/stdlib/Observation/Observable.swift +++ b/test/stdlib/Observation/Observable.swift @@ -1,6 +1,6 @@ // REQUIRES: swift_swift_parser, executable_test -// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature InitAccessors -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -plugin-path -Xfrontend %swift-host-lib-dir/plugins) +// RUN: %target-run-simple-swift( -Xfrontend -dump-macro-expansions -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature InitAccessors -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -plugin-path -Xfrontend %swift-host-lib-dir/plugins) // Run this test via the swift-plugin-server // RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature InitAccessors -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -external-plugin-path -Xfrontend %swift-host-lib-dir/plugins#%swift-plugin-server) @@ -183,6 +183,10 @@ struct Validator { let suite = TestSuite("Observable") + + suite.test("only instantiate") { + let test = MiddleNamePerson() + } suite.test("unobserved value changes") { let test = MiddleNamePerson() From 90d341421ec4a4ba2a2fdfe5d36f035099426e57 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Wed, 19 Jul 2023 16:07:13 -0500 Subject: [PATCH 2/3] Remove dump-macro flag in test --- test/stdlib/Observation/Observable.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/stdlib/Observation/Observable.swift b/test/stdlib/Observation/Observable.swift index 7f690feca992f..cdc555c6e8006 100644 --- a/test/stdlib/Observation/Observable.swift +++ b/test/stdlib/Observation/Observable.swift @@ -1,6 +1,6 @@ // REQUIRES: swift_swift_parser, executable_test -// RUN: %target-run-simple-swift( -Xfrontend -dump-macro-expansions -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature InitAccessors -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -plugin-path -Xfrontend %swift-host-lib-dir/plugins) +// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature InitAccessors -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -plugin-path -Xfrontend %swift-host-lib-dir/plugins) // Run this test via the swift-plugin-server // RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature InitAccessors -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -external-plugin-path -Xfrontend %swift-host-lib-dir/plugins#%swift-plugin-server) From e68395c9232f9d50fbea5ac5ba3abf19d5e26415 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Thu, 20 Jul 2023 17:20:46 -0500 Subject: [PATCH 3/3] Disable diagnostic test again for now --- test/stdlib/Observation/ObservableDiagnostics.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/test/stdlib/Observation/ObservableDiagnostics.swift b/test/stdlib/Observation/ObservableDiagnostics.swift index cbb405b94cf54..4efb26a280a61 100644 --- a/test/stdlib/Observation/ObservableDiagnostics.swift +++ b/test/stdlib/Observation/ObservableDiagnostics.swift @@ -10,6 +10,7 @@ // REQUIRES: objc_interop // UNSUPPORTED: use_os_stdlib // UNSUPPORTED: back_deployment_runtime +// REQUIRES: rdar112623492 import Observation