Skip to content

[Observation] Add property definite initialization support #65984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions lib/Macros/Sources/ObservationMacros/ObservableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,15 @@ extension ObservableMacro: MemberMacro {
declaration.addIfNeeded(ObservableMacro.registrarVariable(observableType), to: &declarations)
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 }
if property.initializer == nil {
context.addDiagnostics(from: DiagnosticsError(syntax: property, message: "@Observable requires property '\(property.identifier?.text ?? "")' to have an initial value", id: .missingInitializer), node: property)
}
let storage = DeclSyntax(property.privatePrefixed("_", addingAttribute: ObservableMacro.ignoredAttribute))
declaration.addIfNeeded(storage, to: &declarations)

if property.hasMacroApplication(ObservableMacro.ignoredMacroName) { continue }
let storage = DeclSyntax(property.privatePrefixed("_", addingAttribute: ObservableMacro.ignoredAttribute))
declaration.addIfNeeded(storage, to: &declarations)
}
#endif

return declarations
}
Expand Down Expand Up @@ -293,6 +291,13 @@ public struct ObservationTrackedMacro: AccessorMacro {
return []
}

let initAccessor: AccessorDeclSyntax =
"""
init(initialValue) initializes(_\(identifier)) {
_\(identifier) = initialValue
}
"""

let getAccessor: AccessorDeclSyntax =
"""
get {
Expand All @@ -310,7 +315,7 @@ public struct ObservationTrackedMacro: AccessorMacro {
}
"""

return [getAccessor, setAccessor]
return [initAccessor, getAccessor, setAccessor]
}
}

Expand All @@ -327,8 +332,9 @@ extension ObservationTrackedMacro: PeerMacro {
property.isValidForObservation else {
return []
}

if property.hasMacroApplication(ObservableMacro.ignoredMacroName) {

if property.hasMacroApplication(ObservableMacro.ignoredMacroName) ||
property.hasMacroApplication(ObservableMacro.trackedMacroName) {
return []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
#if $Macros && hasAttribute(attached)

@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(conformance)
public macro Observable() =
#externalMacro(module: "ObservationMacros", type: "ObservableMacro")

@available(SwiftStdlib 5.9, *)
@attached(accessor)
// @attached(peer, names: prefixed(_))
#if OBSERVATION_SUPPORTS_PEER_MACROS
@attached(peer, names: prefixed(_))
#endif
public macro ObservationTracked() =
#externalMacro(module: "ObservationMacros", type: "ObservationTrackedMacro")

Expand Down
20 changes: 19 additions & 1 deletion test/stdlib/Observation/Observable.swift
Original file line number Diff line number Diff line change
@@ -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 Macros -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 -Xfrontend -plugin-path -Xfrontend %swift-host-lib-dir/plugins)

// REQUIRES: observation
// REQUIRES: concurrency
Expand All @@ -23,6 +23,24 @@ struct Structure {
var field: Int = 0
}

@Observable
struct MemberwiseInitializers {
var field: Int
}

func validateMemberwiseInitializers() {
_ = MemberwiseInitializers(field: 3)
}

@Observable
struct DefiniteInitialization {
var field: Int

init(field: Int) {
self.field = field
}
}

@Observable
class ContainsWeak {
weak var obj: AnyObject? = nil
Expand Down