Skip to content

Commit 9a01e87

Browse files
committed
Sema: Address FIXME resulting in a crash in filterProtocolRequirements()
1 parent 4fa2e97 commit 9a01e87

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,10 +3888,19 @@ filterProtocolRequirements(
38883888
return Filtered;
38893889
}
38903890

3891-
const auto getProtocolSubstitutionMap = [&](ValueDecl *Req) {
3892-
auto *const PD = cast<ProtocolDecl>(Req->getDeclContext());
3893-
auto Conformance = lookupConformance(Adoptee, PD);
3894-
return SubstitutionMap::getProtocolSubstitutions(Conformance);
3891+
const auto getProtocolSubstitutionMap = [&](ValueDecl *req) {
3892+
ASSERT(isa<ProtocolDecl>(req->getDeclContext()));
3893+
auto genericSig = req->getInnermostDeclContext()
3894+
->getGenericSignatureOfContext();
3895+
SmallVector<Type, 2> args;
3896+
for (auto paramTy : genericSig.getGenericParams()) {
3897+
if (args.empty())
3898+
args.push_back(Adoptee);
3899+
else
3900+
args.push_back(paramTy);
3901+
}
3902+
return SubstitutionMap::get(genericSig, args,
3903+
LookUpConformanceInModule());
38953904
};
38963905

38973906
llvm::SmallDenseMap<DeclName, llvm::SmallVector<ValueDecl *, 2>, 4>
@@ -3907,11 +3916,11 @@ filterProtocolRequirements(
39073916
auto OverloadTy = Req->getOverloadSignatureType();
39083917
if (OverloadTy) {
39093918
auto Subs = getProtocolSubstitutionMap(Req);
3910-
// FIXME: This is wrong if the overload has its own generic parameters
3911-
if (auto GenericFnTy = dyn_cast<GenericFunctionType>(OverloadTy))
3919+
if (auto GenericFnTy = dyn_cast<GenericFunctionType>(OverloadTy)) {
39123920
OverloadTy = GenericFnTy.substGenericArgs(Subs);
3913-
else
3921+
} else {
39143922
OverloadTy = OverloadTy.subst(Subs)->getCanonicalType();
3923+
}
39153924
}
39163925
if (llvm::any_of(DeclsByName[Req->getName()], [&](ValueDecl *OtherReq) {
39173926
auto OtherOverloadTy = OtherReq->getOverloadSignatureType();
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 A
5+
}
6+
7+
protocol P2 {
8+
func f<T>(_: T, _: T) // expected-note {{protocol requires function 'f' with type '<T> (T, T) -> ()'}}
9+
func f<T: P1>(_: T, _: T.A) // expected-note {{protocol requires function 'f' with type '<T> (T, T.A) -> ()'}}
10+
}
11+
12+
struct S: P2 {} // expected-error {{type 'S' does not conform to protocol 'P2'}}
13+
// expected-note@-1 {{add stubs for conformance}}

validation-test/compiler_crashers_2/2617a198505b47e4.swift renamed to validation-test/compiler_crashers_2_fixed/2617a198505b47e4.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// {"signature":"swift::rewriting::RequirementMachine::getReducedShape(swift::Type, llvm::ArrayRef<swift::GenericTypeParamType*>) const"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
protocol a{ b < c > (c, _ : c}
44
protocol d : a{
55
b<c : e>(c, c.c) protocol e {

0 commit comments

Comments
 (0)