Hi all.
I continue to work on bringing about this vision for a stable @_implementationOnly via abstract type layouts, but I've reached a point where I'd like to discuss the direction.
Specifically, I want to discuss how much information we are willing to hide, potentially at the cost of diagnostics quality. Consider the following:
Library Module:
public class PublicClass {
public init() {}
internal func internalMethod() {}
}
Client Module:
import Library
func test(_ value: PublicClass) {
value.internalMethod()
}
Today, if you build Client against Library as normal, you'll get the following error:
/Users/nuriamari/local/git/swift-project/access-control-diagnostic-example/Client.swift:4:9: error: 'internalMethod' is inaccessible due to 'internal' protection level
2 |
3 | func test(_ value: PublicClass) {
4 | value.internalMethod()
| `- error: 'internalMethod' is inaccessible due to 'internal' protection level
5 | }
6 |
However, if we first produce a .swiftinterface file from Library, we get the following: (private API is dropped):
// swift-interface-format-version: 1.0
// swift-compiler-version: Swift version 6.5-dev (LLVM 270e638cf7ca80b, Swift 8b4449a3749e274)
// swift-module-flags: -module-name Library -language-mode 6 -enable-library-evolution
// swift-module-flags-ignorable: -formal-cxx-interoperability-mode=off -interface-compiler-version 6.5
import Swift
import _Concurrency
import _StringProcessing
import _SwiftConcurrencyShims
public class PublicClass {
public init()
@objc deinit
}
If we then convert this back into a .swiftmodule file and build Client again, we get the following less helpful error:
/Users/nuriamari/local/git/swift-project/access-control-diagnostic-example/Client.swift:4:9: error: value of type 'PublicClass' has no member 'internalMethod'
2 |
3 | func test(_ value: PublicClass) {
4 | value.internalMethod()
| `- error: value of type 'PublicClass' has no member 'internalMethod'
5 | }
6 |
Are we happy with the second degraded diagnostic? Personally I am, but I'd like to hear other's thoughts.
I would like to bring .swiftmodule files closer to .swiftinterface files via a change similar to @tshortli's experimental change, to exclude module internal declarations from .swiftmodule files: Frontend: Introduce `-experimental-serialize-external-decls-only` option by tshortli · Pull Request #68166 · swiftlang/swift · GitHub
This comes into play with @_implementationOnly types. Consider I have:
@_implementationOnly import HiddenModule
public struct PublicStruct {
internal func internalMethod(_ foo: HiddenModule.HiddenType) {}
}
Today clients of this module that don't have access to HiddenModule will drop internalMethod during de-serialization, as they can't resolve its entire signature, and attempts to call it will result in the degraded .swiftinterface style diagnostics above.
Are we ok with this? I suggest we should be, based on the precedent set by .swiftinterface files.
If we wish to preserve the same diagnostics, we could instead serialize unavailable stub declarations that carry only a name, and exist solely to differentiate between an API which does not exist, and one which is not accessible outside the module.