Skip to content

Commit 3c10e7d

Browse files
Making sure to add explicit coercion path only for coercion constraint on disjunction.
1 parent 5ef5c6f commit 3c10e7d

File tree

5 files changed

+15
-44
lines changed

5 files changed

+15
-44
lines changed

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,15 +3526,7 @@ namespace {
35263526
auto rvalueSub = cs.coerceToRValue(expr->getSubExpr());
35273527
expr->setSubExpr(rvalueSub);
35283528

3529-
auto *locator = [&]() {
3530-
// Only adding this path for explicty coercions e.g _ = a as Int
3531-
// and for non literal/array-literal subExpr.
3532-
if (!expr->isImplicit()
3533-
&& !isa<LiteralExpr>(expr->getSubExpr())
3534-
&& !isa<CollectionExpr>(expr->getSubExpr()))
3535-
return cs.getConstraintLocator(expr, LocatorPathElt::ExplicitTypeCoercion());
3536-
return cs.getConstraintLocator(expr);
3537-
}();
3529+
auto *locator = cs.getConstraintLocator(expr);
35383530

35393531
// If we weren't explicitly told by the caller which disjunction choice,
35403532
// get it from the solution to determine whether we've picked a coercion

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,15 +2661,7 @@ namespace {
26612661
CS.setType(expr->getCastTypeLoc(), toType);
26622662

26632663
auto fromType = CS.getType(expr->getSubExpr());
2664-
auto locator = [&]() {
2665-
// Only adding this path for explicty coercions e.g _ = a as Int
2666-
// and for non literal/array-literal subExpr.
2667-
if (!expr->isImplicit()
2668-
&& !isa<LiteralExpr>(expr->getSubExpr())
2669-
&& !isa<CollectionExpr>(expr->getSubExpr()))
2670-
return CS.getConstraintLocator(expr, LocatorPathElt::ExplicitTypeCoercion());
2671-
return CS.getConstraintLocator(expr);
2672-
}();
2664+
auto locator = CS.getConstraintLocator(expr);
26732665

26742666
// Add a conversion constraint for the direct conversion between
26752667
// types.

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7753,20 +7753,28 @@ void ConstraintSystem::addExplicitConversionConstraint(
77537753
SmallVector<Constraint *, 3> constraints;
77547754

77557755
auto locatorPtr = getConstraintLocator(locator);
7756-
7756+
auto coerceLocator = [&]() {
7757+
if (auto expr = dyn_cast_or_null<CoerceExpr>(locatorPtr->getAnchor())) {
7758+
// Only adding this path for explicty coercions e.g _ = a as Int
7759+
// and for non literal/array-literal subExpr.
7760+
if (!expr->isImplicit()
7761+
&& !isa<LiteralExpr>(expr->getSubExpr())
7762+
&& !isa<CollectionExpr>(expr->getSubExpr()))
7763+
return getConstraintLocator(expr, LocatorPathElt::ExplicitTypeCoercion());
7764+
}
7765+
return locatorPtr;
7766+
}();
77577767
// Coercion (the common case).
77587768
Constraint *coerceConstraint =
77597769
Constraint::create(*this, ConstraintKind::Conversion,
7760-
fromType, toType, locatorPtr);
7770+
fromType, toType, coerceLocator);
77617771
coerceConstraint->setFavored();
77627772
constraints.push_back(coerceConstraint);
77637773

7764-
auto bridgingPtr = getConstraintLocator(
7765-
locator.withPathElement(LocatorPathElt::BridgingTypeCoercion()));
77667774
// The source type can be explicitly converted to the destination type.
77677775
Constraint *bridgingConstraint =
77687776
Constraint::create(*this, ConstraintKind::BridgingConversion,
7769-
fromType, toType, bridgingPtr);
7777+
fromType, toType, locatorPtr);
77707778
constraints.push_back(bridgingConstraint);
77717779

77727780
if (allowFixes && shouldAttemptFixes()) {

lib/Sema/ConstraintLocator.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
8585
case KeyPathValue:
8686
case KeyPathComponentResult:
8787
case ExplicityTypeCoercion:
88-
case BridgingTypeCoercion:
8988
auto numValues = numNumericValuesInPathElement(elt.getKind());
9089
for (unsigned i = 0; i < numValues; ++i)
9190
id.AddInteger(elt.getValue(i));
@@ -418,10 +417,6 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
418417
case ExplicityTypeCoercion:
419418
out << "type coercion";
420419
break;
421-
422-
case BridgingTypeCoercion:
423-
out << "bridging type coercion";
424-
break;
425420
}
426421
}
427422
out << ']';

lib/Sema/ConstraintLocator.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
139139
KeyPathComponentResult,
140140
/// A explicity type coercion
141141
ExplicityTypeCoercion,
142-
/// A bridging type coercion
143-
BridgingTypeCoercion
144142
};
145143

146144
/// Determine the number of numeric values used for the given path
@@ -175,7 +173,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
175173
case KeyPathValue:
176174
case KeyPathComponentResult:
177175
case ExplicityTypeCoercion:
178-
case BridgingTypeCoercion:
179176
return 0;
180177

181178
case ContextualType:
@@ -248,7 +245,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
248245
case KeyPathValue:
249246
case KeyPathComponentResult:
250247
case ExplicityTypeCoercion:
251-
case BridgingTypeCoercion:
252248
return 0;
253249

254250
case FunctionArgument:
@@ -368,7 +364,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
368364
class OpenedGeneric;
369365
class KeyPathDynamicMember;
370366
class ExplicitTypeCoercion;
371-
class BridgingTypeCoercion;
372367

373368
PathElement(PathElementKind kind)
374369
: storage(encodeStorage(kind, 0)), storedKind(StoredKindAndValue)
@@ -909,17 +904,6 @@ class LocatorPathElt::ExplicitTypeCoercion final : public LocatorPathElt {
909904
}
910905
};
911906

912-
913-
class LocatorPathElt::BridgingTypeCoercion final : public LocatorPathElt {
914-
public:
915-
BridgingTypeCoercion()
916-
: LocatorPathElt(ConstraintLocator::BridgingTypeCoercion) {}
917-
918-
static bool classof(const LocatorPathElt *elt) {
919-
return elt->getKind() == ConstraintLocator::BridgingTypeCoercion;
920-
}
921-
};
922-
923907
/// A simple stack-only builder object that constructs a
924908
/// constraint locator without allocating memory.
925909
///

0 commit comments

Comments
 (0)