Skip to content

Commit ac50dfd

Browse files
committed
[CS] Fix crasher caught by stress tester
We need to be more lenient checking here as the type variable may not be bound yet.
1 parent b8e4c67 commit ac50dfd

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/Sema/CSRanking.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -771,16 +771,18 @@ static void addKeyPathDynamicMemberOverloads(
771771
/// shouldn't be compared.
772772
static Optional<std::pair<Type, Type>>
773773
getConstructorParamsAsTuples(ASTContext &ctx, Type boundTy1, Type boundTy2) {
774-
// If the bound types are placeholders, they haven't been resolved, so let's
775-
// not try and rank them.
776-
if (boundTy1->isPlaceholder() || boundTy2->isPlaceholder())
774+
auto choiceTy1 =
775+
boundTy1->lookThroughAllOptionalTypes()->getAs<FunctionType>();
776+
auto choiceTy2 =
777+
boundTy2->lookThroughAllOptionalTypes()->getAs<FunctionType>();
778+
779+
// If the type variables haven't been bound to functions yet, let's not try
780+
// and rank them.
781+
if (!choiceTy1 || !choiceTy2)
777782
return None;
778783

779-
auto choiceTy1 = boundTy1->lookThroughAllOptionalTypes();
780-
auto choiceTy2 = boundTy2->lookThroughAllOptionalTypes();
781-
782-
auto initParams1 = choiceTy1->castTo<FunctionType>()->getParams();
783-
auto initParams2 = choiceTy2->castTo<FunctionType>()->getParams();
784+
auto initParams1 = choiceTy1->getParams();
785+
auto initParams2 = choiceTy2->getParams();
784786
if (initParams1.size() != initParams2.size())
785787
return None;
786788

test/Constraints/pr39543.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=CC
2+
3+
// PR #39543: Make sure we can complete in this position without crashing.
4+
extension String {
5+
init(format: String, _: Any) { fatalError() }
6+
}
7+
extension RandomAccessCollection {
8+
func foo() {
9+
print(String(format: "", Int(distance(from:#^CC^# startIndex, to: startIndex))))
10+
}
11+
}

0 commit comments

Comments
 (0)