Skip to content

Commit bfa6d73

Browse files
committed
[Function builders] Fake Expr-based locators for now.
The right solution is to extend the notion of the "anchor" of a locator to also cover statements (and TypeReprs, and Patterns, and more), so this is a stop-gap.
1 parent cf96e65 commit bfa6d73

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,11 +1054,33 @@ Optional<BraceStmt *> TypeChecker::applyFunctionBuilderBodyTransform(
10541054
// Build a constraint system in which we can check the body of the function.
10551055
ConstraintSystem cs(func, options);
10561056

1057+
// Find an expression... any expression... to use for a locator.
1058+
// FIXME: This is a hack because we don't have the notion of locators that
1059+
// refer to statements.
1060+
Expr *fakeAnchor = nullptr;
1061+
{
1062+
class FindExprWalker : public ASTWalker {
1063+
Expr *&fakeAnchor;
1064+
1065+
public:
1066+
explicit FindExprWalker(Expr *&fakeAnchor) : fakeAnchor(fakeAnchor) { }
1067+
1068+
std::pair<bool, Expr *> walkToExprPre(Expr *E) {
1069+
if (!fakeAnchor)
1070+
fakeAnchor = E;
1071+
1072+
return { false, nullptr };
1073+
}
1074+
} walker(fakeAnchor);
1075+
1076+
func->getBody()->walk(walker);
1077+
}
1078+
10571079
// FIXME: check the result
10581080
cs.matchFunctionBuilder(func, builderType, resultContextType,
10591081
resultConstraintKind,
1060-
/*calleeLocator=*/nullptr,
1061-
/*FIXME:*/ConstraintLocatorBuilder(nullptr));
1082+
/*calleeLocator=*/cs.getConstraintLocator(fakeAnchor),
1083+
/*FIXME:*/cs.getConstraintLocator(fakeAnchor));
10621084

10631085
// Solve the constraint system.
10641086
SmallVector<Solution, 4> solutions;

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt(
10051005
// If anchor is n explicit address-of, or expression which produces
10061006
// an l-value (e.g. first argument of `+=` operator), let's not
10071007
// suggest default value here because that would produce r-value type.
1008-
if (isa<InOutExpr>(anchor))
1008+
if (!anchor || isa<InOutExpr>(anchor))
10091009
return;
10101010

10111011
auto &cs = getConstraintSystem();

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,13 +3653,14 @@ ConstraintSystem::getFunctionArgApplyInfo(ConstraintLocator *locator) {
36533653
assert(!shouldHaveDirectCalleeOverload(call) &&
36543654
"Should we have resolved a callee for this?");
36553655
rawFnType = getType(call->getFn());
3656-
} else {
3656+
} else if (auto *apply = dyn_cast<ApplyExpr>(anchor)) {
36573657
// FIXME: ArgumentMismatchFailure is currently used from CSDiag, meaning
36583658
// we can end up a BinaryExpr here with an unresolved callee. It should be
36593659
// possible to remove this once we've gotten rid of the old CSDiag logic
36603660
// and just assert that we have a CallExpr.
3661-
auto *apply = cast<ApplyExpr>(anchor);
36623661
rawFnType = getType(apply->getFn());
3662+
} else {
3663+
return None;
36633664
}
36643665
}
36653666

0 commit comments

Comments
 (0)