Concat'ing literal arrays causes compile problems

I've come across what I believe is a bug - or at least leaves much room for improvement.
I have code along the lines of:

func bar() -> [UInt8] {
  return [ 0x00 ]
}

let foo: [UInt8] =
  bar() +
  bar() +
  ...
  bar()

Given that the types are very explicit I didn't think it should be too hard to figure out the type information. Yet if this list becomes longer compilation never finishes - at least not a reasonable time. I always aborted. Also because the CPU and memory usage was crazy.

Another similar example should also demonstrate the problem:

While this compiles fine

    let foo: [UInt8] =
    [1] +
    [2]

this does not

    let foo: [UInt8] =
    [1] +
    [2] +
    [3] +
    [4] +
    [5] +
    [6] +
    [7]

Switching to

var foo: [UInt8] = []
foo += bar()
foo += bar()
foo += bar()
foo += bar()
foo += bar()

is a really ugly workaround.

I am using

$ swift -v
Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9
/Applications/Xcode.app/Contents/Developer/usr/bin/lldb "--repl=-enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -color-diagnostics"

Any thoughts on this?

Hi,

Bugs are tracked at https://bugs.swift.org, and that would be the best place to report problems you encounter with the compiler.

The type checker is known to struggle when many literals are used in an expression. There are many bugs related to such issues already filed.

Thanks for the pointer.
I've opened a bug report over there.

Adding as [UInt8] to each parameter can help with inference performance.

I can see that helping here

let foo: [UInt8] =
    [1] as [UInt8] +
    [2] as [UInt8]

but does that even matter on this example?

func bar() -> [UInt8] {
  return [ 0x00 ]
}

let foo: [UInt8] =
  bar() as [UInt8] +
  bar() as [UInt8] +
  ...
  bar() as [UInt8]

It matters if there are overloads of bar()

1 Like

Good point. I removed all overloads but unfortunately I am still seeing the problem.