Skip to content

Commit 4ebedc8

Browse files
[Sema] Adjust getCalleeLocator to look into coercion operand
1 parent 6d85bd5 commit 4ebedc8

File tree

6 files changed

+38
-43
lines changed

6 files changed

+38
-43
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,12 +3249,6 @@ class ConstraintSystem {
32493249
ConstraintLocator *
32503250
getConstraintLocator(const ConstraintLocatorBuilder &builder);
32513251

3252-
/// Retrieves a locator that identifies where an overload ambiguity may be
3253-
/// present. In most cases this is a no-op, but may simplify locator e.g
3254-
/// coercions for ambiguities in the expression operand.
3255-
ConstraintLocator *
3256-
getConstraintLocatorForAmbiguity(ConstraintLocator *locator);
3257-
32583252
/// Compute a constraint locator for an implicit value-to-value
32593253
/// conversion rooted at the given location.
32603254
ConstraintLocator *

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4275,15 +4275,8 @@ namespace {
42754275
bool hasForcedOptionalResult(ExplicitCastExpr *expr) {
42764276
const auto *const TR = expr->getCastTypeRepr();
42774277
if (TR && TR->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional) {
4278-
ConstraintLocator *locator;
4279-
if (isExpr<CoerceExpr>(expr)) {
4280-
locator = cs.getConstraintLocator(
4281-
expr, {LocatorPathElt::CoercionOperand(),
4282-
ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice});
4283-
} else {
4284-
locator = cs.getConstraintLocator(
4285-
expr, ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice);
4286-
}
4278+
auto *locator = cs.getConstraintLocator(
4279+
expr, ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice);
42874280
return solution.getDisjunctionChoice(locator);
42884281
}
42894282
return false;
@@ -4357,7 +4350,7 @@ namespace {
43574350
// get it from the solution to determine whether we've picked a coercion
43584351
// or a bridging conversion.
43594352
auto *locator =
4360-
cs.getConstraintLocator(expr, LocatorPathElt::CoercionOperand());
4353+
cs.getConstraintLocator(expr, ConstraintLocator::CoercionOperand);
43614354
auto choice = solution.getDisjunctionChoice(locator);
43624355

43634356
// Handle the coercion/bridging of the underlying subexpression, where

lib/Sema/CSDiagnostics.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,21 +1225,28 @@ ASTNode InvalidCoercionFailure::getAnchor() const {
12251225
return anchor;
12261226
}
12271227

1228+
SourceLoc InvalidCoercionFailure::getLoc() const {
1229+
if (getLocator()->isForCoercion()) {
1230+
auto *CE = castToExpr<CoerceExpr>(getRawAnchor());
1231+
return CE->getAsLoc();
1232+
}
1233+
return FailureDiagnostic::getLoc();
1234+
}
1235+
12281236
bool InvalidCoercionFailure::diagnoseAsError() {
12291237
auto fromType = getFromType();
12301238
auto toType = getToType();
1231-
auto *CE = getAsExpr<CoerceExpr>(getRawAnchor());
12321239

12331240
emitDiagnostic(diag::cannot_coerce_to_type, fromType, toType);
12341241

12351242
if (UseConditionalCast) {
12361243
emitDiagnostic(diag::missing_optional_downcast)
1237-
.highlight(CE->getSourceRange())
1238-
.fixItReplace(CE->getAsLoc(), "as?");
1244+
.highlight(getSourceRange())
1245+
.fixItReplace(getLoc(), "as?");
12391246
} else {
12401247
emitDiagnostic(diag::missing_forced_downcast)
1241-
.highlight(CE->getSourceRange())
1242-
.fixItReplace(CE->getAsLoc(), "as!");
1248+
.highlight(getSourceRange())
1249+
.fixItReplace(getLoc(), "as!");
12431250
}
12441251

12451252
return true;
@@ -2662,7 +2669,7 @@ bool ContextualFailure::diagnoseAsError() {
26622669
}
26632670

26642671
bool ContextualFailure::diagnoseAsNote() {
2665-
auto *locator = getAmbiguityLocator();
2672+
auto *locator = getLocator();
26662673

26672674
auto overload = getCalleeOverloadChoiceIfAvailable(locator);
26682675
if (!(overload && overload->choice.isDecl()))
@@ -4931,7 +4938,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
49314938
}
49324939

49334940
bool MissingArgumentsFailure::diagnoseAsNote() {
4934-
auto *locator = getAmbiguityLocator();
4941+
auto *locator = getLocator();
49354942
if (auto overload = getCalleeOverloadChoiceIfAvailable(locator)) {
49364943
auto *fn = resolveType(overload->adjustedOpenedType)->getAs<AnyFunctionType>();
49374944
auto loc = overload->choice.getDecl()->getLoc();
@@ -5733,16 +5740,16 @@ bool ExtraneousArgumentsFailure::diagnoseAsError() {
57335740
}
57345741

57355742
bool ExtraneousArgumentsFailure::diagnoseAsNote() {
5736-
auto overload = getCalleeOverloadChoiceIfAvailable(getAmbiguityLocator());
5743+
auto overload = getCalleeOverloadChoiceIfAvailable(getLocator());
57375744
if (!(overload && overload->choice.isDecl()))
57385745
return false;
57395746

57405747
auto *decl = overload->choice.getDecl();
57415748
auto numArgs = getTotalNumArguments();
57425749
if (isExpr<ClosureExpr>(getAnchor())) {
57435750
emitDiagnosticAt(decl, diag::candidate_with_extraneous_args_closure,
5744-
ContextualType, ContextualType->getNumParams(), numArgs,
5745-
(numArgs == 1));
5751+
ContextualType, ContextualType->getNumParams(), numArgs,
5752+
(numArgs == 1));
57465753
} else {
57475754
emitDiagnosticAt(decl, diag::candidate_with_extraneous_args,
57485755
overload->adjustedOpenedType, numArgs, ContextualType,

lib/Sema/CSDiagnostics.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ class FailureDiagnostic {
9090

9191
ConstraintLocator *getLocator() const { return Locator; }
9292

93-
ConstraintLocator *getAmbiguityLocator() const {
94-
auto &cs = getConstraintSystem();
95-
return cs.getConstraintLocatorForAmbiguity(Locator);
96-
}
97-
9893
Type getType(ASTNode node, bool wantRValue = true) const;
9994

10095
/// Get type associated with a given ASTNode without resolving it,
@@ -2182,6 +2177,8 @@ class InvalidCoercionFailure final : public ContextualFailure {
21822177

21832178
ASTNode getAnchor() const override;
21842179

2180+
SourceLoc getLoc() const override;
2181+
21852182
bool diagnoseAsError() override;
21862183
};
21872184

lib/Sema/CSGen.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,7 +3307,7 @@ namespace {
33073307

33083308
auto fromType = CS.getType(expr->getSubExpr());
33093309
auto locator =
3310-
CS.getConstraintLocator(expr, LocatorPathElt::CoercionOperand());
3310+
CS.getConstraintLocator(expr, ConstraintLocator::CoercionOperand);
33113311

33123312
// Literal initialization (e.g. `UInt32(0)`) doesn't require
33133313
// a conversion because the literal is supposed to assume the
@@ -3327,8 +3327,12 @@ namespace {
33273327

33283328
// If the result type was declared IUO, add a disjunction for
33293329
// bindings for the result of the coercion.
3330-
if (repr && repr->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional)
3331-
return createTypeVariableAndDisjunctionForIUOCoercion(toType, locator);
3330+
if (repr &&
3331+
repr->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional) {
3332+
auto *locatorIUO = CS.getConstraintLocator(expr);
3333+
return createTypeVariableAndDisjunctionForIUOCoercion(toType,
3334+
locatorIUO);
3335+
}
33323336

33333337
return toType;
33343338
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,6 @@ ConstraintLocator *ConstraintSystem::getConstraintLocator(
422422
return getConstraintLocator(anchor, newPath);
423423
}
424424

425-
ConstraintLocator *
426-
ConstraintSystem::getConstraintLocatorForAmbiguity(ConstraintLocator *locator) {
427-
if (locator->findLast<LocatorPathElt::CoercionOperand>()) {
428-
return getConstraintLocator(simplifyLocatorToAnchor(locator));
429-
}
430-
return locator;
431-
}
432-
433425
ConstraintLocator *ConstraintSystem::getImplicitValueConversionLocator(
434426
ConstraintLocatorBuilder root, ConversionRestrictionKind restriction) {
435427
SmallVector<LocatorPathElt, 4> path;
@@ -630,6 +622,14 @@ ConstraintLocator *ConstraintSystem::getCalleeLocator(
630622
if (isExpr<ObjectLiteralExpr>(anchor))
631623
return getConstraintLocator(anchor, ConstraintLocator::ConstructorMember);
632624

625+
if (locator->isFirstElement<LocatorPathElt::CoercionOperand>()) {
626+
auto *CE = castToExpr<CoerceExpr>(anchor);
627+
locator = getConstraintLocator(CE->getSubExpr()->getValueProvidingExpr(),
628+
path.drop_front());
629+
return getCalleeLocator(locator, lookThroughApply, getType, simplifyType,
630+
getOverloadFor);
631+
}
632+
633633
return getConstraintLocator(anchor);
634634
}
635635

@@ -4980,7 +4980,7 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
49804980
const auto &solution = *entry.first;
49814981
const auto *fix = entry.second;
49824982

4983-
auto *locator = getConstraintLocatorForAmbiguity(fix->getLocator());
4983+
auto *locator = fix->getLocator();
49844984

49854985
if (locator->isForContextualType()) {
49864986
contextualFixes.push_back({&solution, fix});

0 commit comments

Comments
 (0)