Skip to content

Commit 04fded6

Browse files
authored
Merge pull request #7123 from DougGregor/shrink-less-aggressively-3.1
[Type checker] Don't shrink() arithmetic expressions of literals.
2 parents 6bca9f7 + d902a5f commit 04fded6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,24 @@ void ConstraintSystem::shrink(Expr *expr) {
16411641
return { true, expr };
16421642
}
16431643

1644+
/// Determine whether this is an arithmetic expression comprised entirely
1645+
/// of literals.
1646+
static bool isArithmeticExprOfLiterals(Expr *expr) {
1647+
expr = expr->getSemanticsProvidingExpr();
1648+
1649+
if (auto prefix = dyn_cast<PrefixUnaryExpr>(expr))
1650+
return isArithmeticExprOfLiterals(prefix->getArg());
1651+
1652+
if (auto postfix = dyn_cast<PostfixUnaryExpr>(expr))
1653+
return isArithmeticExprOfLiterals(postfix->getArg());
1654+
1655+
if (auto binary = dyn_cast<BinaryExpr>(expr))
1656+
return isArithmeticExprOfLiterals(binary->getArg()->getElement(0)) &&
1657+
isArithmeticExprOfLiterals(binary->getArg()->getElement(1));
1658+
1659+
return isa<IntegerLiteralExpr>(expr) || isa<FloatLiteralExpr>(expr);
1660+
}
1661+
16441662
Expr *walkToExprPost(Expr *expr) override {
16451663
if (expr == PrimaryExpr) {
16461664
// If this is primary expression and there are no candidates
@@ -1688,7 +1706,7 @@ void ConstraintSystem::shrink(Expr *expr) {
16881706
// If there are fewer than two overloads in the chain
16891707
// there is no point of solving this expression,
16901708
// because we won't be able to reduce its domain.
1691-
if (numOverloadSets > 1)
1709+
if (numOverloadSets > 1 && !isArithmeticExprOfLiterals(expr))
16921710
Candidates.push_back(Candidate(CS, expr));
16931711

16941712
return expr;

test/Constraints/operator.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,9 @@ extension P3 {
132132
}
133133

134134
struct S3 : P3, Equatable { }
135+
136+
// rdar://problem/30220565
137+
func shrinkTooFar(_ : Double, closure : ()->()) {}
138+
func testShrinkTooFar() {
139+
shrinkTooFar(0*0*0) {}
140+
}

0 commit comments

Comments
 (0)