@@ -1700,6 +1700,7 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
17001700 case ConstraintKind::UnresolvedMemberChainBase:
17011701 case ConstraintKind::PropertyWrapper:
17021702 case ConstraintKind::ClosureBodyElement:
1703+ case ConstraintKind::BindTupleOfFunctionParams:
17031704 llvm_unreachable (" Not a conversion" );
17041705 }
17051706
@@ -1839,6 +1840,7 @@ static bool matchFunctionRepresentations(FunctionType::ExtInfo einfo1,
18391840 case ConstraintKind::UnresolvedMemberChainBase:
18401841 case ConstraintKind::PropertyWrapper:
18411842 case ConstraintKind::ClosureBodyElement:
1843+ case ConstraintKind::BindTupleOfFunctionParams:
18421844 return true ;
18431845 }
18441846
@@ -2250,6 +2252,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
22502252 case ConstraintKind::UnresolvedMemberChainBase:
22512253 case ConstraintKind::PropertyWrapper:
22522254 case ConstraintKind::ClosureBodyElement:
2255+ case ConstraintKind::BindTupleOfFunctionParams:
22532256 llvm_unreachable (" Not a relational constraint" );
22542257 }
22552258
@@ -5337,6 +5340,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
53375340 case ConstraintKind::UnresolvedMemberChainBase:
53385341 case ConstraintKind::PropertyWrapper:
53395342 case ConstraintKind::ClosureBodyElement:
5343+ case ConstraintKind::BindTupleOfFunctionParams:
53405344 llvm_unreachable (" Not a relational constraint" );
53415345 }
53425346 }
@@ -6308,6 +6312,19 @@ ConstraintSystem::simplifyConstructionConstraint(
63086312 fnLocator,
63096313 ConstraintLocator::ConstructorMember));
63106314
6315+ // HACK: Bind the function's parameter list as a tuple to a type variable.
6316+ // This only exists to preserve compatibility with rdar://85263844, as it can
6317+ // affect the prioritization of bindings, which can affect behavior for tuple
6318+ // matching as tuple subtyping is currently a *weaker* constraint than tuple
6319+ // conversion.
6320+ if (!getASTContext ().isSwiftVersionAtLeast (6 )) {
6321+ auto paramTypeVar = createTypeVariable (
6322+ getConstraintLocator (locator, ConstraintLocator::ApplyArgument),
6323+ TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape);
6324+ addConstraint (ConstraintKind::BindTupleOfFunctionParams, memberType,
6325+ paramTypeVar, locator);
6326+ }
6327+
63116328 addConstraint (ConstraintKind::ApplicableFunction, fnType, memberType,
63126329 fnLocator);
63136330
@@ -7131,6 +7148,59 @@ ConstraintSystem::simplifyOptionalObjectConstraint(
71317148 return SolutionKind::Solved;
71327149}
71337150
7151+ ConstraintSystem::SolutionKind
7152+ ConstraintSystem::simplifyBindTupleOfFunctionParamsConstraint (
7153+ Type first, Type second, TypeMatchOptions flags,
7154+ ConstraintLocatorBuilder locator) {
7155+ auto simplified = simplifyType (first);
7156+ auto simplifiedCopy = simplified;
7157+
7158+ unsigned unwrapCount = 0 ;
7159+ if (shouldAttemptFixes ()) {
7160+ while (auto objectTy = simplified->getOptionalObjectType ()) {
7161+ simplified = objectTy;
7162+
7163+ // Track how many times we do this so that we can record a fix for each.
7164+ ++unwrapCount;
7165+ }
7166+
7167+ if (simplified->isPlaceholder ()) {
7168+ if (auto *typeVar = second->getAs <TypeVariableType>())
7169+ recordPotentialHole (typeVar);
7170+ return SolutionKind::Solved;
7171+ }
7172+ }
7173+
7174+ if (simplified->isTypeVariableOrMember ()) {
7175+ if (!flags.contains (TMF_GenerateConstraints))
7176+ return SolutionKind::Unsolved;
7177+
7178+ addUnsolvedConstraint (
7179+ Constraint::create (*this , ConstraintKind::BindTupleOfFunctionParams,
7180+ simplified, second, getConstraintLocator (locator)));
7181+ return SolutionKind::Solved;
7182+ }
7183+
7184+ auto *funcTy = simplified->getAs <FunctionType>();
7185+ if (!funcTy)
7186+ return SolutionKind::Error;
7187+
7188+ auto tupleTy =
7189+ AnyFunctionType::composeTuple (getASTContext (), funcTy->getParams (),
7190+ /* wantParamFlags*/ false );
7191+
7192+ addConstraint (ConstraintKind::Bind, tupleTy, second,
7193+ locator.withPathElement (ConstraintLocator::FunctionArgument));
7194+
7195+ if (unwrapCount > 0 ) {
7196+ auto *fix = ForceOptional::create (*this , simplifiedCopy, second,
7197+ getConstraintLocator (locator));
7198+ if (recordFix (fix, /* impact=*/ unwrapCount))
7199+ return SolutionKind::Error;
7200+ }
7201+ return SolutionKind::Solved;
7202+ }
7203+
71347204static bool isForKeyPathSubscript (ConstraintSystem &cs,
71357205 ConstraintLocator *locator) {
71367206 if (!locator || !locator->getAnchor ())
@@ -12046,6 +12116,10 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
1204612116 return simplifyUnresolvedMemberChainBaseConstraint (first, second, subflags,
1204712117 locator);
1204812118
12119+ case ConstraintKind::BindTupleOfFunctionParams:
12120+ return simplifyBindTupleOfFunctionParamsConstraint (first, second, subflags,
12121+ locator);
12122+
1204912123 case ConstraintKind::ValueMember:
1205012124 case ConstraintKind::UnresolvedValueMember:
1205112125 case ConstraintKind::ValueWitness:
@@ -12588,6 +12662,11 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
1258812662 return simplifyClosureBodyElementConstraint (
1258912663 constraint.getClosureElement (), constraint.getElementContext (),
1259012664 /* flags=*/ None, constraint.getLocator ());
12665+
12666+ case ConstraintKind::BindTupleOfFunctionParams:
12667+ return simplifyBindTupleOfFunctionParamsConstraint (
12668+ constraint.getFirstType (), constraint.getSecondType (), /* flags*/ None,
12669+ constraint.getLocator ());
1259112670 }
1259212671
1259312672 llvm_unreachable (" Unhandled ConstraintKind in switch." );
0 commit comments