Compile error in SwiftUI previews only, Xcode 12.5 RC?

Unsure if this is an Xcode bug or a compiler bug - I suspect the former, but anyway, in a SwiftUI view we have some code similar to:

SomeView().padding(.top, someCondition ? 0 : 24)

This compiles and runs fine in both Xcode 12.5 and 12.4 however it fails to compile when trying to load the SwiftUI preview for this view using the 12.5 RC with the following error:

result values in '? :' expression have mismatching types 'Int' and 'CGFloat'
<snip>
Compiling failed: result values in '? :' expression have mismatching types 'Int' and 'CGFloat'

Changing the 24 to CGFloat(24) fixes the issue.

1 Like

We're also getting errors where the preview can't find some of our local types: cannot find 'X' in scope, despite normal compilation working fine.

I have tried this snippet:

import SwiftUI

struct S : View {
  var body: some View {
    EmptyView().padding(.top, true ? 0 : 24)
  }
}

With top of tree main and it did type-check just fine. Could it be possible for you to try a snapshot from swift.org to see whether it would reproduce the issue?

Actually never mind, the preview is built differently from regular code...

And snapshots don't generally work with SwiftUI previews anyway, unless something's changed recently?

Our example is more like:

struct A: View {
  var body: some View {
    B()
  }

  struct B: View {
    var body: some View {
      C() // Preview error: cannot find 'C' in scope.
    }
  }

  struct C: View {}
}

I managed to reduce this down to:

import Foundation

func test<T: ExpressibleByIntegerLiteral>(_: T) -> T {
  fatalError()
}

func bug(_: CGFloat?) {
}

bug(true ? test(0) : test(24))

This example fails type-check with 5.4 branch but works correctly on main. I have an idea why that is, will keep you posted.

5 Likes

I just wanted to close a loop on this. The PR has been merged to 5.4 release, so the issue has been addressed.

2 Likes