Skip to content

Sema: Fix two problems in checkReferencedGenericParams() analysis #67566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,25 @@ void TypeChecker::checkReferencedGenericParams(GenericContext *dc) {
ReferencedGenericParams.insert(ty->getCanonicalType());
return Action::SkipChildren;
}

// Skip the count type, which is always a generic parameter;
// we don't consider it a reference because it only binds the
// shape and not the metadata.
if (auto *expansionTy = ty->getAs<PackExpansionType>()) {
expansionTy->getPatternType().walk(*this);
return Action::SkipChildren;
}

// Don't walk into generic type alias substitutions. This does
// not constrain `T`:
//
// typealias Foo<T> = Int
// func foo<T>(_: Foo<T>) {}
if (auto *aliasTy = dyn_cast<TypeAliasType>(ty.getPointer())) {
Type(aliasTy->getSinglyDesugaredType()).walk(*this);
return Action::SkipChildren;
}

return Action::Continue;
}

Expand Down
9 changes: 9 additions & 0 deletions test/type/pack_expansion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct Outer<each T> {
func packRef<each T>(_: repeat each T) where repeat each T: P {}

func packMemberRef<each T>(_: repeat (each T).T) where repeat each T: P {}
// expected-error@-1 {{generic parameter 'T' is not used in function signature}}

// expected-error@+1 {{'each' cannot be applied to non-pack type 'Int'}}{{31-35=}}
func invalidPackRefEachInt(_: each Int) {}
Expand Down Expand Up @@ -98,3 +99,11 @@ func golden<Z>(_ z: Z) {}
func hour<each T>(_ t: repeat each T) {
_ = (repeat golden(each t))
}

func unusedParameterPack1<each T: Sequence>(_: repeat (each T).Element) {}
// expected-error@-1 {{generic parameter 'T' is not used in function signature}}

typealias First<T, U> = T

func unusedParameterPack2<each T>(_: repeat First<Int, each T>) {}
// expected-error@-1 {{generic parameter 'T' is not used in function signature}}