Move CG symbols to CoreFoundation to match Darwin behavior

On recent SDK of Darwin platform, Apple has moved the definition of many CG symbols(CGFloat CGSize CGRect) from CoreGraphic to CoreFoundation.

So when we use import CoreFoundation, we can use CGFloat on Darwin now. But it will require use to import the whole Foundation module on non-Darwin platform.

Also, to avoid source breaking change, we may apply some trick which Apple use on Darwin platform to avoid import struct CoreGraphics.CGFloat failing to build issue.

// Currently valid on Darwin platform with macOS 14/iOS 17 SDK
import struct CoreGraphics.CGFloat
import struct CoreFoundation.CGFloat

I have found it first in this discussion [Need Compiler Expert] Is there a way to workaround 'Symbol not found' crash on iOS 15 (sim) for APIS available since iOS 13? - #17 by Kyle-Ye

And besides it I also facing some issue on my other project:

I used to use the following code before assuming CGXX is defined in CoreGraphics

#if canImport(Darwin)
import CoreGraphics
import Foundation // swift-corelibs-foundation

Then after the transition of CGXX from CoreGraphics to CoreFoundation
I changed to use the following code

import Foundation

And assume I have a CKit which reexported CoreFoundation.
The following code compiles on Darwin platform but not on Linux nor WASM.

import CKit // or import CoreFoundation
let a: CGFloat = 0.0 // error: cannot find type 'CGFloat' in scope

Considering we will migrate to use swift-foundation later, I'm always try to avoid the direct dependency of Foundation here.
Would it be great if we can align the behavior on all platforms?

GitHub issue link: Move CG symbols to CoreFoundation to match Darwin behavior · Issue #4922 · apple/swift-corelibs-foundation · GitHub

My understanding is that CoreFoundation is not meant to be available for direct use at all on non-Darwin platforms and that it's only present to bootstrap the old Swift implementation of Foundation in swift-corelibs-foundation. Writing import CoreFoundation makes your module non-portable.

I'm actually a bit surprised CGFloat is in swift-corelibs-foundation at all. Someone at Apple can correct me if I'm wrong, but I definitely wouldn't expect the other CoreGraphics.CGGeometry types to make it into non-Darwin CF just because they moved there on Darwin. I see that they're explicitly ported in swift-corelibs-foundation in NSGeometry.swift, which is a bit unexpected. I wonder if this was just for NSValue compatibility?


You shouldn't be using CGFloat on non-Darwin platforms; its use is necessary to interoperate with a lot of Darwin API, but that's not the case on other platforms, so you should generally be using Float or Double instead in non-Darwin-specific code.


Yes, I could replace CGFloat with Double or Float. But what about CGPoint, CGRect and CGSize? Actually there are a lot of Linux code still use them widely. @scanon

Here is a related post I create years ago CGPoint and CGRect related proposal

And we currently do not have such type in Standard library or Foundation yet. (We do have them in CoreFoundation now on Darwin platform)

1 Like