Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

[Tensor] Add precondition in 'Tensor.init(arrayLiteral:)' to reject 'init()' calls. #336

Merged
merged 2 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Sources/TensorFlow/Core/ShapedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -587,15 +587,15 @@ extension ShapedArray: RandomAccessCollection, MutableCollection {
public subscript(index: Int) -> Element {
get {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(index < endIndex, "ShapedArray index is out of range")
precondition(index >= startIndex, "Negative ShapedArray index is out of range")
precondition(index < endIndex, "ShapedArray index is out of range.")
precondition(index >= startIndex, "Negative ShapedArray index is out of range.")
return ShapedArraySlice(base: self, baseIndices: [index])
}
set {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(index < endIndex, "ShapedArray index is out of range")
precondition(index >= startIndex, "Negative ShapedArray index is out of range")
precondition(shape.dropFirst().elementsEqual(newValue.shape), "Element shape mismatch")
precondition(index < endIndex, "ShapedArray index is out of range.")
precondition(index >= startIndex, "Negative ShapedArray index is out of range.")
precondition(shape.dropFirst().elementsEqual(newValue.shape), "Element shape mismatch.")
let scalarIndex = self.scalarIndex(fromIndex: index)
withUnsafeMutableBufferPointer { destBuffPtr in
let ptr = destBuffPtr.baseAddress!.advanced(by: scalarIndex)
Expand Down Expand Up @@ -703,6 +703,7 @@ extension ShapedArray: ExpressibleByArrayLiteral where Scalar: TensorFlowScalar
public typealias ArrayLiteralElement = _TensorElementLiteral<Scalar>
@inlinable
public init(arrayLiteral elements: _TensorElementLiteral<Scalar>...) {
precondition(!elements.isEmpty, "Cannot create a 'ShapedArray' with no elements.")
self = Tensor<Scalar>(_tensorElementLiterals: elements).array
}
}
Expand Down Expand Up @@ -836,7 +837,7 @@ public struct ShapedArraySlice<Scalar>: _ShapedArrayProtocol {
baseIndices indices: __owned [Int] = [],
bounds: Range<Int>? = nil
) {
precondition(indices.count <= base.rank, "Number of base indices exceeds base rank")
precondition(indices.count <= base.rank, "Number of base indices exceeds base rank.")
precondition(
zip(base.shape, indices).allSatisfy { $1 >= 0 && $1 < $0 },
"Base indices are out of range")
Expand Down Expand Up @@ -1065,6 +1066,7 @@ extension ShapedArraySlice: ExpressibleByArrayLiteral where Scalar: TensorFlowSc
public typealias ArrayLiteralElement = _TensorElementLiteral<Scalar>
@inlinable
public init(arrayLiteral elements: _TensorElementLiteral<Scalar>...) {
precondition(!elements.isEmpty, "Cannot create a 'ShapedArraySlice' with no elements.")
self.init(base: Tensor(_tensorElementLiterals: elements).array)
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/TensorFlow/Core/Tensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ extension Tensor: ExpressibleByArrayLiteral {
/// Creates a tensor initialized with the given elements.
@inlinable
public init(arrayLiteral elements: _TensorElementLiteral<Scalar>...) {
precondition(!elements.isEmpty, "Cannot create a 'Tensor' with no elements.")
self.init(_tensorElementLiterals: elements)
}
}
Expand Down
1 change: 0 additions & 1 deletion Tests/TensorFlowTests/TensorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ final class TensorTests: XCTestCase {
}
return b
}

XCTAssertEqual(0, selectValue(true).scalar)
}

Expand Down