Skip to content

Commit 2c15dc2

Browse files
authored
Merge pull request #20625 from rudkx/designated-types-array
[ConstraintSystem] Add Array as a designated type for `+` and `+=`.
2 parents d042375 + d3b660d commit 2c15dc2

File tree

8 files changed

+40
-13
lines changed

8 files changed

+40
-13
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,13 +1948,19 @@ Constraint *ConstraintSystem::selectDisjunction() {
19481948
if (disjunctions.empty())
19491949
return nullptr;
19501950

1951-
if (auto *disjunction = selectBestBindingDisjunction(*this, disjunctions))
1952-
return disjunction;
1953-
1951+
// Attempt apply disjunctions first. When we have operators with
1952+
// designated types, this is important, because it allows us to
1953+
// select all the preferred operator overloads prior to other
1954+
// disjunctions that we may not be able to short-circuit, allowing
1955+
// us to eliminate behavior that is exponential in the number of
1956+
// operators in the expression.
19541957
if (getASTContext().isSwiftVersionAtLeast(5))
19551958
if (auto *disjunction = selectApplyDisjunction())
19561959
return disjunction;
19571960

1961+
if (auto *disjunction = selectBestBindingDisjunction(*this, disjunctions))
1962+
return disjunction;
1963+
19581964
// Pick the disjunction with the smallest number of active choices.
19591965
auto minDisjunction =
19601966
std::min_element(disjunctions.begin(), disjunctions.end(),

stdlib/public/core/Array.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,25 @@ extension Array: RangeReplaceableCollection {
12901290
}
12911291
}
12921292

1293+
// Implementations of + and += for same-type arrays. This combined
1294+
// with the operator declarations for these operators designating this
1295+
// type as a place to prefer this operator help the expression type
1296+
// checker speed up cases where there is a large number of uses of the
1297+
// operator in the same expression.
1298+
extension Array {
1299+
@inlinable
1300+
public static func + (lhs: Array, rhs: Array) -> Array {
1301+
var lhs = lhs
1302+
lhs.append(contentsOf: rhs)
1303+
return lhs
1304+
}
1305+
1306+
@inlinable
1307+
public static func += (lhs: inout Array, rhs: Array) {
1308+
lhs.append(contentsOf: rhs)
1309+
}
1310+
}
1311+
12931312
extension Array: CustomReflectable {
12941313
/// A mirror that reflects the array.
12951314
public var customMirror: Mirror {

stdlib/public/core/Policy.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ infix operator & : MultiplicationPrecedence, BinaryInteger
420420

421421
// "Additive"
422422

423-
infix operator + : AdditionPrecedence, AdditiveArithmetic, String, Strideable
423+
infix operator + : AdditionPrecedence, AdditiveArithmetic, String, Array, Strideable
424424
infix operator &+ : AdditionPrecedence, FixedWidthInteger
425425
infix operator - : AdditionPrecedence, AdditiveArithmetic, Strideable
426426
infix operator &- : AdditionPrecedence, FixedWidthInteger
@@ -474,7 +474,7 @@ infix operator *= : AssignmentPrecedence, Numeric
474474
infix operator &*= : AssignmentPrecedence, FixedWidthInteger
475475
infix operator /= : AssignmentPrecedence, BinaryInteger
476476
infix operator %= : AssignmentPrecedence, BinaryInteger
477-
infix operator += : AssignmentPrecedence, AdditiveArithmetic, String, Strideable
477+
infix operator += : AssignmentPrecedence, AdditiveArithmetic, String, Array, Strideable
478478
infix operator &+= : AssignmentPrecedence, FixedWidthInteger
479479
infix operator -= : AssignmentPrecedence, AdditiveArithmetic, Strideable
480480
infix operator &-= : AssignmentPrecedence, FixedWidthInteger

test/Constraints/bridging.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func rdar19831698() {
269269
var v72 = true + true // expected-error{{binary operator '+' cannot be applied to two 'Bool' operands}}
270270
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists:}}
271271
var v73 = true + [] // expected-error{{binary operator '+' cannot be applied to operands of type 'Bool' and '[Any]'}}
272-
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Self, Other), (Other, Self)}}
272+
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Array<Element>, Array<Element>), (Self, Other), (Other, Self)}}
273273
var v75 = true + "str" // expected-error {{binary operator '+' cannot be applied to operands of type 'Bool' and 'String'}} expected-note {{expected an argument list of type '(String, String)'}}
274274
}
275275

