[Diagnostics] Don't attempt to force unwrap invalid optional chaining in the argument position

If the argument has an extra `?` or `!`, let's not attempt to force
optional (because it's use is already invalid) and re-introduce
the constraint with unwrapped type instead. This would help to diagnose
the invalid chaining as well as any argument to parameter mismatches.

Resolves: rdar://126080504
(cherry picked from commit ef2fa4a1b6)
This commit is contained in:
Pavel Yaskevich
2025-05-26 00:12:09 -07:00
parent d220be40a5
commit 8a51175acb
2 changed files with 31 additions and 1 deletions

View File

@@ -5883,6 +5883,24 @@ bool ConstraintSystem::repairFailures(
if (hasConversionOrRestriction(ConversionRestrictionKind::Existential))
break;
if (auto *typeVar =
lhs->getOptionalObjectType()->getAs<TypeVariableType>()) {
auto *argLoc = typeVar->getImpl().getLocator();
if (argLoc->directlyAt<OptionalEvaluationExpr>()) {
auto OEE = castToExpr<OptionalEvaluationExpr>(argLoc->getAnchor());
// If the optional chain in the argument position is invalid
// let's unwrap optional and re-introduce the constraint to
// be solved later once both sides are sufficiently resolved,
// this would allow to diagnose not only the invalid unwrap
// but an invalid conversion (if any) as well.
if (hasFixFor(getConstraintLocator(OEE->getSubExpr()),
FixKind::RemoveUnwrap)) {
addConstraint(matchKind, typeVar, rhs, loc);
return true;
}
}
}
auto result = matchTypes(lhs->getOptionalObjectType(), rhs, matchKind,
TMF_ApplyingFix, locator);