diff --git a/include/swift/Runtime/MutexPThread.h b/include/swift/Runtime/MutexPThread.h index 1b06a2977bd06..132c580013f4d 100644 --- a/include/swift/Runtime/MutexPThread.h +++ b/include/swift/Runtime/MutexPThread.h @@ -26,12 +26,12 @@ typedef pthread_cond_t ConditionHandle; typedef pthread_mutex_t MutexHandle; typedef pthread_rwlock_t ReadWriteLockHandle; -#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) +#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) || defined(__wasi__) // At the moment CYGWIN pthreads implementation doesn't support the use of // constexpr for static allocation versions. The way they define things // results in a reinterpret_cast which violates constexpr. Similarly, Android's // pthread implementation makes use of volatile attributes that prevent it from -// being marked as constexpr. +// being marked as constexpr. WASI currently doesn't support threading/locking at all. #define SWIFT_CONDITION_SUPPORTS_CONSTEXPR 0 #define SWIFT_MUTEX_SUPPORTS_CONSTEXPR 0 #define SWIFT_READWRITELOCK_SUPPORTS_CONSTEXPR 0 diff --git a/stdlib/public/core/ContiguousArrayBuffer.swift b/stdlib/public/core/ContiguousArrayBuffer.swift index 6e2c638c1b731..e06b139eba596 100644 --- a/stdlib/public/core/ContiguousArrayBuffer.swift +++ b/stdlib/public/core/ContiguousArrayBuffer.swift @@ -607,12 +607,14 @@ internal func += ( ) where C.Element == Element { let oldCount = lhs.count - let newCount = oldCount + numericCast(rhs.count) + let newCount = oldCount + rhs.count let buf: UnsafeMutableBufferPointer if _fastPath(newCount <= lhs.capacity) { - buf = UnsafeMutableBufferPointer(start: lhs.firstElementAddress + oldCount, count: numericCast(rhs.count)) + buf = UnsafeMutableBufferPointer( + start: lhs.firstElementAddress + oldCount, + count: rhs.count) lhs.count = newCount } else { @@ -624,7 +626,9 @@ internal func += ( from: lhs.firstElementAddress, count: oldCount) lhs.count = 0 (lhs, newLHS) = (newLHS, lhs) - buf = UnsafeMutableBufferPointer(start: lhs.firstElementAddress + oldCount, count: numericCast(rhs.count)) + buf = UnsafeMutableBufferPointer( + start: lhs.firstElementAddress + oldCount, + count: rhs.count) } var (remainders,writtenUpTo) = buf.initialize(from: rhs) @@ -716,7 +720,7 @@ internal func _copyCollectionToContiguousArray< C: Collection >(_ source: C) -> ContiguousArray { - let count: Int = numericCast(source.count) + let count = source.count if count == 0 { return ContiguousArray() } @@ -725,7 +729,9 @@ internal func _copyCollectionToContiguousArray< _uninitializedCount: count, minimumCapacity: 0) - let p = UnsafeMutableBufferPointer(start: result.firstElementAddress, count: count) + let p = UnsafeMutableBufferPointer( + start: result.firstElementAddress, + count: count) var (itr, end) = source._copyContents(initializing: p) _debugPrecondition(itr.next() == nil, diff --git a/stdlib/public/core/ExistentialCollection.swift b/stdlib/public/core/ExistentialCollection.swift index e01c87095bd2f..445dd271c69ed 100644 --- a/stdlib/public/core/ExistentialCollection.swift +++ b/stdlib/public/core/ExistentialCollection.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift.org open source project // -// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -743,7 +743,7 @@ internal final class _CollectionBox: _AnyCollectionBox internal override func _index( _ i: _AnyIndexBox, offsetBy n: Int ) -> _AnyIndexBox { - return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n))) + return _IndexBox(_base: _base.index(_unbox(i), offsetBy: n)) } @inlinable @@ -752,10 +752,7 @@ internal final class _CollectionBox: _AnyCollectionBox offsetBy n: Int, limitedBy limit: _AnyIndexBox ) -> _AnyIndexBox? { - return _base.index( - _unbox(i), - offsetBy: numericCast(n), - limitedBy: _unbox(limit)) + return _base.index(_unbox(i), offsetBy: n, limitedBy: _unbox(limit)) .map { _IndexBox(_base: $0) } } @@ -764,7 +761,7 @@ internal final class _CollectionBox: _AnyCollectionBox _ i: inout _AnyIndexBox, offsetBy n: Int ) { if let box = i as? _IndexBox { - return _base.formIndex(&box._base, offsetBy: numericCast(n)) + return _base.formIndex(&box._base, offsetBy: n) } fatalError("Index type mismatch!") } @@ -774,10 +771,7 @@ internal final class _CollectionBox: _AnyCollectionBox _ i: inout _AnyIndexBox, offsetBy n: Int, limitedBy limit: _AnyIndexBox ) -> Bool { if let box = i as? _IndexBox { - return _base.formIndex( - &box._base, - offsetBy: numericCast(n), - limitedBy: _unbox(limit)) + return _base.formIndex(&box._base, offsetBy: n, limitedBy: _unbox(limit)) } fatalError("Index type mismatch!") } @@ -787,12 +781,12 @@ internal final class _CollectionBox: _AnyCollectionBox from start: _AnyIndexBox, to end: _AnyIndexBox ) -> Int { - return numericCast(_base.distance(from: _unbox(start), to: _unbox(end))) + return _base.distance(from: _unbox(start), to: _unbox(end)) } @inlinable internal override var _count: Int { - return numericCast(_base.count) + return _base.count } @usableFromInline @@ -949,7 +943,7 @@ internal final class _BidirectionalCollectionBox internal override func _index( _ i: _AnyIndexBox, offsetBy n: Int ) -> _AnyIndexBox { - return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n))) + return _IndexBox(_base: _base.index(_unbox(i), offsetBy: n)) } @inlinable @@ -958,11 +952,7 @@ internal final class _BidirectionalCollectionBox offsetBy n: Int, limitedBy limit: _AnyIndexBox ) -> _AnyIndexBox? { - return _base.index( - _unbox(i), - offsetBy: numericCast(n), - limitedBy: _unbox(limit) - ) + return _base.index(_unbox(i), offsetBy: n, limitedBy: _unbox(limit)) .map { _IndexBox(_base: $0) } } @@ -971,7 +961,7 @@ internal final class _BidirectionalCollectionBox _ i: inout _AnyIndexBox, offsetBy n: Int ) { if let box = i as? _IndexBox { - return _base.formIndex(&box._base, offsetBy: numericCast(n)) + return _base.formIndex(&box._base, offsetBy: n) } fatalError("Index type mismatch!") } @@ -981,10 +971,7 @@ internal final class _BidirectionalCollectionBox _ i: inout _AnyIndexBox, offsetBy n: Int, limitedBy limit: _AnyIndexBox ) -> Bool { if let box = i as? _IndexBox { - return _base.formIndex( - &box._base, - offsetBy: numericCast(n), - limitedBy: _unbox(limit)) + return _base.formIndex(&box._base, offsetBy: n, limitedBy: _unbox(limit)) } fatalError("Index type mismatch!") } @@ -994,12 +981,12 @@ internal final class _BidirectionalCollectionBox from start: _AnyIndexBox, to end: _AnyIndexBox ) -> Int { - return numericCast(_base.distance(from: _unbox(start), to: _unbox(end))) + return _base.distance(from: _unbox(start), to: _unbox(end)) } @inlinable internal override var _count: Int { - return numericCast(_base.count) + return _base.count } @inlinable @@ -1168,7 +1155,7 @@ internal final class _RandomAccessCollectionBox internal override func _index( _ i: _AnyIndexBox, offsetBy n: Int ) -> _AnyIndexBox { - return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n))) + return _IndexBox(_base: _base.index(_unbox(i), offsetBy: n)) } @inlinable @@ -1177,11 +1164,7 @@ internal final class _RandomAccessCollectionBox offsetBy n: Int, limitedBy limit: _AnyIndexBox ) -> _AnyIndexBox? { - return _base.index( - _unbox(i), - offsetBy: numericCast(n), - limitedBy: _unbox(limit) - ) + return _base.index(_unbox(i), offsetBy: n, limitedBy: _unbox(limit)) .map { _IndexBox(_base: $0) } } @@ -1190,7 +1173,7 @@ internal final class _RandomAccessCollectionBox _ i: inout _AnyIndexBox, offsetBy n: Int ) { if let box = i as? _IndexBox { - return _base.formIndex(&box._base, offsetBy: numericCast(n)) + return _base.formIndex(&box._base, offsetBy: n) } fatalError("Index type mismatch!") } @@ -1200,10 +1183,7 @@ internal final class _RandomAccessCollectionBox _ i: inout _AnyIndexBox, offsetBy n: Int, limitedBy limit: _AnyIndexBox ) -> Bool { if let box = i as? _IndexBox { - return _base.formIndex( - &box._base, - offsetBy: numericCast(n), - limitedBy: _unbox(limit)) + return _base.formIndex(&box._base, offsetBy: n, limitedBy: _unbox(limit)) } fatalError("Index type mismatch!") } @@ -1213,12 +1193,12 @@ internal final class _RandomAccessCollectionBox from start: _AnyIndexBox, to end: _AnyIndexBox ) -> Int { - return numericCast(_base.distance(from: _unbox(start), to: _unbox(end))) + return _base.distance(from: _unbox(start), to: _unbox(end)) } @inlinable internal override var _count: Int { - return numericCast(_base.count) + return _base.count } @inlinable diff --git a/stdlib/public/core/Filter.swift b/stdlib/public/core/Filter.swift index d643eaca126b3..97abb6fb8a536 100644 --- a/stdlib/public/core/Filter.swift +++ b/stdlib/public/core/Filter.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift.org open source project // -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -231,7 +231,7 @@ extension LazyFilterCollection: Collection { // _base at least once, to trigger a _precondition in forward only // collections. _ensureBidirectional(step: step) - for _ in 0 ..< abs(numericCast(n)) { + for _ in 0 ..< abs(n) { _advanceIndex(&i, step: step) } return i @@ -252,7 +252,7 @@ extension LazyFilterCollection: Collection { // invoked on the _base at least once, to trigger a _precondition in // forward only collections. _ensureBidirectional(step: step) - for _ in 0 ..< abs(numericCast(n)) { + for _ in 0 ..< abs(n) { if i == limit { return nil } diff --git a/stdlib/public/core/RangeReplaceableCollection.swift b/stdlib/public/core/RangeReplaceableCollection.swift index 0765d74f8e20a..6b2fda0a7767a 100644 --- a/stdlib/public/core/RangeReplaceableCollection.swift +++ b/stdlib/public/core/RangeReplaceableCollection.swift @@ -449,8 +449,7 @@ extension RangeReplaceableCollection { public mutating func append(contentsOf newElements: __owned S) where S.Element == Element { - let approximateCapacity = self.count + - numericCast(newElements.underestimatedCount) + let approximateCapacity = self.count + newElements.underestimatedCount self.reserveCapacity(approximateCapacity) for element in newElements { append(element) @@ -800,7 +799,7 @@ extension RangeReplaceableCollection @inlinable public mutating func _customRemoveLast(_ n: Int) -> Bool { - self = self[startIndex..(lhs: Other, rhs: Self) -> Self where Element == Other.Element { var result = Self() - result.reserveCapacity(rhs.count + numericCast(lhs.underestimatedCount)) + result.reserveCapacity(rhs.count + lhs.underestimatedCount) result.append(contentsOf: lhs) result.append(contentsOf: rhs) return result diff --git a/stdlib/public/core/Slice.swift b/stdlib/public/core/Slice.swift index 751e88cd10bbe..3e2df8edbee09 100644 --- a/stdlib/public/core/Slice.swift +++ b/stdlib/public/core/Slice.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift.org open source project // -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -337,7 +337,7 @@ extension Slice: RangeReplaceableCollection let newSliceCount = _base.distance(from: _startIndex, to: subRange.lowerBound) + _base.distance(from: subRange.upperBound, to: _endIndex) - + (numericCast(newElements.count) as Int) + + newElements.count _base.replaceSubrange(subRange, with: newElements) _startIndex = _base.index(_base.startIndex, offsetBy: sliceOffset) _endIndex = _base.index(_startIndex, offsetBy: newSliceCount) @@ -400,7 +400,7 @@ extension Slice let newSliceCount = _base.distance(from: _startIndex, to: subRange.lowerBound) + _base.distance(from: subRange.upperBound, to: _endIndex) - + (numericCast(newElements.count) as Int) + + newElements.count _base.replaceSubrange(subRange, with: newElements) _startIndex = _base.startIndex _endIndex = _base.index(_startIndex, offsetBy: newSliceCount) @@ -409,7 +409,7 @@ extension Slice let lastValidIndex = _base.index(before: subRange.lowerBound) let newEndIndexOffset = _base.distance(from: subRange.upperBound, to: _endIndex) - + (numericCast(newElements.count) as Int) + 1 + + newElements.count + 1 _base.replaceSubrange(subRange, with: newElements) if shouldUpdateStartIndex { _startIndex = _base.index(after: lastValidIndex) @@ -443,7 +443,7 @@ extension Slice where S: Collection, S.Element == Base.Element { // FIXME: swift-3-indexing-model: range check. if i == _base.startIndex { - let newSliceCount = count + numericCast(newElements.count) + let newSliceCount = count + newElements.count _base.insert(contentsOf: newElements, at: i) _startIndex = _base.startIndex _endIndex = _base.index(_startIndex, offsetBy: newSliceCount) @@ -452,7 +452,7 @@ extension Slice let lastValidIndex = _base.index(before: i) let newEndIndexOffset = _base.distance(from: i, to: _endIndex) - + numericCast(newElements.count) + 1 + + newElements.count + 1 _base.insert(contentsOf: newElements, at: i) if shouldUpdateStartIndex { _startIndex = _base.index(after: lastValidIndex) diff --git a/stdlib/public/core/SliceBuffer.swift b/stdlib/public/core/SliceBuffer.swift index e134319f28dc2..d936250d0d359 100644 --- a/stdlib/public/core/SliceBuffer.swift +++ b/stdlib/public/core/SliceBuffer.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift.org open source project // -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -116,8 +116,7 @@ internal struct _SliceBuffer /// the given collection. /// /// - Precondition: This buffer is backed by a uniquely-referenced - /// `_ContiguousArrayBuffer` and - /// `insertCount <= numericCast(newValues.count)`. + /// `_ContiguousArrayBuffer` and `insertCount <= newValues.count`. @inlinable internal mutating func replaceSubrange( _ subrange: Range, @@ -126,7 +125,7 @@ internal struct _SliceBuffer ) where C: Collection, C.Element == Element { _invariantCheck() - _internalInvariant(insertCount <= numericCast(newValues.count)) + _internalInvariant(insertCount <= newValues.count) _internalInvariant(_hasNativeBuffer) _internalInvariant(isUniquelyReferenced()) diff --git a/test/AutoDiff/compiler_crashers/sr12744-unhandled-pullback-indirect-result.swift b/test/AutoDiff/compiler_crashers/sr12744-unhandled-pullback-indirect-result.swift new file mode 100644 index 0000000000000..24f38b4c6d3f0 --- /dev/null +++ b/test/AutoDiff/compiler_crashers/sr12744-unhandled-pullback-indirect-result.swift @@ -0,0 +1,19 @@ +// RUN: not --crash %target-swift-frontend -emit-sil -verify %s +// REQUIRES: asserts + +// SR-12744: Pullback generation crash for unhandled indirect result. +// May be due to inconsistent derivative function type calculation logic in +// `VJPEmitter::createEmptyPullback`. + +import _Differentiation + +class Class: Differentiable { + @differentiable(wrt: (self, x)) + @differentiable(wrt: x) + func f(_ x: Float) -> Float { x } +} + +func test(_ c: C, _ x: Float) { + _ = gradient(at: c, x) { c, x in c.f(x) } + _ = gradient(at: x) { x in c.f(x) } +}