Skip to content

Commit 719547f

Browse files
authored
Merge pull request #23125 from DougGregor/constraint-solver-remove-sr-2505-hack
[Constraint solver] Remove a dubious hack introduced with SR-2505
2 parents 2e2c12f + d60b00d commit 719547f

File tree

6 files changed

+22
-47
lines changed

6 files changed

+22
-47
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ Swift 5.1
2626

2727
* Key path expressions can now include references to tuple elements.
2828

29+
* Single-parameter functions accepting values of type `Any` are no
30+
longer preferred over other functions.
31+
32+
```swift
33+
func foo(_: Any) { print("Any") }
34+
func foo<T>(_: T) { print("T") }
35+
foo(0) // prints "Any" in Swift < 5.1, "T" in Swift 5.1
36+
```
37+
2938
Swift 5.0
3039
---------
3140

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -917,36 +917,6 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
917917
argsWithLabels.append(args.begin(), args.end());
918918
AnyFunctionType::relabelParams(argsWithLabels, argLabels);
919919

920-
// FIXME: Remove this. It's functionally identical to the real code
921-
// path below, except for some behavioral differences in solution ranking
922-
// that I don't understand.
923-
if (params.size() == 1 &&
924-
args.size() == 1 &&
925-
params[0].getLabel().empty() &&
926-
args[0].getLabel().empty() &&
927-
!params[0].getParameterFlags().isInOut() &&
928-
!args[0].getParameterFlags().isInOut() &&
929-
params[0].getPlainType()->isAny()) {
930-
auto argType = args[0].getPlainType();
931-
932-
// Disallow assignment of noescape function to parameter of type
933-
// Any. Allowing this would allow these functions to escape.
934-
if (auto *fnTy = argType->getAs<AnyFunctionType>()) {
935-
if (fnTy->isNoEscape()) {
936-
auto *loc = cs.getConstraintLocator(locator);
937-
// Allow assigned of 'no-escape' function with recorded fix.
938-
if (cs.shouldAttemptFixes()) {
939-
if (!cs.recordFix(MarkExplicitlyEscaping::create(cs, loc)))
940-
return cs.getTypeMatchSuccess();
941-
}
942-
943-
return cs.getTypeMatchFailure(locator);
944-
}
945-
}
946-
947-
return cs.getTypeMatchSuccess();
948-
}
949-
950920
// Match up the call arguments to the parameters.
951921
SmallVector<ParamBinding, 4> parameterBindings;
952922
ArgumentFailureTracker listener(cs, parameterBindings, locator);

test/ClangImporter/objc_parse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func keyedSubscripting(_ b: B, idx: A, a: A) {
178178
dict[NSString()] = a
179179
let value = dict[NSString()]
180180

181-
dict[nil] = a // expected-error {{cannot assign value of type 'A' to type 'Any?'}}
181+
dict[nil] = a // expected-error {{ambiguous subscript with base type 'NSMutableDictionary' and index type '_'}}
182182
let q = dict[nil] // expected-error {{ambiguous subscript}}
183183
_ = q
184184
}

test/Constraints/closures.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,11 @@ func r20789423() {
416416

417417
}
418418

419-
// Make sure that behavior related to allowing trailing closures to match functions
420-
// with Any as a final parameter is the same after the changes made by SR-2505, namely:
421-
// that we continue to select function that does _not_ have Any as a final parameter in
422-
// presence of other possibilities.
423-
419+
// In the example below, SR-2505 started preferring C_SR_2505.test(_:) over
420+
// test(it:). Prior to Swift 5.1, we emulated the old behavior. However,
421+
// that behavior is inconsistent with the typical approach of preferring
422+
// overloads from the concrete type over one from a protocol, so we removed
423+
// the hack.
424424
protocol SR_2505_Initable { init() }
425425
struct SR_2505_II : SR_2505_Initable {}
426426

@@ -442,10 +442,9 @@ class C_SR_2505 : P_SR_2505 {
442442
}
443443

444444
func call(_ c: C_SR_2505) -> Bool {
445-
// Note: no diagnostic about capturing 'self', because this is a
446-
// non-escaping closure -- that's how we know we have selected
447-
// test(it:) and not test(_)
448-
return c.test { o in test(o) }
445+
// Note: the diagnostic about capturing 'self', indicates that we have
446+
// selected test(_) rather than test(it:)
447+
return c.test { o in test(o) } // expected-error{{call to method 'test' in closure requires explicit 'self.' to make capture semantics explicit}}
449448
}
450449
}
451450

validation-test/Sema/OverridesAndOverloads.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ Overrides.test("covariant argument override, struct to protocol") {
103103
}
104104

105105
// FIXME: https://bugs.swift.org/browse/SR-731
106-
expectFailure {
107-
Derived().foo(P1ImplS1())
108-
expectEqual("Derived.foo(P1)", which)
109-
}
106+
Derived().foo(P1ImplS1())
107+
expectEqual("Base.foo(P1ImplS1)", which)
110108

111109
Derived().foo(P1xImplS1())
112110
expectEqual("Derived.foo(P1)", which)
@@ -321,14 +319,13 @@ Overloads.test("generic methods are worse than non-generic") {
321319
func foo(_: C1) { which = "foo(C1)" }
322320
func foo(_: Any) { which = "foo(Any)" }
323321
func foo<T>(_: T) { which = "foo(T)" }
324-
// It is not possible to call foo<T>(T). foo(Any) always wins.
325322

326323
func bar(_: C1) { which = "bar(C1)" }
327324
func bar<T>(_: T) { which = "bar(T)" }
328325
}
329326

330327
Base().foo(C1()); expectEqual("foo(C1)", which)
331-
Base().foo(Token1()); expectEqual("foo(Any)", which)
328+
Base().foo(Token1()); expectEqual("foo(T)", which)
332329

333330
Base().bar(C1()); expectEqual("bar(C1)", which)
334331
Base().bar(Token1()); expectEqual("bar(T)", which)

validation-test/Sema/type_checker_perf/fast/rdar29358447.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 0 --end 25000 --step 1000 --typecheck --select incrementConstraintsPerContractionCounter %s -Xfrontend=-solver-disable-shrink -Xfrontend=-disable-constraint-solver-performance-hacks -Xfrontend=-solver-enable-operator-designated-types
1+
// RUN: %scale-test --begin 0 --end 10000 --step 1000 --typecheck --select incrementConstraintsPerContractionCounter %s -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)