module 'Builtin' has no member named convertUnownedUnsafeToGuaranteed

Viewed 475

Yesterday my Mac decided to auto update Xcode 11 to 12, but oops, I still need Xcode 11. So I installed 11.7 directly from https://developer.apple.com/download/more/ .

Now all my projects fail to build with the error module 'Builtin' has no member named convertUnownedUnsafeToGuaranteed. It's always when compiling some framework (e.g. Alamofire) but it's a different framework in each project. The projects are all set to use Swift 5.

I reinstalled the Xcode 12 command line tools, which didn't help. I tried to install the Xcode 11 command line tools, but the installer tells me my OS is too new (I'm on Big Sur 20A5374g).

xcode-switch'ing from one to the other doesn't help. Neither does any amount of project cleaning/deleting build folders.

Google says I'm the only person running into this. Anyone else? Ideas?

1 Answers

I don't fully understand the internal issue that occurs when this error happens but I HAVE found a way to solve it on my local system. (Mojave, Xcode 11.7) My particular problem was I inserted the iOS 14 SDK in place of the iOS 13 SDK Xcode 11.7 originally shipped with. It only gave me problems when trying to build any swift code. (which i try to avoid like the plague personally)

I ended up writing a script that changes back and forth between my iOS 14 and iOS 13 SDK folders to temporarily 'solve' the issue, but from further investigation it appears to be what i suspected all along: a need for a updated swift toolchain (so much for ABI stability, eh?)

https://forums.swift.org/t/error-abort-trap-6-when-compiling-using-xcode-12-beta-12a6159-with-swift-5-2-4-release-toolchain/37942

The swift toolchain that ships with newer versions of Xcode must have this function added to 'builtin' (as similarly mentioned on that link above)

I haven't tried installing anything passed swift 5.0 toolchain, but I presume attempting to install a newer one will potentially provide a more proper mitigation of this issue.

EDIT: Upon further investigation installing these toolchains won't let you build to device, however, I did find a bit of a hacky solution if you are ready to get your hands dirty (if this ever happens again, or happens to anyone else)

if you open the following file in a text editor (i did it in vim) /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/swift/Swift.swiftmodule/arm64-apple-ios.swiftinterface

and search for 'convertUnownedUnsafeToGuaranteed' you should happen upon this function:

  @inlinable @_transparent public func _withUnsafeGuaranteedRef<Result>(_ body: (Instance) throws -> Result) rethrows -> Result {
var tmp = self
// Builtin.convertUnownedUnsafeToGuaranteed expects to have a base value
// that the +0 value depends on. In this case, we are assuming that is done
// for us opaquely already. So, the builtin will emit a mark_dependence on a
// trivial object. The optimizer knows to eliminate that so we do not have
// any overhead from this.
let fakeBase: Int? = nil
return try body(Builtin.convertUnownedUnsafeToGuaranteed(fakeBase,
                                                         &tmp._value))
}

you can comment it out using standard /* */ and then put this in its place and it /should/ get passed that error.

@inlinable public func _withUnsafeGuaranteedRef<Result>(_ body: (Instance) throws -> Result) rethrows -> Result {
let (guaranteedInstance, token) = Builtin.unsafeGuaranteed(_value)
let result = try body(guaranteedInstance)
Builtin.unsafeGuaranteedEnd(token)
return result
}

If you are curious where i found this function, its inside the iOS 13.x SDK version of the same file. located at: (if you have an XCode installed with a iOS 13 SDK anywhere) /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/swift/Swift.swiftmodule/arm64.swiftinterface

If you want to see the exact path of the iOS SDK you are building with xcrun can be extremely helpful:

xcrun --sdk iphoneos --show-sdk-path

yields:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk

Presumably, you would need to repeat these steps for any other architecture you are targeting (ie arm64e-apple-ios.swiftinterface) etc.

Related