Skip to content
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

[Property wrappers] Prototype support for enclosing self-based access #25884

Merged

Conversation

DougGregor
Copy link
Member

Allow property wrapper types to support a second access pattern for
instance properties of classes. When supported, the property wrapper's
static subscript(_enclosingInstance:storage:) is provided with the
enclosing "self" and a reference-writable key path referring to the
backing storage property.

Implements rdar://problem/52222560.

Allow property wrapper types to support a second access pattern for
instance properties of classes. When supported, the property wrapper's
static subscript(_enclosingInstance:storage:) is provided with the
enclosing "self" and a reference-writable key path referring to the
backing storage property.

Implements rdar://problem/52222560.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

@swift-ci
Copy link
Contributor

Linux Toolchain (Ubuntu 16.04)
Download Toolchain
Git Sha - 8c54db7

Install command
tar zxf swift-PR-25884-252-ubuntu16.04.tar.gz
More info

@swift-ci
Copy link
Contributor

macOS Toolchain
Download Toolchain
Git Sha - 8c54db7

Install command
tar -zxf swift-PR-25884-328-osx.tar.gz --directory ~/

Extend handling of enclosing-self subscripts by differentiating
between the original wrapped property (which now goes through
`subscript(_enclosingInstance:wrapped:storage:)`) and the projected
property (which goes through
`subscript(_enclosingInstance:projected:storage:)`). The new middle
argument provides a key path to the property that was accessed,
allowing one to distinguish the property being updated.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

@swift-ci
Copy link
Contributor

Linux Toolchain (Ubuntu 16.04)
Download Toolchain
Git Sha - bc2e605

Install command
tar zxf swift-PR-25884-254-ubuntu16.04.tar.gz
More info

@swift-ci
Copy link
Contributor

macOS Toolchain
Download Toolchain
Git Sha - bc2e605

Install command
tar -zxf swift-PR-25884-330-osx.tar.gz --directory ~/

@DougGregor DougGregor merged commit 3faf0e8 into swiftlang:master Jul 1, 2019
@DougGregor DougGregor deleted the property-wrappers-enclosing-self branch July 1, 2019 14:06
@@ -4452,6 +4452,9 @@ ERROR(property_wrapper_type_requirement_not_accessible,none,
"more restrictive access than its enclosing property wrapper type %3 "
"(which is %select{private|fileprivate|internal|public|open}4)",
(AccessLevel, DescriptiveDeclKind, DeclName, Type, AccessLevel))
ERROR(property_wrapper_ambiguous_enclosing_self_subscript, none,
"property wrapper type %0 has multiple enclosing-self subscripts %1",
(Type, DeclName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work if you have something like this, to handle a value-semantics wrapper type?

// Read only access from a read only base
subscript(_enclosingInstance: EnclosingSelf, storage: KeyPath<EnclosingSelf, Self>) -> Wrapped { get }

// Writable projection from a writable base
subscript(_enclosingInstance: inout EnclosingSelf, storage: WritableKeyPath<EnclosingSelf, Self>) -> Wrapped { get set }

/// Describes the information needed to perform property wrapper access via
/// the enclosing self.
struct EnclosingSelfPropertyWrapperAccess {
/// The (genreric) subscript that will be used to perform the access.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

@jckarter
Copy link
Contributor

jckarter commented Jul 1, 2019

Any tests?

@lattner
Copy link
Contributor

lattner commented Jul 2, 2019

This appears to be a pretty substantial expansion of the property wrappers feature, which isn't yet standardized. It seems like a really nice extension, but are you planning to go through swift-evolution with this?

@andyj-at-aspin
Copy link

andyj-at-aspin commented Sep 11, 2019

Um. Is there a problem with the Swift toolchain in Xcode 11 GM?

I have replicated the code from the Unit Tests in this pull request into a test project. As coded, with the declaration of the Observable property wrapper and MyType test class in the same source file it all works spiffingly but the moment I move the definition of the MyType class to another source file, mimicking defining the property wrapper in a utility file and then
making use of it in many others...

Xcode spits out a Segmentation fault 11 and will not compile.

If you can only use property wrappers with the so useful enclosing self subscript creation in the file in which you define them then that's going to mean a return to boiler plate for me. I was sooooo looking forward to using this to finally create the thread safe lazy that I've been boiler plating around for the last 3 years!

Segfault and the test project linked:

SegFault.txt
Archive.zip

@jckarter
Copy link
Contributor

@andyj-at-aspin Please file a bug at bugs.swift.org, if you haven't already. Thanks for reporting this!

@andyj-at-aspin
Copy link

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants