Skip to content

Commit eb4d8ac

Browse files
committed
Sema: Fix incorrect use of STL iterator in associated type inference
Fixes rdar://problem/127575477.
1 parent b691cce commit eb4d8ac

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,6 +2643,8 @@ bool AssociatedTypeInference::simplifyCurrentTypeWitnesses() {
26432643
anyChanged = false;
26442644
anyUnsubstituted = false;
26452645

2646+
LLVM_DEBUG(llvm::dbgs() << "Simplifying type witnesses -- iteration " << iterations << "\n");
2647+
26462648
if (++iterations > 100) {
26472649
llvm::errs() << "Too many iterations in simplifyCurrentTypeWitnesses()\n";
26482650

@@ -2678,6 +2680,10 @@ bool AssociatedTypeInference::simplifyCurrentTypeWitnesses() {
26782680
if (!typeWitness->hasTypeParameter())
26792681
continue;
26802682

2683+
LLVM_DEBUG(llvm::dbgs() << "Attempting to simplify witness for "
2684+
<< assocType->getName()
2685+
<< ": " << typeWitness << "\n";);
2686+
26812687
auto simplified = typeWitness.transformRec(
26822688
[&](TypeBase *type) -> std::optional<Type> {
26832689
// Skip.
@@ -2806,10 +2812,16 @@ AssociatedTypeInference::getSubstOptionsWithCurrentTypeWitnesses() {
28062812
return nullptr;
28072813
}
28082814

2809-
Type type = self->typeWitnesses.begin(assocType)->first;
2815+
auto found = self->typeWitnesses.begin(assocType);
2816+
if (found == self->typeWitnesses.end()) {
2817+
// Invalid code.
2818+
return ErrorType::get(thisProto->getASTContext()).getPointer();
2819+
}
2820+
2821+
Type type = found->first;
28102822
if (type->hasTypeParameter()) {
28112823
// Not fully substituted yet.
2812-
return ErrorType::get(type->getASTContext()).getPointer();
2824+
return ErrorType::get(thisProto->getASTContext()).getPointer();
28132825
}
28142826

28152827
return type->mapTypeOutOfContext().getPointer();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: not %target-typecheck-verify-swift
2+
3+
// This is highly invalid, so just don't crash.
4+
5+
struct G<T> {}
6+
7+
extension G: Collection {
8+
typealias SubSequence = DoesNotExist
9+
typealias Index = DoesNotExist
10+
11+
subscript(position: Index) -> Element { fatalError() }
12+
}
13+
14+
extension G: BidirectionalCollection where T: BidirectionalCollection {}

0 commit comments

Comments
 (0)