Skip to content
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

[SR-2102] Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions #44710

Closed
swift-ci opened this issue Jul 18, 2016 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself performance regression swift 3.0 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2102
Radar rdar://problem/33688344
Original Reporter aoberoi (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Mac OS X 10.11.5, Xcode 8.0 beta 2, MacBook Pro (Retina, 15-inch, Mid 2014)

Apple Swift version 3.0 (swiftlang-800.0.33.1 clang-800.0.31)
Target: x86_64-apple-macosx10.9

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 3.0Regression, Performance, TypeChecker
Assignee aoberoi (JIRA)
Priority Medium

md5: 22daec4b18ddda35440f98e4c5e2964f

Issue Description:

Swift 3.0 doesn't seem to be able to complete type-checking on a file similar to one that could be type-checked fine in Swift 2.2.

Attached:

  • CalculatorBrain-ideal.swift: This is a file I'd expect to finish type-checking just fine with the ideal expression of my code.

  • CalculatorBrain-works.swift: This is a version of the file that does work, but required me to add more typing information.

  • CalculatorBrain-broke.swift: This is a version of the file that is just barely too much for the type-checker on my system.

@swift-ci
Copy link
Collaborator Author

Comment by Abdullah Abanmi (JIRA)

CalculatorBrain-works.swift and CalculatorBrain-broke.swift is working in Xcode 8b6 but still the ideal one dose invoke "Expression was too complex" thing.
however this code dose compile: (Xcode 8b6)

// Some comments here
class CalculatorBrain {
    
    private var accumulator = 0.0
    
    func set(operand: Double) {
        accumulator = operand
    }
    
    enum Operation {
        case constant(Double)
        case unaryOperation((Double) -> Double)
        case binaryOperation((Double, Double) -> Double)
        case equals
    }
    
    var operations: Dictionary<String, Operation> = [
        "π": .constant(M_PI),
        "e": .constant(M_E),
        "": .unaryOperation(sqrt),
        "cos": .unaryOperation(cos),
        // using `as` will invoke "expression was too complex"
        // force casting will produce a warning but it'll caomplie.
        "×": .binaryOperation({ $0 as! Double * $1 as! Double }),
        "÷": .binaryOperation({ $0 / $1 }),
        "+": .binaryOperation({ $0 + $1 }),
        "": .binaryOperation({ $0 - $1 }),
        "=": .equals,
        ]
    
    struct PendingBinaryOperationInfo {
        var binaryFunction: (Double, Double) -> Double
        var firstOperand: Double
    }
    
    private var pending: PendingBinaryOperationInfo?
    
    private func executePendingBinaryOperation() {
        if let p = pending {
            accumulator = p.binaryFunction(p.firstOperand, accumulator)
            pending = nil
        }
    }
    
    func perform(symbol: String) {
        if let operation = operations[symbol] {
            switch operation {
            case .constant(let value):
                accumulator = value
            case .unaryOperation(let function):
                accumulator = function(accumulator)
            case .binaryOperation(let function):
                executePendingBinaryOperation()
                pending = PendingBinaryOperationInfo(binaryFunction: function, firstOperand: accumulator)
            case .equals:
                executePendingBinaryOperation()
            }
        }
    }
    
    var result: Double {
        get {
            return accumulator
        }
    }
}

let cal = CalculatorBrain()
cal.set(operand: 2)
cal.perform(symbol: "×")
cal.set(operand: 2)
cal.perform(symbol: "=")
cal.result // => 4

it'll take time to compile this code with SourceKitService and lldb-roc processes having 200 %CPU.

@rudkx
Copy link
Member

rudkx commented Aug 25, 2016

Fixed by 5a8950a

@xedin
Copy link
Member

xedin commented Aug 3, 2017

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself performance regression swift 3.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants