From dcb2b3e7e0b33e2584edfed19c3ba5c9a8b19a42 Mon Sep 17 00:00:00 2001 From: Mustii <26250654+mustiikhalil@users.noreply.github.com> Date: Thu, 10 Jul 2025 22:20:34 +0200 Subject: [PATCH] Fixes module exports and inlining The following moves away from @_exported since it isnt finalized yet, and is strongly discouraged. Fixes a couple of @inline(__always) calls within the new flexbuffers library, and instead attributing them with @usableFromInline --- .../Sources/Model/greeter_generated.swift | 4 ++ samples/monster_generated.swift | 4 ++ src/idl_gen_swift.cpp | 10 ++-- swift/Sources/FlatBuffers/Constants.swift | 2 +- swift/Sources/FlatBuffers/Enum.swift | 3 ++ .../FlatBuffers/FlatBufferBuilder.swift | 3 ++ swift/Sources/FlatBuffers/Mutable.swift | 3 ++ swift/Sources/FlatBuffers/Struct.swift | 3 ++ swift/Sources/FlatBuffers/Table.swift | 3 ++ swift/Sources/FlatBuffers/Verifiable.swift | 4 ++ .../FlatBuffers/_InternalByteBuffer.swift | 4 ++ .../Sources/FlexBuffers/Utils/Constants.swift | 2 +- swift/Sources/FlexBuffers/Utils/Value.swift | 3 ++ .../Sources/FlexBuffers/Utils/functions.swift | 3 ++ .../Writer/FlexBuffersWriter.swift | 51 +++++++++---------- .../FlexBuffers/_InternalByteBuffer.swift | 6 +++ .../Flatbuffers/MutatingBool_generated.swift | 4 ++ .../Flatbuffers/monster_test_generated.swift | 4 ++ .../Flatbuffers/more_defaults_generated.swift | 4 ++ .../Flatbuffers/nan_inf_test_generated.swift | 4 ++ .../optional_scalars_generated.swift | 4 ++ .../Flatbuffers/union_vector_generated.swift | 4 ++ .../vector_has_test_generated.swift | 4 ++ .../monster_test_generated.swift | 4 ++ .../test_import_generated.swift | 4 ++ 25 files changed, 110 insertions(+), 34 deletions(-) diff --git a/grpc/examples/swift/Greeter/Sources/Model/greeter_generated.swift b/grpc/examples/swift/Greeter/Sources/Model/greeter_generated.swift index 5339272510d..77871696ec9 100644 --- a/grpc/examples/swift/Greeter/Sources/Model/greeter_generated.swift +++ b/grpc/examples/swift/Greeter/Sources/Model/greeter_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers public struct models_HelloReply: FlatBufferObject, Verifiable { diff --git a/samples/monster_generated.swift b/samples/monster_generated.swift index 53ecd8e3c2c..bab8157d155 100644 --- a/samples/monster_generated.swift +++ b/samples/monster_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers public enum MyGame_Sample_Color: Int8, Enum, Verifiable { diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp index b73333b43ae..b061b30564e 100644 --- a/src/idl_gen_swift.cpp +++ b/src/idl_gen_swift.cpp @@ -174,10 +174,12 @@ class SwiftGenerator : public BaseGenerator { code_ += "// swiftlint:disable all"; code_ += "// swiftformat:disable all\n"; if (parser_.opts.include_dependence_headers || parser_.opts.generate_all) { - if (parser_.opts.swift_implementation_only) - code_ += "@_implementationOnly \\"; - - code_ += "import FlatBuffers\n"; + code_.SetValue("IMPLEMENTONLY", parser_.opts.swift_implementation_only ? "@_implementationOnly " : ""); + code_ += "#if canImport(Common)"; + code_ += "{{IMPLEMENTONLY}}import Common"; + code_ += "#endif"; + code_ += ""; + code_ += "{{IMPLEMENTONLY}}import FlatBuffers\n"; } // Generate code for all the enum declarations. diff --git a/swift/Sources/FlatBuffers/Constants.swift b/swift/Sources/FlatBuffers/Constants.swift index 14bbb9f0991..44b48093623 100644 --- a/swift/Sources/FlatBuffers/Constants.swift +++ b/swift/Sources/FlatBuffers/Constants.swift @@ -15,7 +15,7 @@ */ #if canImport(Common) -@_exported import Common +import Common #endif import Foundation diff --git a/swift/Sources/FlatBuffers/Enum.swift b/swift/Sources/FlatBuffers/Enum.swift index 29b382247a6..f260f8f06b9 100644 --- a/swift/Sources/FlatBuffers/Enum.swift +++ b/swift/Sources/FlatBuffers/Enum.swift @@ -13,6 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#if canImport(Common) +import Common +#endif import Foundation diff --git a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift index 6d6d92b573d..b2838aa9e03 100644 --- a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift +++ b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift @@ -13,6 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#if canImport(Common) +import Common +#endif import Foundation diff --git a/swift/Sources/FlatBuffers/Mutable.swift b/swift/Sources/FlatBuffers/Mutable.swift index 307e9a927c3..92f48b48f18 100644 --- a/swift/Sources/FlatBuffers/Mutable.swift +++ b/swift/Sources/FlatBuffers/Mutable.swift @@ -13,6 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#if canImport(Common) +import Common +#endif import Foundation diff --git a/swift/Sources/FlatBuffers/Struct.swift b/swift/Sources/FlatBuffers/Struct.swift index bbce8f978c7..151b6a199a2 100644 --- a/swift/Sources/FlatBuffers/Struct.swift +++ b/swift/Sources/FlatBuffers/Struct.swift @@ -13,6 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#if canImport(Common) +import Common +#endif import Foundation diff --git a/swift/Sources/FlatBuffers/Table.swift b/swift/Sources/FlatBuffers/Table.swift index b3be4d1737e..e1b6722b6ff 100644 --- a/swift/Sources/FlatBuffers/Table.swift +++ b/swift/Sources/FlatBuffers/Table.swift @@ -13,6 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#if canImport(Common) +import Common +#endif import Foundation diff --git a/swift/Sources/FlatBuffers/Verifiable.swift b/swift/Sources/FlatBuffers/Verifiable.swift index 20e3d4d39ef..4343594f17d 100644 --- a/swift/Sources/FlatBuffers/Verifiable.swift +++ b/swift/Sources/FlatBuffers/Verifiable.swift @@ -14,6 +14,10 @@ * limitations under the License. */ +#if canImport(Common) +import Common +#endif + import Foundation /// Verifiable is a protocol all swift flatbuffers object should conform to, diff --git a/swift/Sources/FlatBuffers/_InternalByteBuffer.swift b/swift/Sources/FlatBuffers/_InternalByteBuffer.swift index c6e3b3f85dc..87816c047ca 100644 --- a/swift/Sources/FlatBuffers/_InternalByteBuffer.swift +++ b/swift/Sources/FlatBuffers/_InternalByteBuffer.swift @@ -14,6 +14,10 @@ * limitations under the License. */ +#if canImport(Common) +import Common +#endif + import Foundation /// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object diff --git a/swift/Sources/FlexBuffers/Utils/Constants.swift b/swift/Sources/FlexBuffers/Utils/Constants.swift index 884a9cc9f3a..8b40f2d7b2a 100644 --- a/swift/Sources/FlexBuffers/Utils/Constants.swift +++ b/swift/Sources/FlexBuffers/Utils/Constants.swift @@ -15,7 +15,7 @@ */ #if canImport(Common) -@_exported import Common +import Common #endif import Foundation diff --git a/swift/Sources/FlexBuffers/Utils/Value.swift b/swift/Sources/FlexBuffers/Utils/Value.swift index 4c11405901e..65cc61f8ca1 100644 --- a/swift/Sources/FlexBuffers/Utils/Value.swift +++ b/swift/Sources/FlexBuffers/Utils/Value.swift @@ -13,6 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#if canImport(Common) +import Common +#endif import Foundation diff --git a/swift/Sources/FlexBuffers/Utils/functions.swift b/swift/Sources/FlexBuffers/Utils/functions.swift index 972964e2f5c..7aeda39ec03 100644 --- a/swift/Sources/FlexBuffers/Utils/functions.swift +++ b/swift/Sources/FlexBuffers/Utils/functions.swift @@ -13,6 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#if canImport(Common) +import Common +#endif import Foundation diff --git a/swift/Sources/FlexBuffers/Writer/FlexBuffersWriter.swift b/swift/Sources/FlexBuffers/Writer/FlexBuffersWriter.swift index e6ac3ce2a27..68108578f93 100644 --- a/swift/Sources/FlexBuffers/Writer/FlexBuffersWriter.swift +++ b/swift/Sources/FlexBuffers/Writer/FlexBuffersWriter.swift @@ -13,6 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#if canImport(Common) +import Common +#endif import Foundation @@ -80,7 +83,6 @@ public struct FlexBuffersWriter { } // MARK: - Storing root - @inline(__always) public mutating func finish() { assert(stack.count == 1) @@ -115,7 +117,6 @@ public struct FlexBuffersWriter { } @discardableResult - @inline(__always) public mutating func endVector( start: Int, typed: Bool = false, @@ -181,7 +182,6 @@ public struct FlexBuffersWriter { } @discardableResult - @inline(__always) public mutating func endMap(start: Int) -> UInt64 { let len = sortMapByKeys(start: start) @@ -507,8 +507,8 @@ public struct FlexBuffersWriter { // MARK: Writing to buffer - @inline(__always) - private mutating func write(value: Value, byteWidth: Int) { + @usableFromInline + mutating func write(value: Value, byteWidth: Int) { switch value.type { case .null, .int: write(value: value.i, byteWidth: byteWidth) case .bool, .uint: write(value: value.u, byteWidth: byteWidth) @@ -518,8 +518,8 @@ public struct FlexBuffersWriter { } } - @inline(__always) - private mutating func pushIndirect( + @usableFromInline + mutating func pushIndirect( value: T, type: FlexBufferType, bitWidth: BitWidth) @@ -542,8 +542,8 @@ public struct FlexBuffersWriter { /// - Parameter str: String that will be added to the buffer /// - Parameter len: length of the string @discardableResult - @inline(__always) - private mutating func write(str: borrowing String, len: Int) -> UInt { + @usableFromInline + mutating func write(str: borrowing String, len: Int) -> UInt { let resetTo = writerIndex var sloc = str.withCString { storeBlob(pointer: $0, len: len, trailing: 1, type: .string) @@ -573,8 +573,8 @@ public struct FlexBuffersWriter { } @discardableResult - @inline(__always) - private mutating func add(key: borrowing String, len: Int) -> UInt { + @usableFromInline + mutating func add(key: borrowing String, len: Int) -> UInt { _bb.ensureSpace(size: len) var sloc: UInt = numericCast(writerIndex) @@ -596,8 +596,8 @@ public struct FlexBuffersWriter { } // MARK: - Storing Blobs - @inline(__always) - private mutating func storeBlob( + @usableFromInline + mutating func storeBlob( _ bytes: T, len: Int, type: FlexBufferType) -> UInt where T: ContiguousBytes @@ -609,7 +609,6 @@ public struct FlexBuffersWriter { @discardableResult @usableFromInline - @inline(__always) mutating func storeBlob( pointer: borrowing UnsafeRawPointer, len: Int, @@ -636,8 +635,8 @@ public struct FlexBuffersWriter { // MARK: Write Vectors @discardableResult - @inline(__always) - private mutating func create(vector: [T], fixed: Bool) -> Int + @usableFromInline + mutating func create(vector: [T], fixed: Bool) -> Int where T: Scalar { let length: UInt64 = numericCast(vector.count) @@ -668,8 +667,8 @@ public struct FlexBuffersWriter { return vloc } - @inline(__always) - private mutating func createVector( + @usableFromInline + mutating func createVector( start: Int, count: Int, step: Int, @@ -800,8 +799,8 @@ public struct FlexBuffersWriter { return bytes } - @inline(__always) - private mutating func sortMapByKeys(start: Int) -> Int { + @usableFromInline + mutating func sortMapByKeys(start: Int) -> Int { let len = mapElementCount(start: start) for index in stride(from: start, to: stack.count, by: 2) { assert(stack[index].type == .key) @@ -838,10 +837,9 @@ public struct FlexBuffersWriter { // MARK: - Vectors helper functions extension FlexBuffersWriter { @discardableResult - @inline(__always) public mutating func vector( key: String, - _ closure: @escaping FlexBuffersWriterBuilder) -> UInt64 + _ closure: FlexBuffersWriterBuilder) -> UInt64 { let start = startVector(key: key) closure(&self) @@ -849,8 +847,7 @@ extension FlexBuffersWriter { } @discardableResult - @inline(__always) - public mutating func vector(_ closure: @escaping FlexBuffersWriterBuilder) + public mutating func vector(_ closure: FlexBuffersWriterBuilder) -> UInt64 { let start = startVector() @@ -862,10 +859,9 @@ extension FlexBuffersWriter { // MARK: - Maps helper functions extension FlexBuffersWriter { @discardableResult - @inline(__always) public mutating func map( key: String, - _ closure: @escaping FlexBuffersWriterBuilder) -> UInt64 + _ closure: FlexBuffersWriterBuilder) -> UInt64 { let start = startMap(key: key) closure(&self) @@ -873,8 +869,7 @@ extension FlexBuffersWriter { } @discardableResult - @inline(__always) - public mutating func map(_ closure: @escaping FlexBuffersWriterBuilder) + public mutating func map(_ closure: FlexBuffersWriterBuilder) -> UInt64 { let start = startMap() diff --git a/swift/Sources/FlexBuffers/_InternalByteBuffer.swift b/swift/Sources/FlexBuffers/_InternalByteBuffer.swift index 4f87525837e..c78a3a78e63 100644 --- a/swift/Sources/FlexBuffers/_InternalByteBuffer.swift +++ b/swift/Sources/FlexBuffers/_InternalByteBuffer.swift @@ -14,6 +14,10 @@ * limitations under the License. */ +#if canImport(Common) +import Common +#endif + import Foundation /// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object @@ -152,6 +156,7 @@ struct _InternalByteBuffer { ensureSpace(size: writerIndex) } + @inline(__always) mutating func writeBytes(_ ptr: UnsafeRawPointer, len: Int) { memcpy( _storage.memory.advanced(by: writerIndex), @@ -160,6 +165,7 @@ struct _InternalByteBuffer { writerIndex = writerIndex &+ len } + @inline(__always) mutating func write(_ v: T, len: Int) { withUnsafePointer(to: v) { memcpy( diff --git a/tests/swift/Tests/Flatbuffers/MutatingBool_generated.swift b/tests/swift/Tests/Flatbuffers/MutatingBool_generated.swift index 1fa367092f6..c8a27407978 100644 --- a/tests/swift/Tests/Flatbuffers/MutatingBool_generated.swift +++ b/tests/swift/Tests/Flatbuffers/MutatingBool_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers public struct Property: NativeStruct, Verifiable, FlatbuffersInitializable, NativeObject { diff --git a/tests/swift/Tests/Flatbuffers/monster_test_generated.swift b/tests/swift/Tests/Flatbuffers/monster_test_generated.swift index 94ecac1475b..a97f5f8c292 100644 --- a/tests/swift/Tests/Flatbuffers/monster_test_generated.swift +++ b/tests/swift/Tests/Flatbuffers/monster_test_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers /// Composite components of Monster color. diff --git a/tests/swift/Tests/Flatbuffers/more_defaults_generated.swift b/tests/swift/Tests/Flatbuffers/more_defaults_generated.swift index 8dc1ea8ffa2..91bcc28d5bd 100644 --- a/tests/swift/Tests/Flatbuffers/more_defaults_generated.swift +++ b/tests/swift/Tests/Flatbuffers/more_defaults_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers public enum ABC: Int32, Enum, Verifiable { diff --git a/tests/swift/Tests/Flatbuffers/nan_inf_test_generated.swift b/tests/swift/Tests/Flatbuffers/nan_inf_test_generated.swift index 1efa5160bd3..d5def4c8710 100644 --- a/tests/swift/Tests/Flatbuffers/nan_inf_test_generated.swift +++ b/tests/swift/Tests/Flatbuffers/nan_inf_test_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers public struct Swift_Tests_NanInfTable: FlatBufferObject, Verifiable { diff --git a/tests/swift/Tests/Flatbuffers/optional_scalars_generated.swift b/tests/swift/Tests/Flatbuffers/optional_scalars_generated.swift index d413e5340f9..2efcf0422f4 100644 --- a/tests/swift/Tests/Flatbuffers/optional_scalars_generated.swift +++ b/tests/swift/Tests/Flatbuffers/optional_scalars_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers public enum optional_scalars_OptionalByte: Int8, Enum, Verifiable { diff --git a/tests/swift/Tests/Flatbuffers/union_vector_generated.swift b/tests/swift/Tests/Flatbuffers/union_vector_generated.swift index 6574c665213..0f56a3044c8 100644 --- a/tests/swift/Tests/Flatbuffers/union_vector_generated.swift +++ b/tests/swift/Tests/Flatbuffers/union_vector_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers public enum Character: UInt8, UnionEnum { diff --git a/tests/swift/Tests/Flatbuffers/vector_has_test_generated.swift b/tests/swift/Tests/Flatbuffers/vector_has_test_generated.swift index 30601adb68e..9d3fdf515b5 100644 --- a/tests/swift/Tests/Flatbuffers/vector_has_test_generated.swift +++ b/tests/swift/Tests/Flatbuffers/vector_has_test_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers public struct Swift_Tests_Vectors: FlatBufferObject, Verifiable { diff --git a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift index 94ecac1475b..a97f5f8c292 100644 --- a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift +++ b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +import Common +#endif + import FlatBuffers /// Composite components of Monster color. diff --git a/tests/swift/fuzzer/CodeGenerationTests/test_import_generated.swift b/tests/swift/fuzzer/CodeGenerationTests/test_import_generated.swift index 18faaa6c825..ac5df9d72b8 100644 --- a/tests/swift/fuzzer/CodeGenerationTests/test_import_generated.swift +++ b/tests/swift/fuzzer/CodeGenerationTests/test_import_generated.swift @@ -2,6 +2,10 @@ // swiftlint:disable all // swiftformat:disable all +#if canImport(Common) +@_implementationOnly import Common +#endif + @_implementationOnly import FlatBuffers internal struct Message: FlatBufferObject, Verifiable, ObjectAPIPacker {