Skip to content

Commit 5f96d48

Browse files
authored
Merge pull request #943 from swiftwasm/master
[pull] swiftwasm from master
2 parents b48bee1 + 37657d0 commit 5f96d48

File tree

8 files changed

+65
-62
lines changed

8 files changed

+65
-62
lines changed

include/swift/Runtime/MutexPThread.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef pthread_rwlock_t ReadWriteLockHandle;
3131
// constexpr for static allocation versions. The way they define things
3232
// results in a reinterpret_cast which violates constexpr. Similarly, Android's
3333
// pthread implementation makes use of volatile attributes that prevent it from
34-
// being marked as constexpr.
34+
// being marked as constexpr. WASI currently doesn't support threading/locking at all.
3535
#define SWIFT_CONDITION_SUPPORTS_CONSTEXPR 0
3636
#define SWIFT_MUTEX_SUPPORTS_CONSTEXPR 0
3737
#define SWIFT_READWRITELOCK_SUPPORTS_CONSTEXPR 0

stdlib/public/core/ContiguousArrayBuffer.swift

+11-5
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,14 @@ internal func += <Element, C: Collection>(
607607
) where C.Element == Element {
608608

609609
let oldCount = lhs.count
610-
let newCount = oldCount + numericCast(rhs.count)
610+
let newCount = oldCount + rhs.count
611611

612612
let buf: UnsafeMutableBufferPointer<Element>
613613

614614
if _fastPath(newCount <= lhs.capacity) {
615-
buf = UnsafeMutableBufferPointer(start: lhs.firstElementAddress + oldCount, count: numericCast(rhs.count))
615+
buf = UnsafeMutableBufferPointer(
616+
start: lhs.firstElementAddress + oldCount,
617+
count: rhs.count)
616618
lhs.count = newCount
617619
}
618620
else {
@@ -624,7 +626,9 @@ internal func += <Element, C: Collection>(
624626
from: lhs.firstElementAddress, count: oldCount)
625627
lhs.count = 0
626628
(lhs, newLHS) = (newLHS, lhs)
627-
buf = UnsafeMutableBufferPointer(start: lhs.firstElementAddress + oldCount, count: numericCast(rhs.count))
629+
buf = UnsafeMutableBufferPointer(
630+
start: lhs.firstElementAddress + oldCount,
631+
count: rhs.count)
628632
}
629633

630634
var (remainders,writtenUpTo) = buf.initialize(from: rhs)
@@ -716,7 +720,7 @@ internal func _copyCollectionToContiguousArray<
716720
C: Collection
717721
>(_ source: C) -> ContiguousArray<C.Element>
718722
{
719-
let count: Int = numericCast(source.count)
723+
let count = source.count
720724
if count == 0 {
721725
return ContiguousArray()
722726
}
@@ -725,7 +729,9 @@ internal func _copyCollectionToContiguousArray<
725729
_uninitializedCount: count,
726730
minimumCapacity: 0)
727731

728-
let p = UnsafeMutableBufferPointer(start: result.firstElementAddress, count: count)
732+
let p = UnsafeMutableBufferPointer(
733+
start: result.firstElementAddress,
734+
count: count)
729735
var (itr, end) = source._copyContents(initializing: p)
730736

731737
_debugPrecondition(itr.next() == nil,

stdlib/public/core/ExistentialCollection.swift

+19-39
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -743,7 +743,7 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
743743
internal override func _index(
744744
_ i: _AnyIndexBox, offsetBy n: Int
745745
) -> _AnyIndexBox {
746-
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n)))
746+
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: n))
747747
}
748748

749749
@inlinable
@@ -752,10 +752,7 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
752752
offsetBy n: Int,
753753
limitedBy limit: _AnyIndexBox
754754
) -> _AnyIndexBox? {
755-
return _base.index(
756-
_unbox(i),
757-
offsetBy: numericCast(n),
758-
limitedBy: _unbox(limit))
755+
return _base.index(_unbox(i), offsetBy: n, limitedBy: _unbox(limit))
759756
.map { _IndexBox(_base: $0) }
760757
}
761758

