Skip to content

Commit d74384c

Browse files
Merge pull request #59931 from AnthonyLatsis/close-issues-3
Add regression tests to close several issues p. 3
2 parents b14c5bb + 0a2293f commit d74384c

File tree

11 files changed

+127
-6
lines changed

11 files changed

+127
-6
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5665,7 +5665,7 @@ class ConstraintSystem {
56655665
return false;
56665666
}
56675667

5668-
bool isTooComplex(SmallVectorImpl<Solution> const &solutions) {
5668+
bool isTooComplex(ArrayRef<Solution> solutions) {
56695669
if (isAlreadyTooComplex.first)
56705670
return true;
56715671

lib/Sema/CSRanking.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,10 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
14051405
Optional<unsigned>
14061406
ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
14071407
bool minimize) {
1408+
// Don't spend time filtering solutions if we already hit a threshold.
1409+
if (isTooComplex(viable))
1410+
return None;
1411+
14081412
if (viable.empty())
14091413
return None;
14101414
if (viable.size() == 1)
@@ -1449,6 +1453,10 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
14491453
bestIdx = i;
14501454
break;
14511455
}
1456+
1457+
// Give up if we're out of time.
1458+
if (isTooComplex(/*solutions=*/{}))
1459+
return None;
14521460
}
14531461

14541462
// Make sure that our current best is better than all of the solved systems.
@@ -1479,6 +1487,10 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
14791487
ambiguous = true;
14801488
break;
14811489
}
1490+
1491+
// Give up if we're out of time.
1492+
if (isTooComplex(/*solutions=*/{}))
1493+
return None;
14821494
}
14831495

14841496
// If the result was not ambiguous, we're done.

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,6 +3843,10 @@ SolutionResult ConstraintSystem::salvage() {
38433843
// Solve the system.
38443844
solveImpl(viable);
38453845

3846+
// If we hit a threshold, we're done.
3847+
if (isTooComplex(viable))
3848+
return SolutionResult::forTooComplex(getTooComplexRange());
3849+
38463850
// Before removing any "fixed" solutions, let's check
38473851
// if ambiguity is caused by fixes and diagnose if possible.
38483852
if (diagnoseAmbiguityWithFixes(viable))
@@ -3888,9 +3892,6 @@ SolutionResult ConstraintSystem::salvage() {
38883892
// Fall through to produce diagnostics.
38893893
}
38903894

3891-
if (isTooComplex(viable))
3892-
return SolutionResult::forTooComplex(getTooComplexRange());
3893-
38943895
// Could not produce a specific diagnostic; punt to the client.
38953896
return SolutionResult::forUndiagnosedError();
38963897
}

test/Constraints/argument_matching.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,3 +1789,16 @@ func rdar93922410(_ completion: (Int?) -> Void) { // expected-note {{'completion
17891789
return completion() // expected-error {{missing argument for parameter #1 in call}}
17901790
}
17911791
}
1792+
1793+
// https://github.com/apple/swift/issues/43509
1794+
do {
1795+
func myAssertionFailure(
1796+
_ message: @autoclosure () -> String = String(),
1797+
file: StaticString = #file, line: UInt = #line
1798+
) {}
1799+
1800+
let x: Int?
1801+
myAssertionFailure(x != nil, "error")
1802+
// expected-error@-1 {{cannot convert value of type 'Bool' to expected argument type 'String'}}
1803+
// expected-error@-2 {{missing argument label 'file:' in call}}
1804+
}

