mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Don't trap when creating a Set from a literal with duplicate elements
rdar://problem/19178760 Swift SVN r23791
This commit is contained in:
@@ -1882,11 +1882,16 @@ internal struct _Native${Self}Storage<${TypeParametersDecl}> :
|
||||
|
||||
%if Self == 'Set':
|
||||
|
||||
var count = 0
|
||||
for key in elements {
|
||||
var (i, found) = nativeStorage._find(key, nativeStorage._bucket(key))
|
||||
_precondition(!found, "${Self} literal contains duplicate keys")
|
||||
nativeStorage[i.offset] = Element(key: key)
|
||||
if found {
|
||||
continue
|
||||
}
|
||||
nativeStorage[i.offset] = Element(key: key)
|
||||
++count
|
||||
}
|
||||
nativeStorage.count = count
|
||||
|
||||
%elif Self == 'Dictionary':
|
||||
|
||||
@@ -1895,10 +1900,10 @@ internal struct _Native${Self}Storage<${TypeParametersDecl}> :
|
||||
_precondition(!found, "${Self} literal contains duplicate keys")
|
||||
nativeStorage[i.offset] = Element(key: key, value: value)
|
||||
}
|
||||
nativeStorage.count = elements.count
|
||||
|
||||
%end
|
||||
|
||||
nativeStorage.count = elements.count
|
||||
return nativeStorage
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2438,6 +2438,17 @@ SetTestSuite.test("init(SequenceType:)") {
|
||||
expectEqual(s1, s3)
|
||||
}
|
||||
|
||||
SetTestSuite.test("init(arrayLiteral:)") {
|
||||
let s1: Set<Int> = [1010, 2020, 3030, 1010, 2020, 3030]
|
||||
let s2 = Set([1010, 2020, 3030])
|
||||
var s3 = Set<Int>()
|
||||
s3.insert(1010)
|
||||
s3.insert(2020)
|
||||
s3.insert(3030)
|
||||
expectEqual(s1, s2)
|
||||
expectEqual(s2, s3)
|
||||
}
|
||||
|
||||
SetTestSuite.test("isSubsetOf.Set.Set") {
|
||||
let s1 = Set([1010, 2020, 3030, 4040, 5050, 6060])
|
||||
let s2 = Set([1010, 2020, 3030])
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
// RUN: xcrun -sdk %target-sdk-name clang++ -arch %target-cpu %S/Inputs/CatchCrashes.cpp -c -o %t/CatchCrashes.o
|
||||
// RUN: %target-build-swift %s -Xlinker %t/CatchCrashes.o -o %t/a.out
|
||||
//
|
||||
// RUN: %target-run %t/a.out DuplicateKeys1 2>&1 | FileCheck %s -check-prefix=CHECK
|
||||
// RUN: %target-run %t/a.out DuplicateKeys2 2>&1 | FileCheck %s -check-prefix=CHECK
|
||||
// RUN: %target-run %t/a.out DuplicateKeys3 2>&1 | FileCheck %s -check-prefix=CHECK
|
||||
// RUN: %target-run %t/a.out RemoveInvalidIndex1 2>&1 | FileCheck %s -check-prefix=CHECK
|
||||
// RUN: %target-run %t/a.out RemoveInvalidIndex2 2>&1 | FileCheck %s -check-prefix=CHECK
|
||||
// RUN: %target-run %t/a.out RemoveInvalidIndex3 2>&1 | FileCheck %s -check-prefix=CHECK
|
||||
@@ -72,21 +69,6 @@ if true {
|
||||
var nss: NSSet = s
|
||||
}
|
||||
|
||||
if arg == "DuplicateKeys1" {
|
||||
println("OK")
|
||||
let s: Set<Int> = [10, 20, 30, 10]
|
||||
}
|
||||
|
||||
if arg == "DuplicateKeys2" {
|
||||
println("OK")
|
||||
let s: Set<Int> = [10, 20, 30, 10]
|
||||
}
|
||||
|
||||
if arg == "DuplicateKeys3" {
|
||||
println("OK")
|
||||
var s: Set<Int> = [10, 10]
|
||||
}
|
||||
|
||||
if arg == "RemoveInvalidIndex1" {
|
||||
var s = Set<Int>()
|
||||
let index = s.startIndex
|
||||
|
||||
Reference in New Issue
Block a user