-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Unexpected @_spi visibility for public function returning C++ type
#61472
Comments
|
Assigning to @zoecarver as he knows what the issue is and will work on fixing it with @beccadax . |
|
@zoecarver thinks this bug is the result of a bug related to the order modules are loaded. If I'm understanding correctly, because Clang modules are loaded later in the pipeline there is code that gets confused and thinks all of that API is |
|
Yep, pretty much! In more depth: Swift goes to lookup function Then, to produce a nice error, Swift does another lookup, this time ignoring the access specifiers. This time the clang modules have been loaded though, so it does find the function in the serialized module! The Swift compiler then says, "this is a public decl, I didn't find anything the first time, but did find something when ignoring access specifiers, so it must be SPI." That's why you get that error. [1] Normally, what would happen here, is Swift would see that it hasn't loaded the clang module that's be referenced and it would go load that module. However, namespaces are not owned by a module, so they go in the bridging header module (__ObjC). There's nothing to load there (that lookup table gets filled in as a side effect of loading other modules), so we fail to lookup the type. |
* There is/was a bug which unexpectedly threw an error when using C++ types in Swift, which caused the Swift compiler to declare the type as inaccessible due to '@_spi' protection level, even though nothing is marked with a '@_spi' protection level. * The issue for reference can be found on GitHub for @apple/swift: swiftlang/swift#61472 * It is marked as completed, however, it appears this is still a current bug, reproducable (only for some types?) when defining typealiases in Swift to CXX types which "hop" the namespace so-to-speak. * Example: public typealias SomeType = cxxnamespace.CxxType, attempting to use SomeType from a swift module that imports the module defining the typealias triggers "inaccessible due to '@_spi' protection level".
|
Sorry to add noise on a potentially fixed issue; however, I was able to reproduce this issue when attempting to "hop" a c++ namespace from a swift namespaced Note It is inconsistent and seemingly random, with which C++ type it would deem unexpectedly inaccessible from a given swift namespace. Though, pretty consistently it seemed to happen most frequently from a Example: // SwiftPMPackageA/Sources/ModuleA/ModuleA.swift
import SomeCxxLib
// a module defining this typealias
public typealias SomeCxxType = __a_verylong__cxxnamespace.SomeCxxType
// and this typealias, namespaced in swift by CxxTypes
public struct CxxTypes
{
public typealias CxxType = SomeCxxType
}// SwiftPMPackageB includes SwiftPMPackageA as a package dependency,
// and adds the SwiftPMPackageA.ModuleA target as a dependency of
// SwiftPMPackageB.ModuleB using the following:
// .product(name: "ModuleA", package: "SwiftPMPackageA")
// SwiftPMPackageB/Sources/ModuleB/ModuleB.swift
import ModuleA
// would cause SomeCxxType to produce error: 'SomeCxxType' is inaccessible due to '@_spi' protection level
SomeCxxType.doSomething()
// and would also cause CxxTypes to produce error: type 'CxxTypes' has no member 'CxxType'
CxxTypes.CxxType.doSomething()Swift version: I believe what @zoecarver said is spot on, and possibly it is the case that additional "hops" to search for this |
|
@egorzhdan @zoecarver could we reopen this for type alias issues stated above? Or would you prefer a new issue filed? |
|
I can confirm this bug is still prevalent when using type aliases in the latest release though I have not yet tried this with a prerelease version of swift. To reproduce this issue specifically, you should be able to do so when creating swift type aliases to a C++ namespace (swift enum) in particular, and the most crucial part is to then attempt to use this typealias from a SwiftPM package that depends on the package which declares said swift typealias to a C++ namespace, it is in this consuming package you should receive the following error: error: "MySwiftTypealiasToCXXNamespace" is inaccessible due to '@_spi' protection level.Oddly enough, in the example above the only symbol Swift does allow you to use without error is: __a_verylong__cxxnamespace.SomeCxxType.doSomething()
I know the Swift team is very busy, and I can additionally create an example project to demonstrate this bug if it can help in any way, thank you! |
Describe the bug
Commit demonstrating bug: https://github.com/GeorgeLyon/circt/tree/23831ec861f3874cbdc733c30404ce74b892a8a9
I have a C++ CMake target
circt/lib/Bindings/Swift/Bridgewhich add functionality not yet bridged by C++ interop (namely, callingnew MLIRContext()). This target uses the C++ typeMLIRContext, from a dependent module (MLIRIR) withimport_as_refandretain/release:immortalannotations. I also have a Swift CMake target (CIRCTSwift) atcirct/lib/Bindings/Swift/Modulewhich uses C++ interop to depend onBridge. Finally, I have a test targettest/Bindings/Swiftwhich importsCIRCTSwiftas a Swift library and attempts use its API.In
CIRCTSwift, there are two public functions:func fourtyTwo() -> Intandfunc createMLIRContext() -> mlir.MLIRContext, the main difference between them being thatmlir.MLIRContextis a C++ imported withimport_as_ref. Attempting to compile this project produces the following error when callingcreateMLIRContext, but no similar error is produced when only callingfourtyTwo:Steps To Reproduce
The easiest way to reproduce this behavior is to
circt-swift-bindings-testtarget...
Expected behavior
The API is callable
Screenshots
If applicable, add screenshots to help explain your problem.
Environment (please fill out the following information)
This uses Ubuntu 22.04 and the Swift 5.8 snapshot at https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2022-10-03-a/swift-DEVELOPMENT-SNAPSHOT-2022-10-03-a-ubuntu22.04.tar.gz
CMake settings can befound in
.decontainer/VSCode/settings.jsonThe text was updated successfully, but these errors were encountered: