Files
swift-mirror/test/Sema/moveonly_read_set_ban.swift
Michael Gottesman 2a1d9199a3 [sema] Ban user created read/set accessor that produce noncopyable types.
The reason why we are doing this is that this combination of read/set forces the
compiler to emit a copy if we want to emit a modify operation.

The reason why we are forced to emit such a copy is that:

1. _read provides a guaranteed value in memory
2. performing a modify requires an owned value in memory.

This together implies that the only way we can do this is to copy from the _read
into temporary memory. But we have a noncopyable type so we can't do this.

rdar://112915525
2023-07-26 12:53:43 -07:00

65 lines
2.5 KiB
Swift

// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-library-evolution
@frozen
public struct NonCopyableS : ~Copyable {}
@frozen
public enum NonCopyableE : ~Copyable {}
public struct S {
public var x: NonCopyableS { // expected-error {{noncopyable variable cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
public var x2: NonCopyableE { // expected-error {{noncopyable variable cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
public subscript(x x: Int) -> NonCopyableS { // expected-error {{noncopyable subscript cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
public subscript(y y: Int) -> NonCopyableE { // expected-error {{noncopyable subscript cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
}
public class C {
public var x: NonCopyableS { // expected-error {{noncopyable variable cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
public var x2: NonCopyableE { // expected-error {{noncopyable variable cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
public subscript(x x: Int) -> NonCopyableS { // expected-error {{noncopyable subscript cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
public subscript(y y: Int) -> NonCopyableE { // expected-error {{noncopyable subscript cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
}
public enum E {
public var x: NonCopyableS { // expected-error {{noncopyable variable cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
public var x2: NonCopyableE { // expected-error {{noncopyable variable cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
public subscript(x x: Int) -> NonCopyableS { // expected-error {{noncopyable subscript cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
public subscript(y y: Int) -> NonCopyableE { // expected-error {{noncopyable subscript cannot provide a read and set accessor}}
_read { fatalError() }
set { fatalError() }
}
}