swiftlang / swift-foundation

The Foundation project
Apache License 2.0
2.36k stars 150 forks source link

Decimal: different access level for fields and initializer on MacOS and Linux #934

Open blindspotbounty opened 22 hours ago

blindspotbounty commented 22 hours ago

According to documentation, there is an initializer for Decimal is exposed to public API in Foundation: https://developer.apple.com/documentation/foundation/decimal/1407961-init

I see that on MacOS I have access to the following fields and initializer:

public struct Decimal : @unchecked Sendable {

    public init()

    public init(_exponent: Int32, _length: UInt32, _isNegative: UInt32, _isCompact: UInt32, _reserved: UInt32, _mantissa: (UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16))

    public var _exponent: Int32

    public var _length: UInt32

    public var _isNegative: UInt32

    public var _isCompact: UInt32

    public var _reserved: UInt32

    public var _mantissa: (UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16)
}

and my test code compiles and runs:

extension Decimal {
    var details: String {
        return
            """
            Parts: 
            _exponent = \(self._exponent)
            _length = \(self._length)
            _isNegative = \(self._isNegative)
            _isCompact = \(self._isCompact)
            _reserved = \(self._reserved)
            _mantissa = \(self._mantissa)
            significand = \(self.significand)
            """
    }
}

On Linux I have the following error:

DecimalTest.swift:30:32: error: '_exponent' is inaccessible due to 'internal' protection level
 28 |             """
 29 |             Parts: 
 30 |             _exponent = \(self._exponent)
    |                                `- error: '_exponent' is inaccessible due to 'internal' protection level
 31 |             _length = \(self._length)
 32 |             _isNegative = \(self._isNegative)

The same if I use initializer:

extension Decimal: DetailsProviding {
    init() {
        self.init(_exponent: 0, _length: 0, _isNegative: 0, _isCompact: 0, _reserved: 0, _mantissa: (0, 0, 0, 0, 0, 0, 0, 0))
    }
}

error for Linux is the following:

DecimalTest.swift:26:18: error: extra arguments at positions #2, #3, #4, #5, #6 in call
 24 | extension Decimal: DetailsProviding {
 25 |     init() {
 26 |         self.init(_exponent: 0, _length: 0, _isNegative: 0, _isCompact: 0, _reserved: 0, _mantissa: (0, 0, 0, 0, 0, 0, 0, 0))
    |                  `- error: extra arguments at positions #2, #3, #4, #5, #6 in call
 27 |     }
 28 | 

I use Xcode 16.0 beta 6 on MacOS:

$ swift --version
swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx14.0

And swift 6.0 release on Linux:

Swift version 6.0 (swift-6.0-RELEASE)
Target: aarch64-unknown-linux-gnu