Skip to content

Commit 7cea2b8

Browse files
authored
Merge pull request #86068 from slavapestov/fix-issue-50917
Sema: Fix 'generic parameter mentioned in signature' check edge case with subscripts
2 parents 753a83e + 98aee23 commit 7cea2b8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,16 @@ void TypeChecker::checkReferencedGenericParams(GenericContext *dc) {
538538
// Collect all generic params referenced in parameter types and
539539
// return type.
540540
auto *funcTy = decl->getInterfaceType()->castTo<GenericFunctionType>();
541+
542+
// Generic parameters of the outer context are implicitly referenced, but a
543+
// subscript's interface type doesn't include the (Self) -> ... part, for
544+
// historical reasons.
545+
if (isa<SubscriptDecl>(decl)) {
546+
collectReferencedGenericParams(
547+
decl->getDeclContext()->getSelfInterfaceType(),
548+
referencedGenericParams);
549+
}
550+
541551
for (const auto &param : funcTy->getParams())
542552
collectReferencedGenericParams(param.getPlainType(), referencedGenericParams);
543553
collectReferencedGenericParams(funcTy->getResult(), referencedGenericParams);

test/Generics/function_decls.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,16 @@ func boo<T1, T2:Q, T3:Q, T4:Q, T5:Q, T6:Q> (x: T6)
9696
//expected-error@+1{{generic parameter 'U' is not used in function signature}}
9797
func baz<U:P>(_ d: U.A) {
9898
}
99+
100+
101+
// https://github.com/swiftlang/swift/issues/50917
102+
extension Collection {
103+
subscript<C: Collection>(i i: Index, j j: C.Index) -> C.Element where Element == C {
104+
return self[i][j]
105+
}
106+
107+
subscript<C: Collection>(ii i: Index, jj j: C.Index) -> C.Element {
108+
// expected-error@-1 {{generic parameter 'C' is not used in function signature}}
109+
fatalError()
110+
}
111+
}

0 commit comments

Comments
 (0)