Skip to content

Commit 14dc86c

Browse files
committed
Polish off uses of dynamicType in codebase
1 parent 5358c11 commit 14dc86c

19 files changed

+51
-51
lines changed

docs/archive/LangRef.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ <h3 id="type-metatype">Metatype Types</h3>
12551255
type-metatype ::= type-simple '.' 'Type'
12561256
</pre>
12571257

1258-
<p>Every type has an associated metatype <tt>T.dynamicType</tt>. A value of the metatype
1258+
<p>Every type has an associated metatype <tt>type(of: T)</tt>. A value of the metatype
12591259
type is a reference to a global object which describes the type.
12601260
Most metatype types are singleton and therefore require no
12611261
storage, but metatypes associated with <a href="#decl-class">class

include/swift/AST/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3517,7 +3517,7 @@ class AutoClosureExpr : public AbstractClosureExpr {
35173517
}
35183518
};
35193519

3520-
/// DynamicTypeExpr - "base.dynamicType" - Produces a metatype value.
3520+
/// DynamicTypeExpr - "type(of: base)" - Produces a metatype value.
35213521
///
35223522
/// The metatype value can comes from evaluating an expression and then
35233523
/// getting its metatype.

lib/Sema/CSDiag.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,14 +2267,14 @@ bool FailureDiagnosis::diagnoseGeneralMemberFailure(Constraint *constraint) {
22672267
return true;
22682268
}
22692269

2270-
// Suggest inserting '.dynamicType' to construct another object of the
2271-
// same dynamic type.
2272-
SourceLoc fixItLoc
2273-
= ctorRef->getNameLoc().getBaseNameLoc().getAdvancedLoc(-1);
2270+
// Suggest inserting a call to 'type(of:)' to construct another object
2271+
// of the same dynamic type.
2272+
SourceRange fixItRng = ctorRef->getNameLoc().getSourceRange();
22742273

2275-
// Place the '.dynamicType' right before the init.
2274+
// Surround the caller in `type(of:)`.
22762275
diagnose(anchor->getLoc(), diag::init_not_instance_member)
2277-
.fixItInsert(fixItLoc, ".dynamicType");
2276+
.fixItInsert(fixItRng.Start, "type(of: ")
2277+
.fixItInsertAfter(fixItRng.End, ")");
22782278
return true;
22792279
}
22802280
}

stdlib/private/StdlibCollectionUnittest/CheckMutableCollectionType.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,10 @@ self.test("\(testNamePrefix).sorted/DispatchesThrough_withUnsafeMutableBufferPoi
491491
// This sort operation is not in-place.
492492
// The collection is copied into an array before sorting.
493493
expectEqual(
494-
0, lc.log._withUnsafeMutableBufferPointerIfSupported[lc.dynamicType])
494+
0, lc.log._withUnsafeMutableBufferPointerIfSupported[type(of: lc)])
495495
expectEqual(
496496
0,
497-
lc.log._withUnsafeMutableBufferPointerIfSupportedNonNilReturns[lc.dynamicType])
497+
lc.log._withUnsafeMutableBufferPointerIfSupportedNonNilReturns[type(of: lc)])
498498

499499
expectEqualSequence([ 1, 2, 3, 4, 5 ], extractedResult.map { $0.value })
500500
}
@@ -808,10 +808,10 @@ self.test("\(testNamePrefix).partition/DispatchesThrough_withUnsafeMutableBuffer
808808
})
809809

810810
expectEqual(
811-
1, lc.log._withUnsafeMutableBufferPointerIfSupported[lc.dynamicType])
811+
1, lc.log._withUnsafeMutableBufferPointerIfSupported[type(of: lc)])
812812
expectEqual(
813813
withUnsafeMutableBufferPointerIsSupported ? 1 : 0,
814-
lc.log._withUnsafeMutableBufferPointerIfSupportedNonNilReturns[lc.dynamicType])
814+
lc.log._withUnsafeMutableBufferPointerIfSupportedNonNilReturns[type(of: lc)])
815815

