Skip to content

Commit 07168d7

Browse files
committed
Sema: Open-code mapTypeOutOfContext() on weird types
Associated type inference likes to wrap concrete types in DependentMemberTypes. Calling mapTypeOutOfContext() on such a type can crash. Open-code the transform instead. Longer term, I want to clean up the invariants here and stop mapping things in and out of different contexts randomly.
1 parent d0bd026 commit 07168d7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,8 +1294,17 @@ AssociatedTypeInference::getSubstOptionsWithCurrentTypeWitnesses() {
12941294
if (auto *aliasTy = dyn_cast<TypeAliasType>(type.getPointer()))
12951295
type = aliasTy->getSinglyDesugaredType();
12961296

1297-
return type->hasArchetype() ? type->mapTypeOutOfContext().getPointer()
1298-
: type.getPointer();
1297+
if (type->hasArchetype()) {
1298+
type = type.transformRec([&](Type t) -> llvm::Optional<Type> {
1299+
if (auto *archetypeTy = dyn_cast<ArchetypeType>(t.getPointer())) {
1300+
if (!isa<OpaqueTypeArchetypeType>(archetypeTy))
1301+
return archetypeTy->getInterfaceType();
1302+
}
1303+
return llvm::None;
1304+
});
1305+
}
1306+
1307+
return type.getPointer();
12991308
};
13001309
return options;
13011310
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference
2+
// RUN: not %target-typecheck-verify-swift -disable-experimental-associated-type-inference
3+
4+
struct FooIterator<T: Sequence>: IteratorProtocol {
5+
typealias Element = T.Element
6+
7+
mutating func next() -> Element? { fatalError() }
8+
}
9+
10+
struct FooSequence<Element>: Sequence {
11+
func makeIterator() -> FooIterator<Self> { fatalError() }
12+
}

0 commit comments

Comments
 (0)