Skip to content

Commit 754e8e2

Browse files
committed
Sema: Eradicate uses of typeCheckExpressionShallow() from IfExpr and IfStmt rewriting
Instead of spinning up a new ConstraintSystem and solving it, we can just directly construct a call to getBuiltinLogicValue().
1 parent 8ba30d7 commit 754e8e2

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

lib/Sema/CSApply.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,11 +3298,7 @@ namespace {
32983298
auto cond
32993299
= solution.convertBooleanTypeToBuiltinI1(expr->getCondExpr(),
33003300
cs.getConstraintLocator(expr));
3301-
if (!cond) {
3302-
cs.setType(expr->getCondExpr(), ErrorType::get(resultTy));
3303-
} else {
3304-
expr->setCondExpr(cond);
3305-
}
3301+
expr->setCondExpr(cond);
33063302

33073303
// Coerce the then/else branches to the common type.
33083304
expr->setThenExpr(coerceToType(expr->getThenExpr(), resultTy,
@@ -8502,45 +8498,48 @@ Solution::convertBooleanTypeToBuiltinI1(Expr *expr,
85028498
// Find the builtin method.
85038499
if (members.size() != 1) {
85048500
tc.diagnose(expr->getLoc(), diag::broken_bool);
8505-
return nullptr;
8501+
return expr;
85068502
}
85078503
auto *builtinMethod = dyn_cast<FuncDecl>(members[0].getValueDecl());
85088504
if (!builtinMethod) {
85098505
tc.diagnose(expr->getLoc(), diag::broken_bool);
8510-
return nullptr;
8506+
return expr;
85118507
}
85128508

8513-
// Form a reference to the builtin method.
8514-
Expr *memberRef = new (ctx) MemberRefExpr(expr, SourceLoc(),
8515-
builtinMethod,
8516-
DeclNameLoc(expr->getLoc()),
8517-
/*Implicit=*/true);
8518-
cs.cacheSubExprTypes(memberRef);
8519-
cs.setSubExprTypes(memberRef);
8520-
bool failed = tc.typeCheckExpressionShallow(memberRef, cs.DC);
8521-
cs.cacheExprTypes(memberRef);
8522-
assert(!failed && "Could not reference witness?");
8523-
(void)failed;
8509+
// The method is not generic, so there are no substitutions.
8510+
auto builtinMethodType = builtinMethod->getInterfaceType()
8511+
->castTo<FunctionType>();
8512+
8513+
// Form an unbound reference to the builtin method.
8514+
auto *declRef = new (ctx) DeclRefExpr(builtinMethod,
8515+
DeclNameLoc(expr->getLoc()),
8516+
/*Implicit=*/true);
8517+
declRef->setFunctionRefKind(FunctionRefKind::DoubleApply);
8518+
cs.setType(declRef, builtinMethodType);
85248519

8525-
// Call the builtin method.
85268520
auto getType = [&](const Expr *E) -> Type {
85278521
return cs.getType(E);
85288522
};
85298523

8530-
expr = CallExpr::createImplicit(ctx, memberRef, { }, { }, getType);
8531-
cs.cacheSubExprTypes(expr);
8532-
cs.setSubExprTypes(expr);
8533-
failed = tc.typeCheckExpressionShallow(expr, cs.DC);
8534-
cs.cacheExprTypes(expr);
8535-
assert(!failed && "Could not call witness?");
8536-
(void)failed;
8524+
// Apply 'self' to get the method value.
8525+
auto *methodRef = new (ctx) DotSyntaxCallExpr(declRef,
8526+
SourceLoc(),
8527+
expr);
8528+
cs.setType(methodRef, builtinMethodType->getResult());
85378529

8538-
if (expr && !cs.getType(expr)->isBuiltinIntegerType(1)) {
8530+
// Apply the empty argument list to get the final result.
8531+
auto *result = CallExpr::createImplicit(ctx, methodRef,
8532+
{ }, { }, getType);
8533+
cs.setType(result, builtinMethodType->getResult()
8534+
->castTo<FunctionType>()->getResult());
8535+
cs.setType(result->getArg(), ctx.TheEmptyTupleType);
8536+
8537+
if (!cs.getType(result)->isBuiltinIntegerType(1)) {
85398538
tc.diagnose(expr->getLoc(), diag::broken_bool);
8540-
return nullptr;
8539+
return result;
85418540
}
85428541

8543-
return expr;
8542+
return result;
85448543
}
85458544

85468545
Expr *Solution::convertOptionalToBool(Expr *expr,

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2613,7 +2613,7 @@ bool TypeChecker::typeCheckCondition(Expr *&expr, DeclContext *dc) {
26132613

26142614
// Convert the result to a Builtin.i1.
26152615
Expr *appliedSolution(constraints::Solution &solution,
2616-
Expr *expr) override {
2616+
Expr *expr) override {
26172617
auto &cs = solution.getConstraintSystem();
26182618

26192619
auto converted =

0 commit comments

Comments
 (0)