Skip to content

Sema: Fix incorrect use of STL iterator in associated type inference #74800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/Sema/AssociatedTypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,8 @@ bool AssociatedTypeInference::simplifyCurrentTypeWitnesses() {
anyChanged = false;
anyUnsubstituted = false;

LLVM_DEBUG(llvm::dbgs() << "Simplifying type witnesses -- iteration " << iterations << "\n");

if (++iterations > 100) {
llvm::errs() << "Too many iterations in simplifyCurrentTypeWitnesses()\n";

Expand Down Expand Up @@ -2679,6 +2681,10 @@ bool AssociatedTypeInference::simplifyCurrentTypeWitnesses() {
if (!typeWitness->hasTypeParameter())
continue;

LLVM_DEBUG(llvm::dbgs() << "Attempting to simplify witness for "
<< assocType->getName()
<< ": " << typeWitness << "\n";);

auto simplified = typeWitness.transformRec(
[&](TypeBase *type) -> std::optional<Type> {
// Skip.
Expand Down Expand Up @@ -2807,10 +2813,16 @@ AssociatedTypeInference::getSubstOptionsWithCurrentTypeWitnesses() {
return nullptr;
}

Type type = self->typeWitnesses.begin(assocType)->first;
auto found = self->typeWitnesses.begin(assocType);
if (found == self->typeWitnesses.end()) {
// Invalid code.
return ErrorType::get(thisProto->getASTContext()).getPointer();
}

Type type = found->first;
if (type->hasTypeParameter()) {
// Not fully substituted yet.
return ErrorType::get(type->getASTContext()).getPointer();
return ErrorType::get(thisProto->getASTContext()).getPointer();
}

return type->mapTypeOutOfContext().getPointer();
Expand Down
14 changes: 14 additions & 0 deletions test/decl/protocol/req/rdar127575477.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: not %target-typecheck-verify-swift

// This is highly invalid, so just don't crash.

struct G<T> {}

extension G: Collection {
typealias SubSequence = DoesNotExist
typealias Index = DoesNotExist

subscript(position: Index) -> Element { fatalError() }
}

extension G: BidirectionalCollection where T: BidirectionalCollection {}