Skip to content

Commit d8e418a

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 e1e9f04 commit d8e418a

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
@@ -753,7 +753,9 @@ TypeBase::getContextSubstitutions(const DeclContext *dc,
753753
SubstitutionMap TypeBase::getContextSubstitutionMap(
754754
const DeclContext *dc,
755755
GenericEnvironment *genericEnv) {
756-
if (dc == getAnyNominal() && genericEnv == nullptr)
756+
auto *nominal = getAnyNominal();
757+
if (dc == nominal && !isa<ProtocolDecl>(nominal) &&
758+
genericEnv == nullptr)
757759
return getContextSubstitutionMap();
758760

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