3

There is a compilation error in Swift, when expression is too complex: Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions.

Does anyone know how to adjust this timeout (that "reasonable time")? It will be nice to see this hint, it's very useful, but I don't want my program fail to compile at some random circumstances, e.g.: on slow machines.

Anyone have any ideas how to implement it? Or should I stop worrying?

artyom.razinov
  • 610
  • 3
  • 17
  • see http://stackoverflow.com/questions/33794574/swift-expression-was-too-complex-to-be-solved-in-reasonable-time?rq=1 – sschale Mar 12 '16 at 22:27
  • break up the expression so the error goes away and dont worry about it. I mean really, its a nobrainer – FruitAddict Mar 12 '16 at 22:29
  • 2
    The question is a valid one IMO, and different than the linked question. The question isn't about how to avoid this diagnostic when it occurs on *your* system; it's about keeping it from occurring on *other* systems. It turns out not to actually be a problem; but a reasonable reading of the diagnostic suggests that it might be. – Rob Napier Mar 12 '16 at 22:38

1 Answers1

3

That diagnostic isn't actually about time. It's about memory. So it doesn't matter how fast the computer is. There's little need to worry that it will behave differently on different machines (though see below).

From CSSolver.cpp:

  // If the solver has allocated an excessive amount of memory when solving for
  // this expression, short-circuit the binding operation and mark the parent
  // expression as "too complex".
  if (cs.TC.Context.getSolverMemory() >
        cs.TC.Context.LangOpts.SolverMemoryThreshold) {
    cs.setExpressionTooComplex(true);
    return true;
  }

(setExpressionTooComplex is the origin of that diagnostic)

The current SolverMemoryThreshold is ~15MB.

Because Swift is being ported to different architectures, it is possible (though highly unlikely IMO) that you could get this error on some platforms, but not others. Generally this would work in the other direction than you're thinking. For instance, a 32-bit system may actually be allowed to go further down the rabbit hole before this diagnostic fires (since 32-bit machines often allocate less memory for data structures). But if you hit 15 MB trying to evaluate a single expression, you probably weren't going to solve it with just a few more recursions. So this would be a very surprising and unlikely result.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610