@@ -764,7 +761,7 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
764761
_ i: inout _AnyIndexBox, offsetBy n: Int
765762
) {
766763
if let box = i as? _IndexBox<S.Index> {
767-
return _base.formIndex(&box._base, offsetBy: numericCast(n))
764+
return _base.formIndex(&box._base, offsetBy: n)
768765
}
769766
fatalError("Index type mismatch!")
770767
}
@@ -774,10 +771,7 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
774771
_ i: inout _AnyIndexBox, offsetBy n: Int, limitedBy limit: _AnyIndexBox
775772
) -> Bool {
776773
if let box = i as? _IndexBox<S.Index> {
777-
return _base.formIndex(
778-
&box._base,
779-
offsetBy: numericCast(n),
780-
limitedBy: _unbox(limit))
774+
return _base.formIndex(&box._base, offsetBy: n, limitedBy: _unbox(limit))
781775
}
782776
fatalError("Index type mismatch!")
783777
}
@@ -787,12 +781,12 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
787781
from start: _AnyIndexBox,
788782
to end: _AnyIndexBox
789783
) -> Int {
790-
return numericCast(_base.distance(from: _unbox(start), to: _unbox(end)))
784+
return _base.distance(from: _unbox(start), to: _unbox(end))
791785
}
792786

793787
@inlinable
794788
internal override var _count: Int {
795-
return numericCast(_base.count)
789+
return _base.count
796790
}
797791

798792
@usableFromInline
@@ -949,7 +943,7 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
949943
internal override func _index(
950944
_ i: _AnyIndexBox, offsetBy n: Int
951945
) -> _AnyIndexBox {
952-
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n)))
946+
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: n))
953947
}
954948

955949
@inlinable
@@ -958,11 +952,7 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
958952
offsetBy n: Int,
959953
limitedBy limit: _AnyIndexBox
960954
) -> _AnyIndexBox? {
961-
return _base.index(
962-
_unbox(i),
963-
offsetBy: numericCast(n),
964-
limitedBy: _unbox(limit)
965-
)
955+
return _base.index(_unbox(i), offsetBy: n, limitedBy: _unbox(limit))
966956
.map { _IndexBox(_base: $0) }
967957
}
968958

@@ -971,7 +961,7 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
971961
_ i: inout _AnyIndexBox, offsetBy n: Int
972962
) {
973963
if let box = i as? _IndexBox<S.Index> {
974-
return _base.formIndex(&box._base, offsetBy: numericCast(n))
964+
return _base.formIndex(&box._base, offsetBy: n)
975965
}
976966
fatalError("Index type mismatch!")
977967
}
@@ -981,10 +971,7 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
981971
_ i: inout _AnyIndexBox, offsetBy n: Int, limitedBy limit: _AnyIndexBox
982972
) -> Bool {
983973
if let box = i as? _IndexBox<S.Index> {
984-
return _base.formIndex(
985-
&box._base,
986-
offsetBy: numericCast(n),
987-
limitedBy: _unbox(limit))
974+
return _base.formIndex(&box._base, offsetBy: n, limitedBy: _unbox(limit))
988975
}
989976
fatalError("Index type mismatch!")
990977
}
@@ -994,12 +981,12 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
994981
from start: _AnyIndexBox,
995982
to end: _AnyIndexBox
996983
) -> Int {
997-
return numericCast(_base.distance(from: _unbox(start), to: _unbox(end)))
984+
return _base.distance(from: _unbox(start), to: _unbox(end))
998985
}
999986

1000987
@inlinable
1001988
internal override var _count: Int {
1002-
return numericCast(_base.count)
989+
return _base.count
1003990
}
1004991

1005992
@inlinable
@@ -1168,7 +1155,7 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
11681155
internal override func _index(
11691156
_ i: _AnyIndexBox, offsetBy n: Int
11701157
) -> _AnyIndexBox {
1171-
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n)))
1158+
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: n))
11721159
}
11731160

11741161
@inlinable
@@ -1177,11 +1164,7 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
11771164
offsetBy n: Int,
11781165
limitedBy limit: _AnyIndexBox
11791166
) -> _AnyIndexBox? {
1180-
return _base.index(
1181-
_unbox(i),
1182-
offsetBy: numericCast(n),
1183-
limitedBy: _unbox(limit)
1184-
)
1167+
return _base.index(_unbox(i), offsetBy: n, limitedBy: _unbox(limit))
11851168
.map { _IndexBox(_base: $0) }
11861169
}
11871170