816816
expectEqual(4, lc.distance(from: lc.startIndex, to: pivot))
817817
expectEqualsUnordered([1, 2, 3, 4], lc.prefix(upTo: pivot).map { extractValue($0).value })

stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension LoggingType {
3232
}
3333

3434
public var selfType: Any.Type {
35-
return self.dynamicType
35+
return type(of: self)
3636
}
3737
}
3838

stdlib/private/StdlibCollectionUnittest/MinimalCollections.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ public struct ${Self}<T> : ${SelfProtocols} {
555555
self.underestimatedCount = 0
556556
self._elements = []
557557
self._collectionState =
558-
_CollectionState(name: "\(self.dynamicType)", elementCount: 0)
558+
_CollectionState(name: "\(type(of: self))", elementCount: 0)
559559
}
560560

561561
public init<S : Sequence>(_ elements: S) where S.Iterator.Element == T {

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ public func expectEqualTest<T>(
168168
) {
169169
if !equal(expected, actual) {
170170
expectationFailure(
171-
"expected: \(String(reflecting: expected)) (of type \(String(reflecting: expected.dynamicType)))\n"
172-
+ "actual: \(String(reflecting: actual)) (of type \(String(reflecting: actual.dynamicType)))",
171+
"expected: \(String(reflecting: expected)) (of type \(String(reflecting: type(of: expected))))\n"
172+
+ "actual: \(String(reflecting: actual)) (of type \(String(reflecting: type(of: actual))))",
173173
trace: ${trace}
174174
)
175175
}
@@ -178,7 +178,7 @@ public func expectEqualTest<T>(
178178
public func expectNotEqual<T : Equatable>(_ expected: T, _ actual: T, ${TRACE}) {
179179
if expected == actual {
180180
expectationFailure(
181-
"unexpected value: \"\(actual)\" (of type \(String(reflecting: actual.dynamicType)))",
181+
"unexpected value: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
182182
trace: ${trace}
183183
)
184184
}
@@ -197,17 +197,17 @@ public func expectOptionalEqual<T>(
197197
) {
198198
if (actual == nil) || !equal(expected, actual!) {
199199
expectationFailure(
200-
"expected: \"\(expected)\" (of type \(String(reflecting: expected.dynamicType)))\n"
201-
+ "actual: \"\(actual)\" (of type \(String(reflecting: actual.dynamicType)))",
200+
"expected: \"\(expected)\" (of type \(String(reflecting: type(of: expected))))\n"
201+
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
202202
trace: ${trace})
203203
}
204204
}
205205

206206
public func expectEqual<T : Equatable>(_ expected: T?, _ actual: T?, ${TRACE}) {
207207
if expected != actual {
208208
expectationFailure(
209-
"expected: \"\(expected)\" (of type \(String(reflecting: expected.dynamicType)))\n"
210-
+ "actual: \"\(actual)\" (of type \(String(reflecting: actual.dynamicType)))",
209+
"expected: \"\(expected)\" (of type \(String(reflecting: type(of: expected))))\n"
210+
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
211211
trace: ${trace})
212212
}
213213
}
@@ -217,7 +217,7 @@ public func expectNotEqual<T : Equatable>(
217217
) {
218218
if expected == actual {
219219
expectationFailure(
220-
"unexpected value: \"\(actual)\" (of type \(String(reflecting: actual.dynamicType)))",
220+
"unexpected value: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
221221
trace: ${trace})
222222
}
223223
}
@@ -240,8 +240,8 @@ public func expectOptionalEqual${Generic}(
240240
_ expected: ${EquatableType}, _ actual: ${EquatableType}?, ${TRACE}) {
241241
if (actual == nil) || expected != actual! {
242242
expectationFailure(
243-
"expected: \"\(expected)\" (of type \(String(reflecting: expected.dynamicType)))"
244-
+ "actual: \"\(actual)\" (of type \(String(reflecting: actual.dynamicType)))",
243+
"expected: \"\(expected)\" (of type \(String(reflecting: type(of: expected))))"
244+
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
245245
trace: ${trace})
246246
}
247247
}
@@ -2270,7 +2270,7 @@ public func expectEqualSequence<
22702270

22712271
if !expected.elementsEqual(actual, by: sameValue) {
22722272
expectationFailure("expected elements: \"\(expected)\"\n"
2273-
+ "actual: \"\(actual)\" (of type \(String(reflecting: actual.dynamicType)))",
2273+
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
22742274
trace: ${trace})
22752275
}
22762276
}
@@ -2333,15 +2333,15 @@ public func expectEqualsUnordered<T : Strideable>(
23332333
) {
23342334
if numericCast(expected.count) != actual.count {
23352335
expectationFailure("expected elements: \"\(expected)\"\n"
2336-
+ "actual: \"\(actual)\" (of type \(String(reflecting: actual.dynamicType)))",
2336+
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
23372337
trace: ${trace})
23382338
}
23392339
let r = Range(uncheckedBounds:
23402340
(lower: expected.lowerBound, upper: expected.upperBound))
23412341
for e in actual {
23422342
if !r.contains(e) {
23432343
expectationFailure("expected elements: \"\(expected)\"\n"
2344-
+ "actual: \"\(actual)\" (of type \(String(reflecting: actual.dynamicType)))",
2344+
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
23452345
trace: ${trace})
23462346
}
23472347
}

stdlib/public/SDK/Foundation/Measurement.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension Measurement where UnitType : Dimension {
6767
return Measurement(value: value, unit: otherUnit)
6868
} else {
6969
let valueInTermsOfBase = unit.converter.baseUnitValue(fromValue: value)
70-
if otherUnit.isEqual(unit.dynamicType.baseUnit()) {
70+
if otherUnit.isEqual(type(of: unit).baseUnit()) {
7171
return Measurement(value: valueInTermsOfBase, unit: otherUnit)
7272
} else {
7373
let otherValueFromTermsOfBase = otherUnit.converter.value(fromBaseUnitValue: valueInTermsOfBase)
@@ -93,7 +93,7 @@ extension Measurement where UnitType : Dimension {
9393
} else {
9494
let lhsValueInTermsOfBase = lhs.unit.converter.baseUnitValue(fromValue: lhs.value)
9595
let rhsValueInTermsOfBase = rhs.unit.converter.baseUnitValue(fromValue: rhs.value)
96-
return Measurement(value: lhsValueInTermsOfBase + rhsValueInTermsOfBase, unit: lhs.unit.dynamicType.baseUnit())
96+
return Measurement(value: lhsValueInTermsOfBase + rhsValueInTermsOfBase, unit: lhs.type(of: unit).baseUnit())
9797
}
9898
}
9999

@@ -107,7 +107,7 @@ extension Measurement where UnitType : Dimension {
107107
} else {
108108
let lhsValueInTermsOfBase = lhs.unit.converter.baseUnitValue(fromValue: lhs.value)
109109
let rhsValueInTermsOfBase = rhs.unit.converter.baseUnitValue(fromValue: rhs.value)
110-
return Measurement(value: lhsValueInTermsOfBase - rhsValueInTermsOfBase, unit: lhs.unit.dynamicType.baseUnit())
110+
return Measurement(value: lhsValueInTermsOfBase - rhsValueInTermsOfBase, unit: lhs.type(of: unit).baseUnit())
111111
}
112112
}
113113

stdlib/public/core/AnyHashable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal struct _ConcreteHashableBox<Base : Hashable> : _AnyHashableBox {
5454
}
5555

5656
internal var _typeID: ObjectIdentifier {
57-
return ObjectIdentifier(self.dynamicType)
57+
return ObjectIdentifier(type(of: self))
5858
}
5959

6060
internal func _unbox<T : Hashable>() -> T? {

stdlib/public/core/Builtin.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ internal func _makeBridgeObject(
488488

489489
_sanityCheck(
490490
_isObjCTaggedPointer(object)
491-
|| _usesNativeSwiftReferenceCounting(object.dynamicType)
491+
|| _usesNativeSwiftReferenceCounting(type(of: object))
492492
|| bits == _objectPointerIsObjCBit,
493493
"All spare bits must be set in non-native, non-tagged bridge objects"
494494
)
@@ -565,7 +565,7 @@ func _isUnique_native<T>(_ object: inout T) -> Bool {
565565
(_bitPattern(Builtin.reinterpretCast(object)) & _objectPointerSpareBits)
566566
== 0)
567567
_sanityCheck(_usesNativeSwiftReferenceCounting(
568-
(Builtin.reinterpretCast(object) as AnyObject).dynamicType))
568+
type(of: Builtin.reinterpretCast(object) as AnyObject)))
569569
return Bool(Builtin.isUnique_native(&object))
570570
}
571571

@@ -580,7 +580,7 @@ func _isUniqueOrPinned_native<T>(_ object: inout T) -> Bool {
580580
(_bitPattern(Builtin.reinterpretCast(object)) & _objectPointerSpareBits)
581581
== 0)
582582
_sanityCheck(_usesNativeSwiftReferenceCounting(
583-
(Builtin.reinterpretCast(object) as AnyObject).dynamicType))
583+
type(of: Builtin.reinterpretCast(object) as AnyObject)))
584584
return Bool(Builtin.isUniqueOrPinned_native(&object))
585585
}
586586

@@ -606,8 +606,8 @@ public func unsafeUnwrap<T>(_ nonEmpty: T?) -> T {
606606

607607
/// Extract an object reference from an Any known to contain an object.
608608
internal func _unsafeDowncastToAnyObject(fromAny any: Any) -> AnyObject {
609-
_sanityCheck(any.dynamicType is AnyObject.Type
610-
|| any.dynamicType is AnyObject.Protocol,
609+
_sanityCheck(type(of: any) is AnyObject.Type
610+
|| type(of: any) is AnyObject.Protocol,
611611
"Any expected to contain object reference")
612612
// With a SIL instruction, we could more efficiently grab the object reference
613613
// out of the Any's inline storage.

stdlib/public/core/DebuggerSupport.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public enum _DebuggerSupport {
3939
}
4040

4141
internal static func isClass(_ value: Any) -> Bool {
42-
if let _ = value.dynamicType as? AnyClass {
42+
if let _ = type(of: value) as? AnyClass {
4343
return true
4444
}
4545
return false
@@ -119,7 +119,7 @@ public enum _DebuggerSupport {
119119
return csc.description
120120
}
121121
// for a Class with no custom summary, mimic the Foundation default
122-
return "<\(x.dynamicType): 0x\(String(asNumericValue(x), radix: 16, uppercase: false))>"
122+
return "<\(type(of: x)): 0x\(String(asNumericValue(x), radix: 16, uppercase: false))>"
123123
} else {
124124
// but if I can't provide a value, just use the type anyway
125125
return "\(mirror.subjectType)"

stdlib/public/core/ErrorType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public typealias ErrorProtocol = Error
169169

170170
extension Error {
171171
public var _domain: String {
172-
return String(reflecting: self.dynamicType)
172+
return String(reflecting: type(of: self))
173173
}
174174

175175
public var _userInfo: Any? {

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ internal final class _IndexBox<
664664
}
665665

666666
internal var _typeID: ObjectIdentifier {
667-
return ObjectIdentifier(self.dynamicType)
667+
return ObjectIdentifier(type(of: self))
668668
}
669669

670670
internal func _unbox<T : Comparable>() -> T? {

stdlib/public/core/HeapBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct _HeapBuffer<Value, Element> : Equatable {
137137

138138
internal init(_ storage: AnyObject) {
139139
_sanityCheck(
140-
_usesNativeSwiftReferenceCounting(storage.dynamicType),
140+
_usesNativeSwiftReferenceCounting(type(of: storage)),
141141
"HeapBuffer manages only native objects"
142142
)
143143
self._storage = Builtin.castToNativeObject(storage)

stdlib/public/core/ManagedBuffer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
205205
/// - Precondition: `buffer` is an instance of a non-`@objc` class whose
206206
/// `deinit` destroys its stored `Header` and any constructed `Element`s.
207207
public init(unsafeBufferObject buffer: AnyObject) {
208-
ManagedBufferPointer._checkValidBufferClass(buffer.dynamicType)
208+
ManagedBufferPointer._checkValidBufferClass(type(of: buffer))
209209

210210
self._nativeBuffer = Builtin.castToNativeObject(buffer)
211211
}
@@ -220,7 +220,7 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
220220
/// it in this specialized constructor.
221221
@_versioned
222222
internal init(_uncheckedUnsafeBufferObject buffer: AnyObject) {
223-
ManagedBufferPointer._sanityCheckValidBufferClass(buffer.dynamicType)
223+
ManagedBufferPointer._sanityCheckValidBufferClass(type(of: buffer))
224224
self._nativeBuffer = Builtin.castToNativeObject(buffer)
225225
}
226226

stdlib/public/core/Mirror.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public struct Mirror {
100100
} else {
101101
self = Mirror(
102102
legacy: _reflect(subject),
103-
subjectType: subject.dynamicType)
103+
subjectType: type(of: subject))
104104
}
105105
}
106106

@@ -147,7 +147,7 @@ public struct Mirror {
147147
_ subject: AnyObject, asClass targetSuperclass: AnyClass) -> _Mirror? {
148148

149149
// get a legacy mirror and the most-derived type
150-
var cls: AnyClass = subject.dynamicType
150+
var cls: AnyClass = type(of: subject)
151151
var clsMirror = _reflect(subject)
152152

153153
// Walk up the chain of mirrors/classes until we find staticSubclass
@@ -490,7 +490,7 @@ extension _Mirror {
490490
internal func _superMirror() -> _Mirror? {
491491
if self.count > 0 {
492492
let childMirror = self[0].1
493-
if _isClassSuperMirror(childMirror.dynamicType) {
493+
if _isClassSuperMirror(type(of: childMirror)) {
494494
return childMirror
495495
}
496496
}

stdlib/public/core/OutputStream.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ internal func _print_unlocked<T, TargetStream : TextOutputStream>(
338338
// string. Check for Optional first, before checking protocol
339339
// conformance below, because an Optional value is convertible to a
340340
// protocol if its wrapped type conforms to that protocol.
341-
if _isOptional(value.dynamicType) {
341+
if _isOptional(type(of: value)) {
342342
let debugPrintable = value as! CustomDebugStringConvertible
343343
debugPrintable.debugDescription.write(to: &target)
344344
return

stdlib/public/core/Policy.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public typealias FloatLiteralType = Double
7979
/// `BooleanLiteralType` alias. For example:
8080
///
8181
/// let isBool = true
82-
/// print("isBool is a '\(isBool.dynamicType)'")
82+
/// print("isBool is a '\(type(of: isBool))'")
8383
/// // Prints "isBool is a 'Bool'"
8484
///
8585
/// The type aliased by `BooleanLiteralType` must conform to the
@@ -143,7 +143,7 @@ public typealias _MaxBuiltinFloatType = Builtin.FPIEEE64
143143
/// // Prints "true"
144144
///
145145
/// let v: AnyObject = 100
146-
/// print(v.dynamicType)
146+
/// print(type(of: v))
147147
/// // Prints "__NSCFNumber"
148148
///
149149
/// The flexible behavior of the `AnyObject` protocol is similar to

stdlib/public/core/Reflection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public protocol _Mirror {
103103
/// The instance being reflected.
104104
var value: Any { get }
105105

106-
/// Identical to `value.dynamicType`.
106+
/// Identical to `type(of: value)`.
107107
var valueType: Any.Type { get }
108108

109109
/// A unique identifier for `value` if it is a class instance; `nil`
@@ -214,7 +214,7 @@ internal func _dump_unlocked<TargetStream : TextOutputStream>(
214214
_dumpPrint_unlocked(value, mirror, &target)
215215

216216
let id: ObjectIdentifier?
217-
if value.dynamicType is AnyObject.Type {
217+
if type(of: value) is AnyObject.Type {
218218
// Object is a class (but not an ObjC-bridged struct)
219219
id = ObjectIdentifier(_unsafeDowncastToAnyObject(fromAny: value))
220220
} else if let metatypeInstance = value as? Any.Type {

0 commit comments

Comments
 (0)