Skip to content

Commit c0b9394

Browse files
authored
Merge pull request #30113 from xedin/rdar-54580247
[CSBindings] Open collection before binding parameter only if origina…
2 parents 136e6c4 + 20fc51d commit c0b9394

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,25 @@ bool TypeVarBindingProducer::computeNext() {
10431043
if (auto simplifiedSuper = CS.checkTypeOfBinding(TypeVar, supertype))
10441044
addNewBinding(binding.withType(*simplifiedSuper));
10451045
}
1046+
1047+
auto srcLocator = binding.getLocator();
1048+
if (srcLocator &&
1049+
srcLocator->isLastElement<LocatorPathElt::ApplyArgToParam>() &&
1050+
!type->hasTypeVariable() && CS.isCollectionType(type)) {
1051+
// If the type binding comes from the argument conversion, let's
1052+
// instead of binding collection types directly, try to bind
1053+
// using temporary type variables substituted for element
1054+
// types, that's going to ensure that subtype relationship is
1055+
// always preserved.
1056+
auto *BGT = type->castTo<BoundGenericType>();
1057+
auto UGT = UnboundGenericType::get(BGT->getDecl(), BGT->getParent(),
1058+
BGT->getASTContext());
1059+
1060+
auto dstLocator = TypeVar->getImpl().getLocator();
1061+
auto newType = CS.openUnboundGenericType(UGT, dstLocator)
1062+
->reconstituteSugar(/*recursive=*/false);
1063+
addNewBinding(binding.withType(newType));
1064+
}
10461065
}
10471066

10481067
if (newBindings.empty())
@@ -1062,20 +1081,6 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
10621081
if (Binding.hasDefaultedLiteralProtocol()) {
10631082
type = cs.openUnboundGenericType(type, dstLocator);
10641083
type = type->reconstituteSugar(/*recursive=*/false);
1065-
} else if (srcLocator &&
1066-
srcLocator->isLastElement<LocatorPathElt::ApplyArgToParam>() &&
1067-
!type->hasTypeVariable() && cs.isCollectionType(type)) {
1068-
// If the type binding comes from the argument conversion, let's
1069-
// instead of binding collection types directly, try to bind
1070-
// using temporary type variables substituted for element
1071-
// types, that's going to ensure that subtype relationship is
1072-
// always preserved.
1073-
auto *BGT = type->castTo<BoundGenericType>();
1074-
auto UGT = UnboundGenericType::get(BGT->getDecl(), BGT->getParent(),
1075-
BGT->getASTContext());
1076-
1077-
type = cs.openUnboundGenericType(UGT, dstLocator);
1078-
type = type->reconstituteSugar(/*recursive=*/false);
10791084
}
10801085

10811086
cs.addConstraint(ConstraintKind::Bind, TypeVar, type, srcLocator);

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3800,9 +3800,11 @@ bool ConstraintSystem::repairFailures(
38003800
if (tupleLocator->isLastElement<LocatorPathElt::SequenceElementType>())
38013801
break;
38023802

3803-
// Generic argument failures have a more general fix which is attached to a
3804-
// parent type and aggregates all argument failures into a single fix.
3805-
if (tupleLocator->isLastElement<LocatorPathElt::GenericArgument>())
3803+
// Generic argument/requirement failures have a more general fix which
3804+
// is attached to a parent type and aggregates all argument failures
3805+
// into a single fix.
3806+
if (tupleLocator->isLastElement<LocatorPathElt::AnyRequirement>() ||
3807+
tupleLocator->isLastElement<LocatorPathElt::GenericArgument>())
38063808
break;
38073809

38083810
ConstraintFix *fix;

test/Constraints/construction.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ func rdar_50668864() {
216216
struct Foo {
217217
init(anchors: [Int]) { // expected-note {{'init(anchors:)' declared here}}
218218
self = .init { _ in [] } // expected-error {{trailing closure passed to parameter of type '[Int]' that does not accept a closure}}
219-
// expected-error@-1 {{generic parameter 'Element' could not be inferred}}
220219
}
221220
}
222221
}

validation-test/Sema/type_checker_perf/slow/rdar54580427.swift renamed to validation-test/Sema/type_checker_perf/fast/rdar54580427.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// FIXME: This should be linear instead of exponential.
2-
// RUN: %scale-test --begin 1 --end 10 --step 1 --select NumLeafScopes --invert-result %s -Xfrontend=-solver-expression-time-threshold=1
1+
// RUN: %scale-test --begin 1 --end 20 --step 1 --select NumLeafScopes %s -Xfrontend=-solver-expression-time-threshold=1
32
// REQUIRES: asserts,no_asan
4-
// REQUIRES: rdar57138194,SR11770
53

64
enum Val {
75
case d([String: Val])

0 commit comments

Comments
 (0)