@@ -4186,7 +4186,21 @@ ConstraintSystem::simplifyApplicableFnConstraint(
4186
4186
ConstraintLocatorBuilder outerLocator =
4187
4187
getConstraintLocator (anchor, parts, locator.getSummaryFlags ());
4188
4188
4189
- retry:
4189
+ unsigned unwrapCount = 0 ;
4190
+ if (shouldAttemptFixes ()) {
4191
+ // If we have an optional type, try forcing it to see if that
4192
+ // helps. Note that we only deal with function and metatype types
4193
+ // below, so there is no reason not to attempt to strip these off
4194
+ // immediately.
4195
+ while (auto objectType2 = desugar2->getOptionalObjectType ()) {
4196
+ type2 = objectType2;
4197
+ desugar2 = type2->getDesugaredType ();
4198
+
4199
+ // Track how many times we do this so that we can record a fix for each.
4200
+ ++unwrapCount;
4201
+ }
4202
+ }
4203
+
4190
4204
// For a function, bind the output and convert the argument to the input.
4191
4205
auto func1 = type1->castTo <FunctionType>();
4192
4206
if (auto func2 = dyn_cast<FunctionType>(desugar2)) {
@@ -4214,6 +4228,11 @@ ConstraintSystem::simplifyApplicableFnConstraint(
4214
4228
== SolutionKind::Error)
4215
4229
return SolutionKind::Error;
4216
4230
4231
+ // Record any fixes we attempted to get to the correct solution.
4232
+ while (unwrapCount-- > 0 )
4233
+ if (recordFix (FixKind::ForceOptional, getConstraintLocator (locator)))
4234
+ return SolutionKind::Error;
4235
+
4217
4236
return SolutionKind::Solved;
4218
4237
}
4219
4238
@@ -4224,23 +4243,18 @@ ConstraintSystem::simplifyApplicableFnConstraint(
4224
4243
return formUnsolved ();
4225
4244
4226
4245
// Construct the instance from the input arguments.
4227
- return simplifyConstructionConstraint (instance2, func1, subflags,
4246
+ auto simplified = simplifyConstructionConstraint (instance2, func1, subflags,
4228
4247
/* FIXME?*/ DC,
4229
4248
FunctionRefKind::SingleApply,
4230
4249
getConstraintLocator (outerLocator));
4231
- }
4232
4250
4233
- if (!shouldAttemptFixes ())
4234
- return SolutionKind::Error;
4235
-
4236
- // If we're coming from an optional type, unwrap the optional and try again.
4237
- if (auto objectType2 = desugar2->getOptionalObjectType ()) {
4238
- if (recordFix (FixKind::ForceOptional, getConstraintLocator (locator)))
4239
- return SolutionKind::Error;
4251
+ // Record any fixes we attempted to get to the correct solution.
4252
+ if (simplified == SolutionKind::Solved)
4253
+ while (unwrapCount-- > 0 )
4254
+ if (recordFix (FixKind::ForceOptional, getConstraintLocator (locator)))
4255
+ return SolutionKind::Error;
4240
4256
4241
- type2 = objectType2;
4242
- desugar2 = type2->getDesugaredType ();
4243
- goto retry;
4257
+ return simplified;
4244
4258
}
4245
4259
4246
4260
return SolutionKind::Error;
0 commit comments