test/Constraints/array_literal.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,13 @@ func defaultToAny(i: Int, s: String) {
145145
let _: [Any] = [1, "a", 3.5]
146146
let _: [Any] = [1, "a", [3.5, 3.7, 3.9]]
147147
let _: [Any] = [1, "a", [3.5, "b", 3]]
148+
let _: [Any] = [1, [2, [3]]]
148149

149150
let _: [Any?] = [1, "a", nil, 3.5]
150151
let _: [Any?] = [1, "a", nil, [3.5, 3.7, 3.9]]
151152
let _: [Any?] = [1, "a", nil, [3.5, "b", nil]]
153+
let _: [Any?] = [1, [2, [3]]]
154+
let _: [Any?] = [1, nil, [2, nil, [3]]]
152155

153156
let a6 = [B(), C()]
154157
let _: Int = a6 // expected-error{{value of type '[A]'}}

test/Constraints/generics.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,3 +942,26 @@ do {
942942
}
943943
}
944944
}
945+
946+
// https://github.com/apple/swift/issues/43527
947+
do {
948+
struct Box<Contents, U> {}
949+
950+
class Sweets {}
951+
class Chocolate {}
952+
953+
struct Gift<Contents> {
954+
// expected-note@-1 2 {{arguments to generic parameter 'Contents' ('Chocolate' and 'Sweets') are expected to be equal}}
955+
956+
init(_: Box<[Contents], Never>) {}
957+
}
958+
959+
let box = Box<[Chocolate], Never>()
960+
961+
var g1: Gift<Sweets>
962+
g1 = Gift<Chocolate>(box)
963+
// expected-error@-1 {{cannot assign value of type 'Gift<Chocolate>' to type 'Gift<Sweets>'}}
964+
965+
let g2: Gift<Sweets> = Gift<Chocolate>(box)
966+
// expected-error@-1 {{cannot assign value of type 'Gift<Chocolate>' to type 'Gift<Sweets>'}}
967+
}

test/stdlib/RuntimeObjC.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,18 @@ Runtime.test("isBridgedVerbatimToObjectiveC") {
304304

305305
//===----------------------------------------------------------------------===//
306306

307+
protocol SomeNativeProto {}
308+
307309
class SomeClass {}
308310
@objc class SomeObjCClass {}
309-
class SomeNSObjectSubclass : NSObject {}
311+
312+
class SomeNSObjectSubclass : NSObject, SomeNativeProto {}
313+
extension SomeNativeProto {
314+
// https://github.com/apple/swift/issues/43154
315+
func expectSelfTypeNameEqual(to typeName: String) {
316+
expectEqual(typeName, _typeName(type(of: self)))
317+
}
318+
}
310319

311320
Runtime.test("typeName") {
312321
expectEqual("a.SomeObjCClass", _typeName(SomeObjCClass.self))
@@ -321,6 +330,8 @@ Runtime.test("typeName") {
321330

322331
a = NSObject()
323332
expectEqual("NSObject", _typeName(type(of: a)))
333+
334+
SomeNSObjectSubclass().expectSelfTypeNameEqual(to: "a.SomeNSObjectSubclass")
324335
}
325336

326337
class GenericClass<T> {}
@@ -571,7 +582,7 @@ Reflection.test("Class/ObjectiveCBase/Default") {
571582
expectEqual(expected, output)
572583
}
573584
}
574-
protocol SomeNativeProto {}
585+
575586
@objc protocol SomeObjCProto {}
576587
extension SomeClass: SomeObjCProto {}
577588

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %scale-test --begin 1 --end 15 --step 1 --select NumLeafScopes %s -Xfrontend=-solver-expression-time-threshold=1 -Xfrontend=-verify
2+
// REQUIRES: asserts, no_asan
3+
4+
// https://github.com/apple/swift/issues/43386
5+
6+
// expected-error@+1 {{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}}
7+
let _ = [
8+
%for i in range(0, N):
9+
0...1,
10+
0..<1,
11+
%end
12+
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=5
2+
// REQUIRES: tools-release,no_asan
3+
4+
// Hits the default memory threshold
5+
// https://github.com/apple/swift/issues/43369
6+
7+
struct A {}
8+
struct B {}
9+
10+
func - (lhs: A, rhs: A) -> B {}
11+
func / (lhs: B, rhs: Double) -> B {}
12+
func - (lhs: B, rhs: B) -> B {}
13+
14+
do {
15+
let a0 = A()
16+
let a1 = A()
17+
18+
let t0 = 0
19+
let t1 = 0
20+
21+
// expected-error@+1 {{the compiler is unable to type-check this expression in reasonable time}}
22+
let _ = (a0 - a0) / (t0 - t0) - (a1 - a1) / (t1 - t1)
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %scale-test --invert-result --begin 1 --end 8 --step 1 --select NumLeafScopes %s -Xfrontend=-typecheck
2+
// REQUIRES: asserts, no_asan
3+
4+
let x: Int?
5+
6+
let _ = [
7+
%for i in range(0, N):
8+
"k" : x ?? 0,
9+
%end
10+
]

0 commit comments

Comments
 (0)