Skip to content

Commit 498b722

Browse files
authored
Merge pull request #22527 from Catfish-Man/literally-empty
2 parents 638c3ea + 490325c commit 498b722

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

stdlib/public/core/NativeDictionary.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ internal struct _NativeDictionary<Key: Hashable, Value> {
3737

3838
@inlinable
3939
internal init(capacity: Int) {
40-
self._storage = _DictionaryStorage<Key, Value>.allocate(capacity: capacity)
40+
if capacity == 0 {
41+
self._storage = __RawDictionaryStorage.empty
42+
} else {
43+
self._storage = _DictionaryStorage<Key, Value>.allocate(capacity: capacity)
44+
}
4145
}
4246

4347
#if _runtime(_ObjC)
@@ -48,13 +52,17 @@ internal struct _NativeDictionary<Key: Hashable, Value> {
4852

4953
@inlinable
5054
internal init(_ cocoa: __owned __CocoaDictionary, capacity: Int) {
51-
_internalInvariant(cocoa.count <= capacity)
52-
self._storage =
53-
_DictionaryStorage<Key, Value>.convert(cocoa, capacity: capacity)
54-
for (key, value) in cocoa {
55-
insertNew(
56-
key: _forceBridgeFromObjectiveC(key, Key.self),
57-
value: _forceBridgeFromObjectiveC(value, Value.self))
55+
if capacity == 0 {
56+
self._storage = __RawDictionaryStorage.empty
57+
} else {
58+
_internalInvariant(cocoa.count <= capacity)
59+
self._storage =
60+
_DictionaryStorage<Key, Value>.convert(cocoa, capacity: capacity)
61+
for (key, value) in cocoa {
62+
insertNew(
63+
key: _forceBridgeFromObjectiveC(key, Key.self),
64+
value: _forceBridgeFromObjectiveC(value, Value.self))
65+
}
5866
}
5967
}
6068
#endif

stdlib/public/core/NativeSet.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ internal struct _NativeSet<Element: Hashable> {
3636

3737
@inlinable
3838
internal init(capacity: Int) {
39-
self._storage = _SetStorage<Element>.allocate(capacity: capacity)
39+
if capacity == 0 {
40+
self._storage = __RawSetStorage.empty
41+
} else {
42+
self._storage = _SetStorage<Element>.allocate(capacity: capacity)
43+
}
4044
}
4145

4246
#if _runtime(_ObjC)
@@ -47,11 +51,15 @@ internal struct _NativeSet<Element: Hashable> {
4751

4852
@inlinable
4953
internal init(_ cocoa: __owned __CocoaSet, capacity: Int) {
50-
_internalInvariant(cocoa.count <= capacity)
51-
self._storage = _SetStorage<Element>.convert(cocoa, capacity: capacity)
52-
for element in cocoa {
53-
let nativeElement = _forceBridgeFromObjectiveC(element, Element.self)
54-
insertNew(nativeElement, isUnique: true)
54+
if capacity == 0 {
55+
self._storage = __RawSetStorage.empty
56+
} else {
57+
_internalInvariant(cocoa.count <= capacity)
58+
self._storage = _SetStorage<Element>.convert(cocoa, capacity: capacity)
59+
for element in cocoa {
60+
let nativeElement = _forceBridgeFromObjectiveC(element, Element.self)
61+
insertNew(nativeElement, isUnique: true)
62+
}
5563
}
5664
}
5765
#endif

validation-test/stdlib/Dictionary.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,7 +3010,7 @@ DictionaryTestSuite.test("BridgedFromObjC.Nonverbatim.RemoveAll") {
30103010
assert(d.count == 0)
30113011

30123012
let empty = Dictionary<Int, Int>()
3013-
expectNotEqual(empty._rawIdentifier(), d._rawIdentifier())
3013+
expectEqual(empty._rawIdentifier(), d._rawIdentifier())
30143014

30153015
d.removeAll()
30163016
assert(empty._rawIdentifier() == d._rawIdentifier())
@@ -3305,19 +3305,19 @@ DictionaryTestSuite.test("BridgedFromObjC.Nonverbatim.EqualityTest_Empty") {
33053305
var d2 = getBridgedNonverbatimEquatableDictionary([:])
33063306
let identity2 = d2._rawIdentifier()
33073307
assert(isNativeDictionary(d2))
3308-
assert(identity1 != identity2)
3308+
assert(identity1 == identity2)
33093309

33103310
assert(d1 == d2)
33113311
assert(identity1 == d1._rawIdentifier())
33123312
assert(identity2 == d2._rawIdentifier())
33133313

33143314
d2[TestBridgedKeyTy(10)] = TestBridgedEquatableValueTy(2010)
33153315
assert(isNativeDictionary(d2))
3316-
assert(identity2 == d2._rawIdentifier())
3316+
assert(identity2 != d2._rawIdentifier())
33173317

33183318
assert(d1 != d2)
33193319
assert(identity1 == d1._rawIdentifier())
3320-
assert(identity2 == d2._rawIdentifier())
3320+
assert(identity2 != d2._rawIdentifier())
33213321
}
33223322

33233323

validation-test/stdlib/Set.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ SetTestSuite.test("BridgedFromObjC.Nonverbatim.RemoveAll") {
19861986
expectEqual(0, s.count)
19871987

19881988
let emptySet = Set<Int>()
1989-
expectNotEqual(emptySet._rawIdentifier(), s._rawIdentifier())
1989+
expectEqual(emptySet._rawIdentifier(), s._rawIdentifier())
19901990

19911991
s.removeAll()
19921992
expectEqual(emptySet._rawIdentifier(), s._rawIdentifier())
@@ -2178,19 +2178,19 @@ SetTestSuite.test("BridgedFromObjC.Nonverbatim.EqualityTest_Empty") {
21782178
var s2 = getBridgedNonverbatimSet([])
21792179
let identity2 = s2._rawIdentifier()
21802180
expectTrue(isNativeSet(s2))
2181-
expectNotEqual(identity1, identity2)
2181+
expectEqual(identity1, identity2)
21822182

21832183
expectEqual(s1, s2)
21842184
expectEqual(identity1, s1._rawIdentifier())
21852185
expectEqual(identity2, s2._rawIdentifier())
21862186

21872187
s2.insert(TestObjCKeyTy(4040) as TestBridgedKeyTy)
21882188
expectTrue(isNativeSet(s2))
2189-
expectEqual(identity2, s2._rawIdentifier())
2189+
expectNotEqual(identity2, s2._rawIdentifier())
21902190

21912191
expectNotEqual(s1, s2)
21922192
expectEqual(identity1, s1._rawIdentifier())
2193-
expectEqual(identity2, s2._rawIdentifier())
2193+
expectNotEqual(identity2, s2._rawIdentifier())
21942194
}
21952195

21962196
SetTestSuite.test("BridgedFromObjC.Verbatim.EqualityTest_Small") {

0 commit comments

Comments
 (0)