Skip to content

Commit a451523

Browse files
authored
Merge pull request #74800 from slavapestov/fix-rdar127575477
Sema: Fix incorrect use of STL iterator in associated type inference
2 parents 2918532 + 005d83f commit a451523

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
@@ -2644,6 +2644,8 @@ bool AssociatedTypeInference::simplifyCurrentTypeWitnesses() {
26442644
anyChanged = false;
26452645
anyUnsubstituted = false;
26462646

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

@@ -2679,6 +2681,10 @@ bool AssociatedTypeInference::simplifyCurrentTypeWitnesses() {
26792681
if (!typeWitness->hasTypeParameter())
26802682
continue;
26812683

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

2810-
Type type = self->typeWitnesses.begin(assocType)->first;
2816+
auto found = self->typeWitnesses.begin(assocType);
2817+
if (found == self->typeWitnesses.end()) {
2818+
// Invalid code.
2819+
return ErrorType::get(thisProto->getASTContext()).getPointer();
2820+
}
2821+
2822+
Type type = found->first;
28112823
if (type->hasTypeParameter()) {
28122824
// Not fully substituted yet.
2813-
return ErrorType::get(type->getASTContext()).getPointer();
2825+
return ErrorType::get(thisProto->getASTContext()).getPointer();
28142826
}
28152827

28162828
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)