Skip to content

Commit eab6890

Browse files
committed
AST: Fix crash when type parameter is substituted with an existential
getContextSubstitutionMap() didn't handle the case where getAnyNominal() returns a ProtocolDecl. This should not take the "fast path", which is only suitable for concrete nominals. This manifested as a crash-on-invalid -- the user probably meant to write "T.Value: Collection" rather than "T.Value == Collection". Fixes rdar://151479861.
1 parent 06f0913 commit eab6890

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/AST/TypeSubstitution.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,9 @@ TypeBase::getContextSubstitutions(const DeclContext *dc,
747747
SubstitutionMap TypeBase::getContextSubstitutionMap(
748748
const DeclContext *dc,
749749
GenericEnvironment *genericEnv) {
750-
if (dc == getAnyNominal() && genericEnv == nullptr)
750+
auto *nominal = getAnyNominal();
751+
if (dc == nominal && !isa<ProtocolDecl>(nominal) &&
752+
genericEnv == nullptr)
751753
return getContextSubstitutionMap();
752754

753755
auto genericSig = dc->getGenericSignatureOfContext();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
protocol P1 {
4+
associatedtype Value
5+
}
6+
7+
protocol P2 {
8+
typealias A = Int
9+
}
10+
11+
struct G<T: P1> where T.Value == any Collection, T.Value.Element: P2 {}
12+
// expected-error@-1 {{cannot access associated type 'Element' from 'any Collection'; use a concrete type or generic parameter base instead}}
13+

0 commit comments

Comments
 (0)