Skip to content

Commit cac0d07

Browse files
Attempting UnnecessaryCoercion for ExplicityTypeCoercion on CSSimplify
1 parent 4385dd8 commit cac0d07

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,7 +2587,8 @@ bool ConstraintSystem::repairFailures(
25872587
});
25882588
};
25892589

2590-
if (path.empty()) {
2590+
if (path.empty() ||
2591+
path.back().getKind() == ConstraintLocator::ExplicitTypeCoercion) {
25912592
if (!anchor)
25922593
return false;
25932594

@@ -3212,9 +3213,16 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
32123213
// let's defer it until later proper check.
32133214
if (!(desugar1->is<DependentMemberType>() &&
32143215
desugar2->is<DependentMemberType>())) {
3215-
// If the types are obviously equivalent, we're done.
3216-
if (desugar1->isEqual(desugar2) && !isa<InOutType>(desugar2)) {
3217-
return getTypeMatchSuccess();
3216+
if (desugar1->isEqual(desugar2)) {
3217+
if (kind >= ConstraintKind::Conversion &&
3218+
!flags.contains(TMF_ApplyingFix)) {
3219+
if (RemoveUnnecessaryCoercion::attempt(*this, type1, type2,
3220+
getConstraintLocator(locator))) {
3221+
return getTypeMatchFailure(locator);
3222+
}
3223+
}
3224+
if (!isa<InOutType>(desugar2))
3225+
return getTypeMatchSuccess();
32183226
}
32193227
}
32203228

@@ -7999,6 +8007,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
79998007
case FixKind::GenericArgumentsMismatch:
80008008
case FixKind::AllowMutatingMemberOnRValueBase:
80018009
case FixKind::AllowTupleSplatForSingleParameter:
8010+
case FixKind::RemoveUnnecessaryCoercion:
80028011
llvm_unreachable("handled elsewhere");
80038012
}
80048013

@@ -8305,11 +8314,20 @@ void ConstraintSystem::addExplicitConversionConstraint(
83058314
SmallVector<Constraint *, 3> constraints;
83068315

83078316
auto locatorPtr = getConstraintLocator(locator);
8317+
ConstraintLocator *coerceLocator = locatorPtr;
8318+
8319+
if (allowFixes && shouldAttemptFixes()) {
8320+
auto *anchor = locator.getAnchor();
8321+
if (isa<CoerceExpr>(anchor) && !anchor->isImplicit()) {
8322+
coerceLocator =
8323+
getConstraintLocator(anchor, LocatorPathElt::ExplicitTypeCoercion());
8324+
}
8325+
}
83088326

83098327
// Coercion (the common case).
83108328
Constraint *coerceConstraint =
83118329
Constraint::create(*this, ConstraintKind::Conversion,
8312-
fromType, toType, locatorPtr);
8330+
fromType, toType, coerceLocator);
83138331
coerceConstraint->setFavored();
83148332
constraints.push_back(coerceConstraint);
83158333

0 commit comments

Comments
 (0)