Skip to content

Property delegates as custom attributes #23701

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

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Apr 1, 2019

This is an implementation of property delegates as custom attributes, under discussion in this pitch thread. The implementation is mostly complete, although there are some annoying known bugs:

  • Property delegates cannot be used on local variables; only properties of types work well (lazy is similarly broken and always has been!)

@DougGregor DougGregor force-pushed the property-delegates-as-custom-attributes branch 4 times, most recently from e51404e to 7f0096e Compare April 5, 2019 16:33
@hartbit
Copy link
Contributor

hartbit commented Apr 11, 2019

@DougGregor Would it be possible to have a toolchain created to play around with Property Delegates? I'd like to use them to improve a library Im working on as a way to test them out.

@DougGregor
Copy link
Member Author

@hartbit let me make sure things are back in a working state now, and once they are I'll build a toolchain. I just got to the point where they work end-to-end, so long as you don't touch @autoclosure.

@DougGregor DougGregor force-pushed the property-delegates-as-custom-attributes branch from 4a265c1 to d53d93f Compare April 11, 2019 18:05
@hartbit
Copy link
Contributor

hartbit commented Apr 11, 2019

That's a lot of toolchains 😂

@DougGregor
Copy link
Member Author

@swift-ci please please please please please build toolchain

@shahmishal
Copy link
Member

@swift-ci please build toolchain

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

2 similar comments
@shahmishal
Copy link
Member

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci build toolchain

@shahmishal
Copy link
Member

@swift-ci build toolchain Linux

@shahmishal
Copy link
Member

@swift-ci please smoke test Linux

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

@DougGregor DougGregor force-pushed the property-delegates-as-custom-attributes branch from d2fcc35 to 7cac37a Compare April 13, 2019 06:47
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 85ff735eb5c1120af3c47d7c5d682aef71aa5038

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 85ff735eb5c1120af3c47d7c5d682aef71aa5038

@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

@hartbit
Copy link
Contributor

hartbit commented Apr 14, 2019

With the implementation in the toolchain, the DelayedMutable example from the proposal fails to compile:

@propertyDelegate
struct DelayedMutable<Value> {
    private var _value: Value? = nil

    var value: Value {
        get {
            guard let value = _value else {
                fatalError("property accessed before being initialized")
            }
            return value
        }
        set {
            _value = newValue
        }
    }

    /// "Reset" the delegate so it can be initialized again.
    mutating func reset() {
        _value = nil
    }
}

class Foo {
    @DelayedMutable var x: Int

    init() {
        // We don't know "x" yet, and we don't have to set it
    } // error: Return from initializer without initializing all stored properties

    func getX() -> Int {
        return x // Will crash if 'self.x' wasn't initialized
    }
}

Is this known?

@DougGregor
Copy link
Member Author

@hartbit ah, thank you! that should be @DelayedMutable() var x: Int. I'll fix the proposal

@hartbit
Copy link
Contributor

hartbit commented Apr 15, 2019

@DougGregor The () part of the syntax is surprising because the proposal does not talk about it much. When is it required?

@DougGregor
Copy link
Member Author

@hartbit The () is there to call the no-parameter initializer. Without it, the delegate instance hasn't been initialized. There's no special case here

eeckstein and others added 16 commits April 23, 2019 11:32
Used for property delegates.
Nonmutating setters for properties with attached delegates inside
value-semantic types cause SIL verifier errors. Avoid performing DI on
them for now.
… type.

Fixes the crash-on-invalid in rdar://problem/49982937.
…ibutes

When a custom attribute is given a direct initializer, save and re-use
the initializer context we create so that it can be associated with the
enclosing pattern binding. Fixes assertions involving explicit closures
in property delegates.
When we encounter a custom attributes on a property (via a pattern binding),
visit those custom attributes. Provides basic indexing support.
@DougGregor
Copy link
Member Author

And... another rebase is incoming.

@DougGregor DougGregor force-pushed the property-delegates-as-custom-attributes branch from ba095ef to faa176f Compare April 23, 2019 21:16
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

…ivate.

When the property delegate type overrides the delegate value by providing
a delegateValue property, name the backing storage $$foo and make it private.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

@DougGregor DougGregor changed the title [WIP] Property delegates as custom attributes Property delegates as custom attributes Apr 25, 2019
@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit d24bc38 into swiftlang:master Apr 25, 2019
@DougGregor DougGregor deleted the property-delegates-as-custom-attributes branch April 25, 2019 15:30
@compnerd
Copy link
Member

@DougGregor - sigh this broke the windows build :-( https://dev.azure.com/compnerd/windows-swift/_build/results?buildId=1697

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.

9 participants