Skip to content

Commit 4f7f5d7

Browse files
committed
[ConstraintSystem] Simplify some logic around attempting fixes for Optionals.
Only record the fixes when they have made the solution to this point successful.
1 parent fda51bb commit 4f7f5d7

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,7 +4186,21 @@ ConstraintSystem::simplifyApplicableFnConstraint(
41864186
ConstraintLocatorBuilder outerLocator =
41874187
getConstraintLocator(anchor, parts, locator.getSummaryFlags());
41884188

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+
41904204
// For a function, bind the output and convert the argument to the input.
41914205
auto func1 = type1->castTo<FunctionType>();
41924206
if (auto func2 = dyn_cast<FunctionType>(desugar2)) {
@@ -4214,6 +4228,11 @@ ConstraintSystem::simplifyApplicableFnConstraint(
42144228
== SolutionKind::Error)
42154229
return SolutionKind::Error;
42164230

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+
42174236
return SolutionKind::Solved;
42184237
}
42194238

@@ -4224,23 +4243,18 @@ ConstraintSystem::simplifyApplicableFnConstraint(
42244243
return formUnsolved();
42254244

42264245
// Construct the instance from the input arguments.
4227-
return simplifyConstructionConstraint(instance2, func1, subflags,
4246+
auto simplified = simplifyConstructionConstraint(instance2, func1, subflags,
42284247
/*FIXME?*/ DC,
42294248
FunctionRefKind::SingleApply,
42304249
getConstraintLocator(outerLocator));
4231-
}
42324250

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;
42404256

4241-
type2 = objectType2;
4242-
desugar2 = type2->getDesugaredType();
4243-
goto retry;
4257+
return simplified;
42444258
}
42454259

42464260
return SolutionKind::Error;

0 commit comments

Comments
 (0)