Skip to content

[CSSimplify] Don't propagate contextual parameter type if it's a pack… #64531

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
Mar 23, 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
5 changes: 5 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11020,6 +11020,11 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
if (contextualTy->isTypeVariableOrMember())
return false;

// Cannot propagate pack expansion type from context,
// it has to be handled by type matching logic.
if (contextualTy->is<PackExpansionType>())
return false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand everything correctly we could allow that by using getPatternType but I think in vast majority of cases (if not all) it would result in a type variable anyway which is not a suitable type too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to wait until the pattern type is fully substituted and flattened. The pattern type in this case will be substituted with PackType{String}, and the pack expansion will flatten the pack type into just String

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I figured that it better to let it go through the matching logic than try to do anything fancy here.


// If contextual type has an error, let's wait for inference,
// otherwise contextual would interfere with diagnostics.
if (contextualTy->hasError())
Expand Down
10 changes: 10 additions & 0 deletions test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,13 @@ func invalidRepeat<each T>(t: repeat each T) {
_ = [repeat each t]
// expected-error@-1 {{value pack expansion can only appear inside a function argument list or tuple element}}
}

func test_pack_expansions_with_closures() {
func takesVariadicFunction<each T>(function: (repeat each T) -> Int) {}

func test(fn: (Int, String) -> Int, x: Int) {
takesVariadicFunction { fn(x, "") } // Ok
takesVariadicFunction { y in fn(x, y) } // Ok
takesVariadicFunction { y, z in fn(y, z) } // Ok
}
}