mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[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:
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user