Skip to content

[SR-11127] Swift is unable to type-check a common statistical expression #53523

@swift-ci

Description

@swift-ci
Previous ID SR-11127
Radar rdar://problem/72801678
Original Reporter kam (JIRA User)
Type Bug
Environment

Xcode 10.2.1 (1E1001)

Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)

macOS Mojave 10.14.5

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 6a2175251c6d14255f04f22de3eb5237

Issue Description:

Unexpected error when coding up some statistical work. Considering that all the variable types are declared, it is unclear what makes this so complicated that the compiler can't handle it.

To reproduce, place the following code into a playground:

import Foundation
public func standardErrorOfEstimateForObservation(standardErrorOfEstimate: Double,
                                                  sampleSize: Int,
                                                  scoreOnThePredictor: Double,
                                                  meanOfThePredictor: Double,
                                                  standardDeviationOfThePredictor: Double) {
    return standardErrorOfEstimate *
        sqrt(1.0 +
            1.0 / sampleSize +
            square(scoreOnThePredictor - meanOfThePredictor) / (sampleSize * square(standardDeviationOfThePredictor)))
}

private func square(_ x: Double) -> Double {
    return x * x
}

standardErrorOfEstimateForObservation(standardErrorOfEstimate: 1.2,
                                      sampleSize: 90,
                                      scoreOnThePredictor: 12,
                                      meanOfThePredictor: 24.2,
                                      standardDeviationOfThePredictor: 11.53)

This gives the error:

error: SwiftBug.playground:9:36: error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
    return standardErrorOfEstimate *
           ~~~~~~~~~~~~~~~~~~~~~~~~^

While I could break it up into expressions, I'm writing the code to match an equation from a paper. It helps to write the expression the same way, especially if I ask a non-programmer to review my work.

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions