Skip to content
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

If compiled as an xcframework, the package produces a non-compiling swiftinterface file #157

Closed
2 tasks done
flockoffiles opened this issue Aug 12, 2022 · 7 comments · Fixed by #162
Closed
2 tasks done
Labels
bug Something isn't working
Milestone

Comments

@flockoffiles
Copy link

Information

  • Package version: 1.0.2 (or latest main)
  • Platform version: MacOS 12.5
  • Swift version: swift-driver version: 1.45.2 Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)

Checklist

  • If possible, I've reproduced the issue using the main branch of this package.
  • I've searched for existing GitHub issues.

Steps to Reproduce

  1. Compile the package as an xcframework (with library evolution enabled). There are many ways to do this.
    I used the following:
swift create-xcframework OrderedCollections --configuration debug --stack-evolution --platform ios --xc-setting CLANG_ENABLE_CODE_COVERAGE=NO --xc-setting GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=NO --xc-setting SWIFT_SERIALIZE_DEBUGGING_OPTIONS=NO --xc-setting OTHER_SWIFT_FLAGS="-Xfrontend -no-serialize-debugging-options" --xc-setting DEBUG_INFORMATION_FORMAT=dwarf-with-dsym
  1. Try to use the resulting xcframework in an iOS app. Just create an empty SwiftUI based app from a template and add the line:
import OrderedCollections

(Please also add the xcframework to Build Phases -> Link Binary with Libraries)

  1. Build (Cmd+B)

Expected behavior

The app must compile without errors.

Actual behavior

Compiler gives errors in the swiftinterface file of the xcframework:

 /Users/USER/Library/Developer/Xcode/DerivedData/TestOrderedCollections-dugnwyfdwqgmnmhkxmqtpqgywhvo/Build/Products/Debug-iphonesimulator/OrderedCollections.framework/Modules/OrderedCollections.swiftmodule/x86_64-apple-ios-simulator.swiftinterface:2734:22: error: type alias 'Element' references itself
    public typealias Element = OrderedCollections.OrderedSet<Element>.Element
                     ^
/Users/USER/Library/Developer/Xcode/DerivedData/TestOrderedCollections-dugnwyfdwqgmnmhkxmqtpqgywhvo/Build/Products/Debug-iphonesimulator/OrderedCollections.framework/Modules/OrderedCollections.swiftmodule/x86_64-apple-ios-simulator.swiftinterface:2734:62: note: while resolving type 'Element'
    public typealias Element = OrderedCollections.OrderedSet<Element>.Element
                                                             ^
/Users/USER/Library/Developer/Xcode/DerivedData/TestOrderedCollections-dugnwyfdwqgmnmhkxmqtpqgywhvo/Build/Products/Debug-iphonesimulator/OrderedCollections.framework/Modules/OrderedCollections.swiftmodule/x86_64-apple-ios-simulator.swiftinterface:2734:71: note: while resolving type 'OrderedCollections.OrderedSet<Element>.Element'
    public typealias Element = OrderedCollections.OrderedSet<Element>.Element
                                                                      ^
/Users/USER/Library/Developer/Xcode/DerivedData/TestOrderedCollections-dugnwyfdwqgmnmhkxmqtpqgywhvo/Build/Products/Debug-iphonesimulator/OrderedCollections.framework/Modules/OrderedCollections.swiftmodule/x86_64-apple-ios-simulator.swiftinterface:2811:1: error: type 'OrderedSet<Element>.UnorderedView' does not conform to protocol 'SetAlgebra'
extension OrderedCollections.OrderedSet.UnorderedView : Swift.SetAlgebra {
^
Swift.SetAlgebra:2:20: note: protocol requires nested type 'Element'; do you want to add it?
    associatedtype Element
                   ^
/Users/USER/Library/Developer/Xcode/DerivedData/TestOrderedCollections-dugnwyfdwqgmnmhkxmqtpqgywhvo/Build/Products/Debug-iphonesimulator/OrderedCollections.framework/Modules/OrderedCollections.swiftmodule/x86_64-apple-ios-simulator.swiftinterface:2746:20: error: extraneous argument label '_base:' in call
      UnorderedView(_base: self)
                   ^~~~~~~~
                    
/Users/USER/Library/Developer/Xcode/DerivedData/TestOrderedCollections-dugnwyfdwqgmnmhkxmqtpqgywhvo/Build/Products/Debug-iphonesimulator/OrderedCollections.framework/Modules/OrderedCollections.swiftmodule/x86_64-apple-ios-simulator.swiftinterface:2749:31: error: extraneous argument label '_base:' in call
      var view = UnorderedView(_base: self)
                              ^~~~~~~~
                               
/Users/USER/Library/Developer/Xcode/DerivedData/TestOrderedCollections-dugnwyfdwqgmnmhkxmqtpqgywhvo/Build/Products/Debug-iphonesimulator/OrderedCollections.framework/Modules/OrderedCollections.swiftmodule/x86_64-apple-ios-simulator.swiftinterface:2752:14: error: cannot yield immutable value of type '_' as an inout yield of type 'OrderedSet<Element>.UnorderedView'
      yield &view
             ^
/Users/USER/Library/Developer/Xcode/DerivedData/TestOrderedCollections-dugnwyfdwqgmnmhkxmqtpqgywhvo/Build/Products/Debug-iphonesimulator/OrderedCollections.framework/Modules/OrderedCollections.swiftmodule/x86_64-apple-ios-simulator.swiftinterface:2837:35: error: type of expression is ambiguous without more context
    let (inserted, index) = _base.append(newMember)
                            ~~~~~~^~~~~~~~~~~~~~~~~
/Users/USER/Library/Developer/Xcode/DerivedData/TestOrderedCollections-dugnwyfdwqgmnmhkxmqtpqgywhvo/Build/Products/Debug-iphonesimulator/OrderedCollections.framework/Modules/OrderedCollections.swiftmodule/x86_64-apple-ios-simulator.swiftinterface:2841:35: error: type of expression is ambiguous without more context
    let (inserted, index) = _base.append(newMember)
                            ~~~~~~^~~~~~~~~~~~~~~~~
/Users/USER/Developer/Temp/TestOrderedCollections/TestOrderedCollections/TestOrderedCollectionsApp.swift:9:8: error: failed to build module 'OrderedCollections' for importation due to the errors above; the textual interface may be broken by project issues or a compiler bug
import OrderedCollections
@flockoffiles flockoffiles added the bug Something isn't working label Aug 12, 2022
@flockoffiles
Copy link
Author

I have attached a test XCode project that demonstrates the issue:

TestOrderedCollections.zip

@flockoffiles
Copy link
Author

This problem propagates to other frameworks that use this one as a dependency.
Example: pointfreeco/swift-composable-architecture#890

@flockoffiles flockoffiles changed the title If compiled as an xcframework, the package produces a not compiling swiftinterface file If compiled as an xcframework, the package produces a non-compiling swiftinterface file Aug 13, 2022
@flockoffiles
Copy link
Author

I have discovered that if I simply comment out the typealias defined in the UnorderedView
OrderedSet+UnorderedView.swift, line 17

and then fix the line where it is used:

OrderedSet+UnorderedView.swift, line 305

Then the resulting .swiftinterface file compiles again.

Note: I tried it with Xcode 13.3 and 14 beta 5.
This seems like a compiler limitation to me.

@flockoffiles
Copy link
Author

I have created FB11262423 against the Swift compiler.
Below I am attaching an example project where the problem is isolated.
XCFrameworkBug.zip

@casademora
Copy link

casademora commented Aug 16, 2022

in my tests, it seems that commenting out the @inline(__always) directive on some of the initializers in the OrderedSet+UnorderedView.swift file helps me to create a usable archive for my package.

@lorentey
Copy link
Member

This is one bizarre issue -- something smells fishy in the name lookup department. It's evidently triggered by the fully qualified names in the .swiftinterface file.

// Original:
extension OrderedSet {
  @frozen
  public struct UnorderedView {
    public typealias Element = OrderedSet.Element
    ...
  }
}

// Generated .swiftinterface:
extension OrderedCollections.OrderedSet {
  @frozen public struct UnorderedView {
    public typealias Element = OrderedCollections.OrderedSet<Element>.Element
    // 🚫 error: Type alias 'Element' references itself
    ...
  }
}

The name lookup rules are quite wacky, but I think the issue is that the Element in OrderedSet<Element> resolves to the typealias being defined, rather than the Element received from the surrounding context.

#162 works around this problem by moving the typealias declaration into an extension. This results in the following code in the .swiftinterface:

extension OrderedCollections.OrderedSet.UnorderedView : Swift.SetAlgebra {
  public typealias Element = OrderedCollections.OrderedSet<Element>.Element
}

(Don't ask me why/how this is different -- let's see where the compiler bug leads us. 🙈)

@lorentey
Copy link
Member

(Obligatory note: this package does not promise any level of ABI stability -- all modules importing xcframeworks generated from this code must be rebuilt whenever the xcframework is updated.

This package was not designed to be built with library evolution enabled, and it is not optimized for that dialect of Swift. However, such builds still ought to work, and these bug reports are very welcome!)

lorentey added a commit to lorentey/swift-collections that referenced this issue Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants