Files
swift-mirror/test/Sema/generalized_accessors.swift
John McCall 7a4aeed570 Implement generalized accessors using yield-once coroutines.
For now, the accessors have been underscored as `_read` and `_modify`.
I'll prepare an evolution proposal for this feature which should allow
us to remove the underscores or, y'know, rename them to `purple` and
`lettuce`.

`_read` accessors do not make any effort yet to avoid copying the
value being yielded.  I'll work on it in follow-up patches.

Opaque accesses to properties and subscripts defined with `_modify`
accessors will use an inefficient `materializeForSet` pattern that
materializes the value to a temporary instead of accessing it in-place.
That will be fixed by migrating to `modify` over `materializeForSet`,
which is next up after the `read` optimizations.

SIL ownership verification doesn't pass yet for the test cases here
because of a general fault in SILGen where borrows can outlive their
borrowed value due to being cleaned up on the general cleanup stack
when the borrowed value is cleaned up on the formal-access stack.
Michael, Andy, and I discussed various ways to fix this, but it seems
clear to me that it's not in any way specific to coroutine accesses.

rdar://35399664
2018-07-23 18:59:58 -04:00

107 lines
2.6 KiB
Swift

// RUN: %target-typecheck-verify-swift
struct Read {
var simpleReadImmutable: Int {
_read {}
}
var redundantRead: Int {
_read {} // expected-note {{previous definition of 'read' accessor here}}
_read {} // expected-error {{variable already has a 'read' accessor}}
}
var readAndGet: Int {
_read {} // expected-error {{variable cannot provide both a 'read' accessor and a getter}}
get {} // expected-note {{getter defined here}}
}
var readAndAddress: Int {
_read {} // expected-note {{'read' accessor defined here}}
unsafeAddress {} // expected-error {{variable cannot provide both an addressor and a 'read' accessor}}
}
}
struct ReadModifiable {
var readAndWillSet: Int {
_read {}
willSet {} // expected-error {{'willSet' cannot be provided together with a 'read' accessor}}
}
var readAndDidSet: Int {
_read {}
didSet {} // expected-error {{'didSet' cannot be provided together with a 'read' accessor}}
}
var readAndSet: Int {
_read {}
set {}
}
var readAndMutableAddress: Int {
_read {}
unsafeMutableAddress {}
}
var readAndModify: Int {
_read {}
_modify {}
}
}
struct Modify {
var modifyAlone: Int {
_modify {} // expected-error {{variable with a 'modify' accessor must also have a getter, addressor, or 'read' accessor}}
}
var getAndModify: Int {
get {}
_modify {}
}
var addressAndModify: Int {
unsafeAddress {}
_modify {}
}
var readAndModify: Int {
_read {}
_modify {}
}
var getAndRedundantModify: Int {
get {}
_modify {} // expected-note {{previous definition of 'modify' accessor here}}
_modify {} // expected-error {{variable already has a 'modify' accessor}}
}
var getAndModifyAndMutableAddress: Int {
get {}
_modify {} // expected-note {{'modify' accessor defined here}}
unsafeMutableAddress {} // expected-error {{variable cannot provide both a mutable addressor and a 'modify' accessor}}
}
var getAndModifyAndSet: Int {
get {}
_modify {}
set {}
}
var getAndNonMutatingModifyAndNonMutatingSet: Int {
get {}
nonmutating _modify {}
nonmutating set {}
}
var getAndNonMutatingModifyAndSet: Int {
get {}
nonmutating _modify {} // expected-error {{'modify' accessor cannot be nonmutating when the setter is mutating}}
set {} // expected-note {{setter defined here}}
}
var getAndModifyAndNonMutatingSet: Int {
get {}
_modify {}// expected-error {{'modify' accessor cannot be mutating when the setter is nonmutating}}
nonmutating set {} // expected-note {{setter defined here}}
}
}