Files
swift-mirror/stdlib/public/core/Tuple.swift.gyb
Raphael Isemann 334ec53c17 Fix Python 3.x error in Tuple.swift.gyb due concatenating "range + list"
This was failing with the following error on my Python3 system:
FAILED: stdlib/public/core/8/Tuple.swift
cd /home/teemperor/work/swift/swift/stdlib/public/core && /usr/bin/cmake -E make_directory /home/teemperor/work/swift/build/Ninja-ReleaseAssert+stdlib-Release/swift-linux-x86_64/stdlib/public/core/8 && /usr/bin/python /home/teemperor/work/swift/swift/utils/gyb -DunicodeGraphemeBreakPropertyFile=/home/teemperor/work/swift/swift/utils/UnicodeData/GraphemeBreakProperty.txt -DunicodeGraphemeBreakTestFile=/home/teemperor/work/swift/swift/utils/UnicodeData/GraphemeBreakTest.txt -DCMAKE_SIZEOF_VOID_P=8 -o /home/teemperor/work/swift/build/Ninja-ReleaseAssert+stdlib-Release/swift-linux-x86_64/stdlib/public/core/8/Tuple.swift.tmp Tuple.swift.gyb && /usr/bin/cmake -E copy_if_different /home/teemperor/work/swift/build/Ninja-ReleaseAssert+stdlib-Release/swift-linux-x86_64/stdlib/public/core/8/Tuple.swift.tmp /home/teemperor/work/swift/build/Ninja-ReleaseAssert+stdlib-Release/swift-linux-x86_64/stdlib/public/core/8/Tuple.swift && /usr/bin/cmake -E remove /home/teemperor/work/swift/build/Ninja-ReleaseAssert+stdlib-Release/swift-linux-x86_64/stdlib/public/core/8/Tuple.swift.tmp
Traceback (most recent call last):
  File "/home/teemperor/work/swift/swift/utils/gyb", line 3, in <module>
    gyb.main()
  File "/home/teemperor/work/swift/swift/utils/gyb.py", line 1263, in main
    args.target.write(execute_template(ast, args.line_directive, **bindings))
  File "/home/teemperor/work/swift/swift/utils/gyb.py", line 1131, in execute_template
    ast.execute(execution_context)
  File "/home/teemperor/work/swift/swift/utils/gyb.py", line 635, in execute
    x.execute(context)
  File "/home/teemperor/work/swift/swift/utils/gyb.py", line 721, in execute
    result = eval(self.code, context.local_bindings)
  File "/home/teemperor/work/swift/swift/stdlib/public/core/Tuple.swift.gyb", line 109, in <module>
    %   typeParams = [chr(ord("A") + i) for i in range(arity)]
  File "/home/teemperor/work/swift/swift/utils/gyb.py", line 635, in execute
    x.execute(context)
  File "/home/teemperor/work/swift/swift/utils/gyb.py", line 721, in execute
    result = eval(self.code, context.local_bindings)
  File "/home/teemperor/work/swift/swift/stdlib/public/core/Tuple.swift.gyb", line 114, in <module>
    %   greaterTuple = "(\"a\", {})".format(", ".join(map(str, range(1, arity - 1) + [arity])))
TypeError: unsupported operand type(s) for +: 'range' and 'list'

This commit just converts the range to a list so we can concatenate this on Python 3.x (where
the implicit conversion from range to list is no longer possible).
2020-01-16 12:35:22 +01:00

202 lines
6.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// Generate comparison functions for tuples up to some reasonable arity.
%{
comparableOperators = [
('<', 'before'),
('<=', 'before or the same as'),
('>', 'after'),
('>=', 'after or the same as')
]
}%
/// Returns a Boolean value indicating whether the corresponding components of
/// two tuples are equal.
///
/// All arity zero tuples are equal.
///
/// - Parameters:
/// - lhs: An empty tuple.
/// - rhs: An empty tuple.
@inlinable // trivial-implementation
public func ==(lhs: (), rhs: ()) -> Bool {
return true
}
/// Returns a Boolean value indicating whether any corresponding components of
/// the two tuples are not equal.
///
/// All arity zero tuples are equal.
///
/// - Parameters:
/// - lhs: An empty tuple.
/// - rhs: An empty tuple.
@inlinable // trivial-implementation
public func !=(lhs: (), rhs: ()) -> Bool {
return false
}
/// Returns a Boolean value indicating whether the first tuple is ordered
/// before the second in a lexicographical ordering.
///
/// An arity zero tuple is never strictly before another arity zero tuple in a
/// lexicographical ordering.
///
/// - Parameters:
/// - lhs: An empty tuple.
/// - rhs: An empty tuple.
@inlinable // trivial-implementation
public func <(lhs: (), rhs: ()) -> Bool {
return false
}
/// Returns a Boolean value indicating whether the first tuple is ordered
/// before or the same as the second in a lexicographical ordering.
///
/// An arity zero tuple is always before or the same as another arity zero tuple
/// in a lexicographical ordering.
///
/// - Parameters:
/// - lhs: An empty tuple.
/// - rhs: An empty tuple.
@inlinable // trivial-implementation
public func <=(lhs: (), rhs: ()) -> Bool {
return true
}
/// Returns a Boolean value indicating whether the first tuple is ordered
/// after the second in a lexicographical ordering.
///
/// An arity zero tuple is never strictly after another arity zero tuple in a
/// lexicographical ordering.
///
/// - Parameters:
/// - lhs: An empty tuple.
/// - rhs: An empty tuple.
@inlinable // trivial-implementation
public func >(lhs: (), rhs: ()) -> Bool {
return false
}
/// Returns a Boolean value indicating whether the first tuple is ordered
/// after or the same as the second in a lexicographical ordering.
///
/// An arity zero tuple is always after or the same as another arity zero tuple
/// in a lexicographical ordering.
///
/// - Parameters:
/// - lhs: An empty tuple.
/// - rhs: An empty tuple.
@inlinable // trivial-implementation
public func >=(lhs: (), rhs: ()) -> Bool {
return true
}
% for arity in range(2,7):
% typeParams = [chr(ord("A") + i) for i in range(arity)]
% tupleT = "({})".format(",".join(typeParams))
% equatableTypeParams = ", ".join(["{}: Equatable".format(c) for c in typeParams])
% originalTuple = "(\"a\", {})".format(", ".join(map(str, range(1, arity))))
% greaterTuple = "(\"a\", {})".format(", ".join(map(str, list(range(1, arity - 1)) + [arity])))
/// Returns a Boolean value indicating whether the corresponding components of
/// two tuples are equal.
///
/// For two tuples to compare as equal, each corresponding pair of components
/// must be equal. The following example compares tuples made up of ${arity}
/// components:
///
/// let a = ${originalTuple}
/// let b = ${originalTuple}
/// print(a == b)
/// // Prints "true"
///
/// let c = ${greaterTuple}
/// print(a == c)
/// // Prints "false"
///
/// - Parameters:
/// - lhs: A tuple of `Equatable` elements.
/// - rhs: Another tuple of elements of the same type as `lhs`.
@inlinable // trivial-implementation
public func == <${equatableTypeParams}>(lhs: ${tupleT}, rhs: ${tupleT}) -> Bool {
guard lhs.0 == rhs.0 else { return false }
/*tail*/ return (
${", ".join("lhs.{}".format(i) for i in range(1, arity))}
) == (
${", ".join("rhs.{}".format(i) for i in range(1, arity))}
)
}
/// Returns a Boolean value indicating whether any corresponding components of
/// the two tuples are not equal.
///
/// For two tuples to compare as equal, each corresponding pair of components
/// must be equal. The following example compares tuples made up of ${arity}
/// components:
///
/// let a = ${originalTuple}
/// let b = ${originalTuple}
/// print(a != b)
/// // Prints "false"
///
/// let c = ${greaterTuple}
/// print(a != c)
/// // Prints "true"
///
/// - Parameters:
/// - lhs: A tuple of `Equatable` elements.
/// - rhs: Another tuple of elements of the same type as `lhs`.
@inlinable // trivial-implementation
public func != <${equatableTypeParams}>(lhs: ${tupleT}, rhs: ${tupleT}) -> Bool {
guard lhs.0 == rhs.0 else { return true }
/*tail*/ return (
${", ".join("lhs.{}".format(i) for i in range(1, arity))}
) != (
${", ".join("rhs.{}".format(i) for i in range(1, arity))}
)
}
% comparableTypeParams = ", ".join(["{}: Comparable".format(c) for c in typeParams])
% for op, phrase in comparableOperators:
/// Returns a Boolean value indicating whether the first tuple is ordered
/// ${phrase} the second in a lexicographical ordering.
///
/// Given two tuples `(a1, a2, ..., aN)` and `(b1, b2, ..., bN)`, the first
/// tuple is ${phrase} the second tuple if and only if
/// `a1 ${op.replace('=', '')} b1` or (`a1 == b1` and
/// `(a2, ..., aN) ${op} (b2, ..., bN)`).
///
/// - Parameters:
/// - lhs: A tuple of `Comparable` elements.
/// - rhs: Another tuple of elements of the same type as `lhs`.
@inlinable // trivial-implementation
public func ${op} <${comparableTypeParams}>(lhs: ${tupleT}, rhs: ${tupleT}) -> Bool {
if lhs.0 != rhs.0 { return lhs.0 ${op} rhs.0 }
/*tail*/ return (
${", ".join("lhs.{}".format(i) for i in range(1, arity))}
) ${op} (
${", ".join("rhs.{}".format(i) for i in range(1, arity))}
)
}
% end
% end
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End: