Skip to content

Commit 6c00d89

Browse files
committed
Assert when we expect to resolve a callee overload
This helps ensure we don't miss cases where `getChoiceFor` was meant to return a callee overload but doesn't.
1 parent 40d2f51 commit 6c00d89

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,37 @@ Type FailureDiagnostic::resolveInterfaceType(Type type,
143143
: resolvedType;
144144
}
145145

146+
/// Given an apply expr, returns true if it is expected to have a direct callee
147+
/// overload, resolvable using `getChoiceFor`. Otherwise, returns false.
148+
static bool shouldHaveDirectCalleeOverload(const ApplyExpr *apply) {
149+
auto *fnExpr = apply->getFn()->getValueProvidingExpr();
150+
151+
// An apply of an apply/subscript doesn't have a direct callee.
152+
if (isa<ApplyExpr>(fnExpr) || isa<SubscriptExpr>(fnExpr))
153+
return false;
154+
155+
// Applies of closures don't have callee overloads.
156+
if (isa<ClosureExpr>(fnExpr))
157+
return false;
158+
159+
// If the optionality changes, there's no direct callee.
160+
if (isa<BindOptionalExpr>(fnExpr) || isa<ForceValueExpr>(fnExpr) ||
161+
isa<OptionalTryExpr>(fnExpr)) {
162+
return false;
163+
}
164+
165+
// If we have an intermediate cast, there's no direct callee.
166+
if (isa<ExplicitCastExpr>(fnExpr))
167+
return false;
168+
169+
// No direct callee for an if expr.
170+
if (isa<IfExpr>(fnExpr))
171+
return false;
172+
173+
// Assume that anything else would have a direct callee.
174+
return true;
175+
}
176+
146177
Optional<FunctionArgApplyInfo>
147178
FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
148179
auto &cs = getConstraintSystem();
@@ -189,9 +220,11 @@ FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
189220
rawFnType = overload->openedType;
190221
} else {
191222
// If we didn't resolve an overload for the callee, we must be dealing with
192-
// an apply of an arbitrary function expr.
193-
auto *fnExpr = cast<CallExpr>(anchor)->getFn();
194-
rawFnType = cs.getType(fnExpr)->getRValueType();
223+
// a call of an arbitrary function expr.
224+
auto *call = cast<CallExpr>(anchor);
225+
assert(!shouldHaveDirectCalleeOverload(call) &&
226+
"Should we have resolved a callee for this?");
227+
rawFnType = cs.getType(call->getFn())->getRValueType();
195228
}
196229

197230
auto *fnType = resolveType(rawFnType)->getAs<FunctionType>();

0 commit comments

Comments
 (0)