Potential compiler bug with long compilation time from simple addition?

I understand that Swift's type inference cannot handle expressions that are too complex. However, I don't think consider this to be one of those complex expressions:

func fn<T>(_ a: T, _ b: (T) -> T) {}

fn(0) {(result) in
    result + result + (result + 1) + (result + 1)
}

This churns for over 16 seconds before bailing with a message about the expression being too complex. Is this a bug? Again, I understand the complexity issues, but this is something I know other type-inferred languages (e.g. Crystal) handle just fine...

FWIW adding a type annotation to result (e.g. (result: Int) in ...) makes it virtually instant.

Looks like it's having a very hard time resolving the expression type – : Int helps out.

Would you mind filing it https://bugs.swift.org?

It's more complex than it probably seems, because the 0 and 1 constants can be anything that is ExpressibleByIntegerLiteral (and they could all theoretically resolve to different types), and + is very overloaded. You should consider filing it though, because there is ongoing work on type checking performance.

1 Like