test/Misc/misc_diagnostics.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,14 @@ func test17875634() {
103103
var col = 2
104104
var coord = (row, col)
105105

106-
match += (1, 2) // expected-error{{argument type '(Int, Int)' does not conform to expected type 'Sequence'}}
106+
match += (1, 2) // expected-error{{binary operator '+=' cannot be applied to operands of type '[(Int, Int)]' and '(Int, Int)'}}
107+
// expected-note@-1 {{overloads for '+=' exist with these partially matching parameter lists: (inout Array<Element>, Array<Element>), (inout Self, Other)}}
107108

108-
match += (row, col) // expected-error{{argument type '(@lvalue Int, @lvalue Int)' does not conform to expected type 'Sequence'}}
109+
match += (row, col) // expected-error{{binary operator '+=' cannot be applied to operands of type '[(Int, Int)]' and '(Int, Int)}}
110+
// expected-note@-1 {{overloads for '+=' exist with these partially matching parameter lists: (inout Array<Element>, Array<Element>), (inout Self, Other)}}
109111

110-
match += coord // expected-error{{argument type '@lvalue (Int, Int)' does not conform to expected type 'Sequence'}}
112+
match += coord // expected-error{{binary operator '+=' cannot be applied to operands of type '[(Int, Int)]' and '(Int, Int)'}}
113+
// expected-note@-1 {{overloads for '+=' exist with these partially matching parameter lists: (inout Array<Element>, Array<Element>), (inout Self, Other)}}
111114

112115
match.append(row, col) // expected-error {{instance method 'append' expects a single parameter of type '(Int, Int)'}} {{16-16=(}} {{24-24=)}}
113116

validation-test/Sema/type_checker_perf/fast/rdar18360240.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %scale-test --begin 2 --end 10 --step 2 --select NumConstraintScopes --polynomial-threshold 1.5 %s
1+
// RUN: %scale-test --begin 2 --end 10 --step 2 --select NumConstraintScopes --polynomial-threshold 1.5 %s -Xfrontend=-swift-version -Xfrontend=5 -Xfrontend=-solver-disable-shrink -Xfrontend=-disable-constraint-solver-performance-hacks -Xfrontend=-solver-enable-operator-designated-types
22
// REQUIRES: OS=macosx
33
// REQUIRES: asserts
44

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=1
1+
// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=1 -swift-version 5 -solver-disable-shrink -disable-constraint-solver-performance-hacks -solver-enable-operator-designated-types
22
// REQUIRES: tools-release,no_asserts
33

44
let _ = [0].reduce([Int]()) {
5-
// expected-error@-1 {{reasonable time}}
65
return $0.count == 0 && ($1 == 0 || $1 == 2 || $1 == 3) ? [] : $0 + [$1]
76
}

validation-test/Sema/type_checker_perf/fast/rdar25866240.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %scale-test --begin 2 --end 10 --step 1 --select NumConstraintScopes %s
1+
// RUN: %scale-test --begin 2 --end 10 --step 1 --select NumConstraintScopes %s -Xfrontend=-swift-version -Xfrontend=5 -Xfrontend=-solver-disable-shrink -Xfrontend=-disable-constraint-solver-performance-hacks -Xfrontend=-solver-enable-operator-designated-types
22
// REQUIRES: OS=macosx
33
// REQUIRES: asserts
44

0 commit comments

Comments
 (0)