Skip to content

Commit d20e485

Browse files
committed
Allow CustomStringConvertible et al: ~Copyable
1 parent 3313de1 commit d20e485

File tree

6 files changed

+59
-35
lines changed

6 files changed

+59
-35
lines changed

stdlib/public/core/OutputStream.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ extension TextOutputStream {
9999
/// To add `TextOutputStreamable` conformance to a custom type, implement the
100100
/// required `write(to:)` method. Call the given output stream's `write(_:)`
101101
/// method in your implementation.
102-
public protocol TextOutputStreamable {
102+
public protocol TextOutputStreamable: ~Copyable {
103103
/// Writes a textual representation of this instance into the given output
104104
/// stream.
105105
func write<Target: TextOutputStream>(to target: inout Target)
@@ -147,7 +147,7 @@ public protocol TextOutputStreamable {
147147
///
148148
/// print(p)
149149
/// // Prints "(21, 30)"
150-
public protocol CustomStringConvertible {
150+
public protocol CustomStringConvertible: ~Copyable {
151151
/// A textual representation of this instance.
152152
///
153153
/// Calling this property directly is discouraged. Instead, convert an
@@ -182,7 +182,7 @@ public protocol CustomStringConvertible {
182182
/// The description property of a conforming type must be a value-preserving
183183
/// representation of the original value. As such, it should be possible to
184184
/// re-create an instance from its string representation.
185-
public protocol LosslessStringConvertible: CustomStringConvertible {
185+
public protocol LosslessStringConvertible: CustomStringConvertible, ~Copyable {
186186
/// Instantiates an instance of the conforming type from a string
187187
/// representation.
188188
init?(_ description: String)
@@ -239,7 +239,7 @@ public protocol LosslessStringConvertible: CustomStringConvertible {
239239
///
240240
/// print(String(reflecting: p))
241241
/// // Prints "(21, 30)"
242-
public protocol CustomDebugStringConvertible {
242+
public protocol CustomDebugStringConvertible: ~Copyable {
243243
/// A textual representation of this instance, suitable for debugging.
244244
///
245245
/// Calling this property directly is discouraged. Instead, convert an

stdlib/public/core/StringInterpolation.swift

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
105105
/// print(message)
106106
/// // Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."
107107
@inlinable
108-
public mutating func appendInterpolation<T>(_ value: T)
109-
where T: TextOutputStreamable, T: CustomStringConvertible
108+
@_preInverseGenerics
109+
public mutating func appendInterpolation<T>(_ value: borrowing T)
110+
where T: TextOutputStreamable, T: CustomStringConvertible, T: ~Copyable
110111
{
111112
value.write(to: &self)
112113
}
@@ -127,8 +128,9 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
127128
/// print(message)
128129
/// // Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."
129130
@inlinable
130-
public mutating func appendInterpolation<T>(_ value: T)
131-
where T: TextOutputStreamable
131+
@_preInverseGenerics
132+
public mutating func appendInterpolation<T>(_ value: borrowing T)
133+
where T: TextOutputStreamable & ~Copyable
132134
{
133135
value.write(to: &self)
134136
}
@@ -151,8 +153,9 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
151153
/// print(message)
152154
/// // Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."
153155
@inlinable
154-
public mutating func appendInterpolation<T>(_ value: T)
155-
where T: CustomStringConvertible
156+
@_preInverseGenerics
157+
public mutating func appendInterpolation<T>(_ value: borrowing T)
158+
where T: CustomStringConvertible, T: ~Copyable
156159
{
157160
value.description.write(to: &self)
158161
}
@@ -175,7 +178,7 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol, Sendable
175178
/// print(message)
176179
/// // Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."
177180
@inlinable
178-
public mutating func appendInterpolation<T>(_ value: T) {
181+
public mutating func appendInterpolation<T>(_ value: borrowing T) {
179182
#if !$Embedded
180183
_print_unlocked(value, &self)
181184
#else
@@ -217,13 +220,15 @@ extension DefaultStringInterpolation {
217220
/// - value: The value to include in a string interpolation, if non-`nil`.
218221
/// - default: The string to include if `value` is `nil`.
219222
@_alwaysEmitIntoClient
223+
@_preInverseGenerics
220224
public mutating func appendInterpolation<T>(
221-
_ value: T?,
225+
_ value: borrowing T?,
222226
default: @autoclosure () -> some StringProtocol
223-
) where T: TextOutputStreamable, T: CustomStringConvertible {
224-
if let value {
227+
) where T: TextOutputStreamable, T: CustomStringConvertible, T: ~Copyable {
228+
switch value {
229+
case let value?:
225230
self.appendInterpolation(value)
226-
} else {
231+
case nil:
227232
self.appendInterpolation(`default`())
228233
}
229234
}
@@ -247,13 +252,15 @@ extension DefaultStringInterpolation {
247252
/// - value: The value to include in a string interpolation, if non-`nil`.
248253
/// - default: The string to include if `value` is `nil`.
249254
@_alwaysEmitIntoClient
255+
@_preInverseGenerics
250256
public mutating func appendInterpolation<T>(
251-
_ value: T?,
257+
_ value: borrowing T?,
252258
default: @autoclosure () -> some StringProtocol
253-
) where T: TextOutputStreamable {
254-
if let value {
259+
) where T: TextOutputStreamable & ~Copyable {
260+
switch value {
261+
case let value?:
255262
self.appendInterpolation(value)
256-
} else {
263+
case nil:
257264
self.appendInterpolation(`default`())
258265
}
259266
}
@@ -277,13 +284,15 @@ extension DefaultStringInterpolation {
277284
/// - value: The value to include in a string interpolation, if non-`nil`.
278285
/// - default: The string to include if `value` is `nil`.
279286
@_alwaysEmitIntoClient
287+
@_preInverseGenerics
280288
public mutating func appendInterpolation<T>(
281-
_ value: T?,
289+
_ value: borrowing T?,
282290
default: @autoclosure () -> some StringProtocol
283-
) where T: CustomStringConvertible {
284-
if let value {
291+
) where T: CustomStringConvertible, T: ~Copyable {
292+
switch value {
293+
case let value?:
285294
self.appendInterpolation(value)
286-
} else {
295+
case nil:
287296
self.appendInterpolation(`default`())
288297
}
289298
}
@@ -311,9 +320,10 @@ extension DefaultStringInterpolation {
311320
_ value: T?,
312321
default: @autoclosure () -> some StringProtocol
313322
) {
314-
if let value {
323+
switch value {
324+
case let value?:
315325
self.appendInterpolation(value)
316-
} else {
326+
case nil:
317327
self.appendInterpolation(`default`())
318328
}
319329
}

test/Generics/inverse_copyable_requirement.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func checkCasting(_ b: any Box, _ mo: borrowing MO, _ a: Any) {
232232
// the stdlib right now is not yet being compiled with NoncopyableGenerics
233233
func checkStdlibTypes(_ mo: borrowing MO) {
234234
_ = "\(mo)" // expected-error {{no exact matches in call to instance method 'appendInterpolation'}}
235-
let _: String = String(describing: mo) // expected-error {{no exact matches in call to initializer}}
235+
let _: String = String(describing: mo) // expected-error {{initializer 'init(describing:)' requires that 'MO' conform to 'Copyable'}}
236236

237237
let _: [MO] = // expected-error {{type 'MO' does not conform to protocol 'Copyable'}}
238238
[MO(), MO()]

test/api-digester/Outputs/stability-stdlib-source-base.swift.expected

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Constructor ContiguousArray.init(unsafeUninitializedCapacity:initializingWith:)
5353
Constructor ContiguousArray.init(unsafeUninitializedCapacity:initializingWith:) has parameter 1 type change from (inout Swift.UnsafeMutableBufferPointer<Element>, inout Swift.Int) throws -> Swift.Void to (inout Swift.UnsafeMutableBufferPointer<Element>, inout Swift.Int) throws(E) -> Swift.Void
5454
Constructor ContiguousArray.init(unsafeUninitializedCapacity:initializingWith:) is now without rethrows
5555

56+
Protocol LosslessStringConvertible has generic signature change from <Self : Swift.CustomStringConvertible> to <Self : Swift.CustomStringConvertible, Self : ~Copyable>
5657
Protocol SIMDScalar has generic signature change from <Self == Self.SIMD16Storage.Scalar, Self.SIMD16Storage : Swift.SIMDStorage, Self.SIMD2Storage : Swift.SIMDStorage, Self.SIMD32Storage : Swift.SIMDStorage, Self.SIMD4Storage : Swift.SIMDStorage, Self.SIMD64Storage : Swift.SIMDStorage, Self.SIMD8Storage : Swift.SIMDStorage, Self.SIMDMaskScalar : Swift.FixedWidthInteger, Self.SIMDMaskScalar : Swift.SIMDScalar, Self.SIMDMaskScalar : Swift.SignedInteger, Self.SIMD16Storage.Scalar == Self.SIMD2Storage.Scalar, Self.SIMD2Storage.Scalar == Self.SIMD32Storage.Scalar, Self.SIMD32Storage.Scalar == Self.SIMD4Storage.Scalar, Self.SIMD4Storage.Scalar == Self.SIMD64Storage.Scalar, Self.SIMD64Storage.Scalar == Self.SIMD8Storage.Scalar> to <Self : Swift.BitwiseCopyable, Self == Self.SIMD16Storage.Scalar, Self.SIMD16Storage : Swift.SIMDStorage, Self.SIMD2Storage : Swift.SIMDStorage, Self.SIMD32Storage : Swift.SIMDStorage, Self.SIMD4Storage : Swift.SIMDStorage, Self.SIMD64Storage : Swift.SIMDStorage, Self.SIMD8Storage : Swift.SIMDStorage, Self.SIMDMaskScalar : Swift.FixedWidthInteger, Self.SIMDMaskScalar : Swift.SIMDScalar, Self.SIMDMaskScalar : Swift.SignedInteger, Self.SIMDMaskScalar == Self.SIMDMaskScalar.SIMDMaskScalar, Self.SIMD16Storage.Scalar == Self.SIMD2Storage.Scalar, Self.SIMD2Storage.Scalar == Self.SIMD32Storage.Scalar, Self.SIMD32Storage.Scalar == Self.SIMD4Storage.Scalar, Self.SIMD4Storage.Scalar == Self.SIMD64Storage.Scalar, Self.SIMD64Storage.Scalar == Self.SIMD8Storage.Scalar>
5758

5859
Protocol AdditiveArithmetic has added inherited protocol Copyable
@@ -73,15 +74,13 @@ Protocol Collection has added inherited protocol Copyable
7374
Protocol Collection has added inherited protocol Escapable
7475
Protocol Comparable has added inherited protocol Escapable
7576
Protocol Comparable has generic signature change from <Self : Swift.Equatable> to <Self : Swift.Equatable, Self : ~Copyable>
76-
Protocol CustomDebugStringConvertible has added inherited protocol Copyable
7777
Protocol CustomDebugStringConvertible has added inherited protocol Escapable
7878
Protocol CustomLeafReflectable has added inherited protocol Copyable
7979
Protocol CustomLeafReflectable has added inherited protocol Escapable
8080
Protocol CustomPlaygroundDisplayConvertible has added inherited protocol Copyable
8181
Protocol CustomPlaygroundDisplayConvertible has added inherited protocol Escapable
8282
Protocol CustomReflectable has added inherited protocol Copyable
8383
Protocol CustomReflectable has added inherited protocol Escapable
84-
Protocol CustomStringConvertible has added inherited protocol Copyable
8584
Protocol CustomStringConvertible has added inherited protocol Escapable
8685
Protocol Decodable has added inherited protocol Copyable
8786
Protocol Decodable has added inherited protocol Escapable
@@ -130,7 +129,6 @@ Protocol LazyCollectionProtocol has added inherited protocol Copyable
130129
Protocol LazyCollectionProtocol has added inherited protocol Escapable
131130
Protocol LazySequenceProtocol has added inherited protocol Copyable
132131
Protocol LazySequenceProtocol has added inherited protocol Escapable
133-
Protocol LosslessStringConvertible has added inherited protocol Copyable
134132
Protocol LosslessStringConvertible has added inherited protocol Escapable
135133
Protocol MirrorPath has added inherited protocol Copyable
136134
Protocol MirrorPath has added inherited protocol Escapable
@@ -177,7 +175,6 @@ Protocol StringProtocol has added inherited protocol Copyable
177175
Protocol StringProtocol has added inherited protocol Escapable
178176
Protocol TextOutputStream has added inherited protocol Copyable
179177
Protocol TextOutputStream has added inherited protocol Escapable
180-
Protocol TextOutputStreamable has added inherited protocol Copyable
181178
Protocol TextOutputStreamable has added inherited protocol Escapable
182179
Protocol UnicodeCodec has added inherited protocol Copyable
183180
Protocol UnicodeCodec has added inherited protocol Escapable
@@ -190,6 +187,8 @@ Protocol UnsignedInteger has added inherited protocol Escapable
190187
Protocol _AppendKeyPath has added inherited protocol Copyable
191188
Protocol _AppendKeyPath has added inherited protocol Escapable
192189

190+
Accessor CustomDebugStringConvertible.debugDescription.Get() has generic signature change from <Self where Self : Swift.CustomDebugStringConvertible> to <Self where Self : Swift.CustomDebugStringConvertible, Self : ~Copyable>
191+
Accessor CustomStringConvertible.description.Get() has generic signature change from <Self where Self : Swift.CustomStringConvertible> to <Self where Self : Swift.CustomStringConvertible, Self : ~Copyable>
193192
Accessor ManagedBuffer.capacity.Get() has generic signature change from <Header, Element> to <Header, Element where Element : ~Copyable>
194193
Accessor ManagedBuffer.header.Get() has generic signature change from <Header, Element> to <Header, Element where Element : ~Copyable>
195194
Accessor ManagedBuffer.header.Set() has generic signature change from <Header, Element> to <Header, Element where Element : ~Copyable>
@@ -217,6 +216,7 @@ Accessor UnsafePointer.hashValue.Get() has generic signature change from <Pointe
217216
Accessor UnsafePointer.pointee.Get() has been removed
218217
Accessor UnsafePointer.subscript(_:).Get() has been removed
219218
Class ManagedBuffer has generic signature change from <Header, Element> to <Header, Element where Element : ~Copyable>
219+
Constructor LosslessStringConvertible.init(_:) has generic signature change from <Self where Self : Swift.LosslessStringConvertible> to <Self where Self : Swift.LosslessStringConvertible, Self : ~Copyable>
220220
Constructor ExpressibleByNilLiteral.init(nilLiteral:) has generic signature change from <Self where Self : Swift.ExpressibleByNilLiteral> to <Self where Self : Swift.ExpressibleByNilLiteral, Self : ~Copyable, Self : ~Escapable>
221221
Constructor ManagedBufferPointer.init(bufferClass:minimumCapacity:makingHeaderWith:) has generic signature change from <Header, Element> to <Header, Element where Element : ~Copyable>
222222
Constructor ManagedBufferPointer.init(unsafeBufferObject:) has generic signature change from <Header, Element> to <Header, Element where Element : ~Copyable>
@@ -336,6 +336,10 @@ TypeAlias UnsafePointer.Stride has generic signature change from <Pointee> to <P
336336
Func FixedWidthInteger.&*(_:_:) has been added as a protocol requirement
337337
Accessor UnsafeBufferPointer.debugDescription.Get() has generic signature change from <Element> to <Element where Element : ~Copyable>
338338
Accessor UnsafeMutableBufferPointer.debugDescription.Get() has generic signature change from <Element> to <Element where Element : ~Copyable>
339+
Func DefaultStringInterpolation.appendInterpolation(_:) has generic signature change from <T where T : Swift.CustomStringConvertible, T : Swift.TextOutputStreamable> to <T where T : Swift.CustomStringConvertible, T : Swift.TextOutputStreamable, T : ~Copyable>
340+
Func DefaultStringInterpolation.appendInterpolation(_:) has generic signature change from <T where T : Swift.CustomStringConvertible> to <T where T : Swift.CustomStringConvertible, T : ~Copyable>
341+
Func DefaultStringInterpolation.appendInterpolation(_:) has generic signature change from <T where T : Swift.TextOutputStreamable> to <T where T : Swift.TextOutputStreamable, T : ~Copyable>
342+
Func DefaultStringInterpolation.appendInterpolation(_:) has parameter 0 changing from Default to Shared
339343
Func ManagedBuffer.withUnsafeMutablePointerToElements(_:) has generic signature change from <Header, Element, R> to <Header, Element, E, R where E : Swift.Error, Element : ~Copyable, R : ~Copyable>
340344
Func ManagedBuffer.withUnsafeMutablePointerToElements(_:) is now without rethrows
341345
Func ManagedBuffer.withUnsafeMutablePointerToHeader(_:) has generic signature change from <Header, Element, R> to <Header, Element, E, R where E : Swift.Error, Element : ~Copyable, R : ~Copyable>
@@ -358,6 +362,7 @@ Func Result.flatMapError(_:) has generic signature change from <Success, Failure
358362
Func Result.flatMapError(_:) has self access kind changing from NonMutating to Consuming
359363
Func Result.map(_:) has generic signature change from <Success, Failure, NewSuccess where Failure : Swift.Error> to <Success, Failure, NewSuccess where Failure : Swift.Error, NewSuccess : ~Copyable>
360364
Func Result.mapError(_:) has generic signature change from <Success, Failure, NewFailure where Failure : Swift.Error, NewFailure : Swift.Error> to <Success, Failure, NewFailure where Failure : Swift.Error, NewFailure : Swift.Error, Success : ~Copyable, Success : ~Escapable>
365+
Func TextOutputStreamable.write(to:) has generic signature change from <Self, Target where Self : Swift.TextOutputStreamable, Target : Swift.TextOutputStream> to <Self, Target where Self : Swift.TextOutputStreamable, Target : Swift.TextOutputStream, Self : ~Copyable>
361366
Func UnsafeBufferPointer.withMemoryRebound(to:_:) has generic signature change from <Element, T, Result> to <Element, T, E, Result where E : Swift.Error, Element : ~Copyable, T : ~Copyable, Result : ~Copyable>
362367
Func UnsafeBufferPointer.withMemoryRebound(to:_:) is now without rethrows
363368
Func UnsafeMutableBufferPointer.withMemoryRebound(to:_:) has generic signature change from <Element, T, Result> to <Element, T, E, Result where E : Swift.Error, Element : ~Copyable, T : ~Copyable, Result : ~Copyable>

0 commit comments

Comments
 (0)