Skip to content

[cxx-interop] Enabling C++ interop leads to linking errors with URL.bookmark() #66501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tracked by #65808
hyp opened this issue Jun 9, 2023 · 10 comments
Closed
Tracked by #65808
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ clang importer Area → compiler: The clang importer compiler The Swift compiler itself Foundation linker error swift 5.9

Comments

@hyp
Copy link
Contributor

hyp commented Jun 9, 2023

As reported here: https://forums.swift.org/t/undefined-symbol-when-using-c-interop-and-url-bookmark/65444

@hyp hyp added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ triage needed This issue needs more specific labels labels Jun 9, 2023
@hyp hyp self-assigned this Jun 9, 2023
@hyp
Copy link
Contributor Author

hyp commented Jul 10, 2023

@plotfi could you check this one out?

@plotfi
Copy link
Contributor

plotfi commented Jul 10, 2023

@plotfi could you check this one out?

yeah I was already looking at it, thanks @hyp.

So far I can't seem to repro it with a standalone compiler, but I will try the xcode project.

@plotfi
Copy link
Contributor

plotfi commented Jul 10, 2023

Good news, @hyp I have gotten the xcode project to repro. Just gotta extract the clang invocations and see if they repro on the latest main toolchain.

@plotfi
Copy link
Contributor

plotfi commented Jul 11, 2023

Made a little bit of progress, minimized the test case:

///   ContentView.swift

import Foundation

public struct ContentView {
    public init() {
        let url = URL(string: "https://foo.bar")!
        print(url)

        try? url.bookmarkData()
    }
}

/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend \
  -frontend -c \
  -target arm64-apple-ios17.0-simulator \
  -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.0.sdk \
  -enable-objc-interop \
  -cxx-interoperability-mode=default \
  -o ContentView.o  \
  ContentView.swift

/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld \
  -syslibroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.0.sdk \
-o a.out \
-L/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator \
-L/usr/lib/swift \
ContentView.o

Alter the swift-frontend like to the following and it links:

/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend \
  -frontend -c \
  -target arm64-apple-ios17.0-simulator \
  -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.0.sdk \
  -enable-objc-interop \
  -o ContentView.o  \
  ContentView.swift

@plotfi
Copy link
Contributor

plotfi commented Jul 11, 2023

Here are the symbol diffs (from nm dump)

bad.symbols.txt
good.symbols.txt

Difference is:

< U _$s10Foundation3URLV12bookmarkData7options30includingResourceValuesForKeys10relativeToAA0D0VSo5NSURLC23BookmarkCreationOptionsV_ShySo16NSURLResourceKeyaGSgACSgtKF
< U _$s10Foundation3URLV12bookmarkData7options30includingResourceValuesForKeys10relativeToAA0D0VSo5NSURLC23BookmarkCreationOptionsV_ShySo16NSURLResourceKeyaGSgACSgtKFfA_
---
> U _$s10Foundation3URLV12bookmarkData7options30includingResourceValuesForKeys10relativeToAA0D0VSo28NSURLBookmarkCreationOptionsV_ShySo16NSURLResourceKeyaGSgACSgtKF
> T _$s10Foundation3URLV12bookmarkData7options30includingResourceValuesForKeys10relativeToAA0D0VSo28NSURLBookmarkCreationOptionsV_ShySo16NSURLResourceKeyaGSgACSgtKFfA_

Meaning:

1,2c1,2
< U Foundation.URL.bookmarkData(options: __C.NSURL.BookmarkCreationOptions, includingResourceValuesForKeys: Swift.Set<__C.NSURLResourceKey>?, relativeTo: Foundation.URL?) throws -> Foundation.Data
< U default argument 0 of Foundation.URL.bookmarkData(options: __C.NSURL.BookmarkCreationOptions, includingResourceValuesForKeys: Swift.Set<__C.NSURLResourceKey>?, relativeTo: Foundation.URL?) throws -> Foundation.Data
---
> U Foundation.URL.bookmarkData(options: __C.NSURLBookmarkCreationOptions, includingResourceValuesForKeys: Swift.Set<__C.NSURLResourceKey>?, relativeTo: Foundation.URL?) throws -> Foundation.Data
> T default argument 0 of Foundation.URL.bookmarkData(options: __C.NSURLBookmarkCreationOptions, includingResourceValuesForKeys: Swift.Set<__C.NSURLResourceKey>?, relativeTo: Foundation.URL?) throws -> Foundation.Data

@plotfi
Copy link
Contributor

plotfi commented Jul 11, 2023

Difference appears to be a missing dot '.' in NSURL.BookmarkCreationOptions, I may have fixed this in #66619

@plotfi
Copy link
Contributor

plotfi commented Jul 11, 2023

I tested things out with a swiftc built from top of tree, the issue appears to be fixed already.

plotfi@plotfi-mbp GH66501 % /Users/plotfi/opt/dev/S/build/RelWithDebInfo/BinaryCache/toolchain/bin/swift-frontend \
  -frontend -c \
  -target arm64-apple-ios17.0-simulator \
  -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.0.sdk \
  -enable-objc-interop \
  -cxx-interoperability-mode=default \
  -o ContentView.o  \
  ContentView.swift

/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld \
  -syslibroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.0.sdk \
-o a.out \
-L/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator \
-L/usr/lib/swift \
ContentView.o

ContentView.swift:11:9: warning: result of 'try?' is unused
        try? url.bookmarkData()
        ^~~~~~~~~~~~~~~~~~~~~~~
plotfi@plotfi-mbp GH66501 % ls ContentView.o a.out
ContentView.o	a.out

@hyp
Copy link
Contributor Author

hyp commented Jul 11, 2023

Thanks!

@hyp
Copy link
Contributor Author

hyp commented Jul 11, 2023

Let's leave it open for now until we have an Xcode beta out with the fix but this appears to be fixed.

@plotfi
Copy link
Contributor

plotfi commented Jul 11, 2023

Let's leave it open for now until we have an Xcode beta out with the fix but this appears to be fixed.

Agreed, I want to close once we are sure an upcoming Xcode build fixes this.

@hborla hborla removed the triage needed This issue needs more specific labels label Apr 27, 2024
@AnthonyLatsis AnthonyLatsis added Foundation linker error compiler The Swift compiler itself clang importer Area → compiler: The clang importer swift 5.9 labels Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ clang importer Area → compiler: The clang importer compiler The Swift compiler itself Foundation linker error swift 5.9
Projects
None yet
Development

No branches or pull requests

5 participants