Skip to content

Commit 801385a

Browse files
committed
[stdlib] Generalize the signature of type(of:)
`func type(of:)` is wholly magical, but it does have a signature in the stdlib (mostly for documentation purposes), and it currently requires its input to be copyable and escapable. `type(of:)` is actually usable on all entities, so it seems desirable to update its signature to reflect this. Additionally, this seems like a good time to mark its exported symbol obsolete. I don’t expect anyone would ever link to it (unless there is/was a bug), so in theory we could also silently remove it — but explicitly marking it as legacy ABI seems the least risky option.
1 parent 7cd98c3 commit 801385a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

stdlib/public/core/Builtin.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,11 @@ func _trueAfterDiagnostics() -> Builtin.Int1 {
940940
///
941941
/// - Parameter value: The value for which to find the dynamic type.
942942
/// - Returns: The dynamic type, which is a metatype instance.
943-
@_transparent
943+
@_alwaysEmitIntoClient
944944
@_semantics("typechecker.type(of:)")
945-
public func type<T, Metatype>(of value: T) -> Metatype {
945+
public func type<T: ~Copyable & ~Escapable, Metatype>(
946+
of value: borrowing T
947+
) -> Metatype {
946948
// This implementation is never used, since calls to `Swift.type(of:)` are
947949
// resolved as a special case by the type checker.
948950
unsafe Builtin.staticReport(_trueAfterDiagnostics(), true._value,
@@ -951,6 +953,17 @@ public func type<T, Metatype>(of value: T) -> Metatype {
951953
Builtin.unreachable()
952954
}
953955

956+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
957+
@_silgen_name("$ss4type2ofq_x_tr0_lF")
958+
@usableFromInline
959+
func __abi_type<T, Metatype>(of value: T) -> Metatype {
960+
// This is a legacy entry point for the original definition of `type(of:)`
961+
// that the stdlib originally exported for no good reason. The current
962+
// definition no longer exports a symbol, and nothing is expected to link to
963+
// it, but we keep it around anyway.
964+
Builtin.unreachable()
965+
}
966+
954967
/// Allows a nonescaping closure to temporarily be used as if it were allowed
955968
/// to escape.
956969
///

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,6 @@ Protocol Error has added inherited protocol SendableMetatype
370370
Accessor Optional.unsafelyUnwrapped.Get() has generic signature change from <Wrapped> to <Wrapped where Wrapped : ~Escapable>
371371
Accessor UnsafeBufferPointer.indices.Get() has generic signature change from <Element> to <Element where Element : ~Copyable>
372372
Accessor UnsafeMutableBufferPointer.indices.Get() has generic signature change from <Element> to <Element where Element : ~Copyable>
373+
374+
Func type(of:) has generic signature change from <T, Metatype> to <T, Metatype where T : ~Copyable, T : ~Escapable>
375+
Func type(of:) has parameter 0 changing from Default to Shared

0 commit comments

Comments
 (0)