Skip to content

[Type checker] Don't shrink() arithmetic expressions of literals. #7123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,24 @@ void ConstraintSystem::shrink(Expr *expr) {
return { true, expr };
}

/// Determine whether this is an arithmetic expression comprised entirely
/// of literals.
static bool isArithmeticExprOfLiterals(Expr *expr) {
expr = expr->getSemanticsProvidingExpr();

if (auto prefix = dyn_cast<PrefixUnaryExpr>(expr))
return isArithmeticExprOfLiterals(prefix->getArg());

if (auto postfix = dyn_cast<PostfixUnaryExpr>(expr))
return isArithmeticExprOfLiterals(postfix->getArg());

if (auto binary = dyn_cast<BinaryExpr>(expr))
return isArithmeticExprOfLiterals(binary->getArg()->getElement(0)) &&
isArithmeticExprOfLiterals(binary->getArg()->getElement(1));

return isa<IntegerLiteralExpr>(expr) || isa<FloatLiteralExpr>(expr);
}

Expr *walkToExprPost(Expr *expr) override {
if (expr == PrimaryExpr) {
// If this is primary expression and there are no candidates
Expand Down Expand Up @@ -1688,7 +1706,7 @@ void ConstraintSystem::shrink(Expr *expr) {
// If there are fewer than two overloads in the chain
// there is no point of solving this expression,
// because we won't be able to reduce its domain.
if (numOverloadSets > 1)
if (numOverloadSets > 1 && !isArithmeticExprOfLiterals(expr))
Candidates.push_back(Candidate(CS, expr));

return expr;
Expand Down
6 changes: 6 additions & 0 deletions test/Constraints/operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,9 @@ extension P3 {
}

struct S3 : P3, Equatable { }

// rdar://problem/30220565
func shrinkTooFar(_ : Double, closure : ()->()) {}
func testShrinkTooFar() {
shrinkTooFar(0*0*0) {}
}