From b4937be2537ac45cd59cda8d66e50e0a250a0932 Mon Sep 17 00:00:00 2001 From: Pavel Yaskevich Date: Wed, 22 Mar 2023 00:36:36 -0700 Subject: [PATCH] [CSSimplify] Don't propagate contextual parameter type if it's a pack expansion Pack expansion types are handled by matching logic, optimization that attempts to propagate types into the body of the closure should consider them unsuitable even if the pack expansion matches a single parameter. --- lib/Sema/CSSimplify.cpp | 5 +++++ test/Constraints/pack-expansion-expressions.swift | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index 3da3868e6568b..d9f1d46a238df 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -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()) + return false; + // If contextual type has an error, let's wait for inference, // otherwise contextual would interfere with diagnostics. if (contextualTy->hasError()) diff --git a/test/Constraints/pack-expansion-expressions.swift b/test/Constraints/pack-expansion-expressions.swift index f185a2061910f..af71c93c095db 100644 --- a/test/Constraints/pack-expansion-expressions.swift +++ b/test/Constraints/pack-expansion-expressions.swift @@ -261,3 +261,13 @@ func invalidRepeat(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(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 + } +}