@@ -1190,7 +1173,7 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
11901173
_ i: inout _AnyIndexBox, offsetBy n: Int
11911174
) {
11921175
if let box = i as? _IndexBox<S.Index> {
1193-
return _base.formIndex(&box._base, offsetBy: numericCast(n))
1176+
return _base.formIndex(&box._base, offsetBy: n)
11941177
}
11951178
fatalError("Index type mismatch!")
11961179
}
@@ -1200,10 +1183,7 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
12001183
_ i: inout _AnyIndexBox, offsetBy n: Int, limitedBy limit: _AnyIndexBox
12011184
) -> Bool {
12021185
if let box = i as? _IndexBox<S.Index> {
1203-
return _base.formIndex(
1204-
&box._base,
1205-
offsetBy: numericCast(n),
1206-
limitedBy: _unbox(limit))
1186+
return _base.formIndex(&box._base, offsetBy: n, limitedBy: _unbox(limit))
12071187
}
12081188
fatalError("Index type mismatch!")
12091189
}
@@ -1213,12 +1193,12 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
12131193
from start: _AnyIndexBox,
12141194
to end: _AnyIndexBox
12151195
) -> Int {
1216-
return numericCast(_base.distance(from: _unbox(start), to: _unbox(end)))
1196+
return _base.distance(from: _unbox(start), to: _unbox(end))
12171197
}
12181198

12191199
@inlinable
12201200
internal override var _count: Int {
1221-
return numericCast(_base.count)
1201+
return _base.count
12221202
}
12231203

12241204
@inlinable

stdlib/public/core/Filter.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -231,7 +231,7 @@ extension LazyFilterCollection: Collection {
231231
// _base at least once, to trigger a _precondition in forward only
232232
// collections.
233233
_ensureBidirectional(step: step)
234-
for _ in 0 ..< abs(numericCast(n)) {
234+
for _ in 0 ..< abs(n) {
235235
_advanceIndex(&i, step: step)
236236
}
237237
return i
@@ -252,7 +252,7 @@ extension LazyFilterCollection: Collection {
252252
// invoked on the _base at least once, to trigger a _precondition in
253253
// forward only collections.
254254
_ensureBidirectional(step: step)
255-
for _ in 0 ..< abs(numericCast(n)) {
255+
for _ in 0 ..< abs(n) {
256256
if i == limit {
257257
return nil
258258
}

stdlib/public/core/RangeReplaceableCollection.swift

+3-4
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,7 @@ extension RangeReplaceableCollection {
449449
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
450450
where S.Element == Element {
451451

452-
let approximateCapacity = self.count +
453-
numericCast(newElements.underestimatedCount)
452+
let approximateCapacity = self.count + newElements.underestimatedCount
454453
self.reserveCapacity(approximateCapacity)
455454
for element in newElements {
456455
append(element)
@@ -800,7 +799,7 @@ extension RangeReplaceableCollection
800799

801800
@inlinable
802801
public mutating func _customRemoveLast(_ n: Int) -> Bool {
803-
self = self[startIndex..<index(endIndex, offsetBy: numericCast(-n))]
802+
self = self[startIndex..<index(endIndex, offsetBy: -n)]
804803
return true
805804
}
806805
}
@@ -998,7 +997,7 @@ extension RangeReplaceableCollection {
998997
>(lhs: Other, rhs: Self) -> Self
999998
where Element == Other.Element {
1000999
var result = Self()
1001-
result.reserveCapacity(rhs.count + numericCast(lhs.underestimatedCount))
1000+
result.reserveCapacity(rhs.count + lhs.underestimatedCount)
10021001
result.append(contentsOf: lhs)
10031002
result.append(contentsOf: rhs)
10041003
return result

stdlib/public/core/Slice.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -337,7 +337,7 @@ extension Slice: RangeReplaceableCollection
337337
let newSliceCount =
338338
_base.distance(from: _startIndex, to: subRange.lowerBound)
339339
+ _base.distance(from: subRange.upperBound, to: _endIndex)
340-
+ (numericCast(newElements.count) as Int)
340+
+ newElements.count
341341
_base.replaceSubrange(subRange, with: newElements)
342342
_startIndex = _base.index(_base.startIndex, offsetBy: sliceOffset)
343343
_endIndex = _base.index(_startIndex, offsetBy: newSliceCount)
@@ -400,7 +400,7 @@ extension Slice
400400
let newSliceCount =
401401
_base.distance(from: _startIndex, to: subRange.lowerBound)
402402
+ _base.distance(from: subRange.upperBound, to: _endIndex)
403-
+ (numericCast(newElements.count) as Int)
403+
+ newElements.count
404404
_base.replaceSubrange(subRange, with: newElements)
405405
_startIndex = _base.startIndex
406406
_endIndex = _base.index(_startIndex, offsetBy: newSliceCount)
@@ -409,7 +409,7 @@ extension Slice
409409
let lastValidIndex = _base.index(before: subRange.lowerBound)
410410
let newEndIndexOffset =
411411
_base.distance(from: subRange.upperBound, to: _endIndex)
412-
+ (numericCast(newElements.count) as Int) + 1
412+
+ newElements.count + 1
413413
_base.replaceSubrange(subRange, with: newElements)
414414
if shouldUpdateStartIndex {
415415
_startIndex = _base.index(after: lastValidIndex)
@@ -443,7 +443,7 @@ extension Slice
443443
where S: Collection, S.Element == Base.Element {
444444
// FIXME: swift-3-indexing-model: range check.
445445
if i == _base.startIndex {
446-
let newSliceCount = count + numericCast(newElements.count)
446+
let newSliceCount = count + newElements.count
447447
_base.insert(contentsOf: newElements, at: i)
448448
_startIndex = _base.startIndex
449449
_endIndex = _base.index(_startIndex, offsetBy: newSliceCount)
@@ -452,7 +452,7 @@ extension Slice
452452
let lastValidIndex = _base.index(before: i)
453453
let newEndIndexOffset =
454454
_base.distance(from: i, to: _endIndex)
455-
+ numericCast(newElements.count) + 1
455+
+ newElements.count + 1
456456
_base.insert(contentsOf: newElements, at: i)
457457
if shouldUpdateStartIndex {
458458
_startIndex = _base.index(after: lastValidIndex)

stdlib/public/core/SliceBuffer.swift

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -116,8 +116,7 @@ internal struct _SliceBuffer<Element>
116116
/// the given collection.
117117
///
118118
/// - Precondition: This buffer is backed by a uniquely-referenced
119-
/// `_ContiguousArrayBuffer` and
120-
/// `insertCount <= numericCast(newValues.count)`.
119+
/// `_ContiguousArrayBuffer` and `insertCount <= newValues.count`.
121120
@inlinable
122121
internal mutating func replaceSubrange<C>(
123122
_ subrange: Range<Int>,
@@ -126,7 +125,7 @@ internal struct _SliceBuffer<Element>
126125
) where C: Collection, C.Element == Element {
127126

128127
_invariantCheck()
129-
_internalInvariant(insertCount <= numericCast(newValues.count))
128+
_internalInvariant(insertCount <= newValues.count)
130129

131130
_internalInvariant(_hasNativeBuffer)
132131
_internalInvariant(isUniquelyReferenced())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: not --crash %target-swift-frontend -emit-sil -verify %s
2+
// REQUIRES: asserts
3+
4+
// SR-12744: Pullback generation crash for unhandled indirect result.
5+
// May be due to inconsistent derivative function type calculation logic in
6+
// `VJPEmitter::createEmptyPullback`.
7+
8+
import _Differentiation
9+
10+
class Class: Differentiable {
11+
@differentiable(wrt: (self, x))
12+
@differentiable(wrt: x)
13+
func f(_ x: Float) -> Float { x }
14+
}
15+
16+
func test<C: Class>(_ c: C, _ x: Float) {
17+
_ = gradient(at: c, x) { c, x in c.f(x) }
18+
_ = gradient(at: x) { x in c.f(x) }
19+
}

0 commit comments

Comments
 (0)