Skip to content

[Syntax] Workaround for 'cannot bypass resilience' warnings #3020

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

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Sources/SwiftSyntax/Raw/RawSyntaxArena.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public class RawSyntaxArena {
///
/// - Important: This is only intended to be used for assertions to catch
/// retain cycles in syntax arenas.
fileprivate let hasParent: UnsafeMutablePointer<AtomicBool>
/// - Note: `UnsafeMutableRawPointer` + casting accessor is a workaround to silence the warning 'cannot bypass resilience'.
private let _hasParent: UnsafeMutableRawPointer
fileprivate func hasParent() -> UnsafeMutablePointer<AtomicBool> {
_hasParent.assumingMemoryBound(to: AtomicBool.self)
}
#endif

/// Construct a new ``RawSyntaxArena`` in which syntax nodes can be allocated.
Expand All @@ -75,7 +79,7 @@ public class RawSyntaxArena {
self.allocator = BumpPtrAllocator(initialSlabSize: slabSize)
self.childRefs = []
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
self.hasParent = swiftsyntax_atomic_bool_create(false)
self._hasParent = UnsafeMutableRawPointer(swiftsyntax_atomic_bool_create(false))
#endif
}

Expand All @@ -84,7 +88,7 @@ public class RawSyntaxArena {
child.release()
}
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
swiftsyntax_atomic_bool_destroy(self.hasParent)
swiftsyntax_atomic_bool_destroy(self.hasParent())
#endif
}

Expand Down Expand Up @@ -153,7 +157,7 @@ public class RawSyntaxArena {

#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
precondition(
!swiftsyntax_atomic_bool_get(self.hasParent),
!swiftsyntax_atomic_bool_get(self.hasParent()),
"an arena can't have a new child once it's owned by other arenas"
)
#endif
Expand Down Expand Up @@ -299,12 +303,12 @@ struct RawSyntaxArenaRef: Hashable, @unchecked Sendable {
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
/// Accessor for the underlying's `RawSyntaxArena.hasParent`
var hasParent: Bool {
swiftsyntax_atomic_bool_get(value.hasParent)
swiftsyntax_atomic_bool_get(value.hasParent())
}

/// Sets the `RawSyntaxArena.hasParent` on the referenced arena.
func setHasParent(_ newValue: Bool) {
swiftsyntax_atomic_bool_set(value.hasParent, newValue)
swiftsyntax_atomic_bool_set(value.hasParent(), newValue)
}
#endif

Expand Down
13 changes: 8 additions & 5 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ struct SyntaxData: Sendable {
/// `SyntaxDataArena` manages the entire data of a `Syntax` tree.
final class SyntaxDataArena: @unchecked Sendable {
/// Mutex for locking the data when populating layout buffers.
private let mutex: PlatformMutex
///
/// - Note: `UnsafeMutableRawPointer` + casting accessor is a workaround to silence the warning 'cannot bypass resilience'.
private let _mutex: UnsafeMutableRawPointer?
private func mutex() -> PlatformMutex { PlatformMutex(opaque: self._mutex) }

/// Allocator.
private let allocator: BumpPtrAllocator
Expand All @@ -396,7 +399,7 @@ final class SyntaxDataArena: @unchecked Sendable {
init(raw: RawSyntax, rawNodeArena: RetainedRawSyntaxArena) {
precondition(rawNodeArena == raw.arenaReference)

self.mutex = PlatformMutex.create()
self._mutex = PlatformMutex.create().opaque
self.allocator = BumpPtrAllocator(initialSlabSize: Self.slabSize(for: raw))
self.rawArena = rawNodeArena
self.root = Self.createDataImpl(allocator: allocator, raw: raw, parent: nil, absoluteInfo: .forRoot(raw))
Expand All @@ -405,7 +408,7 @@ final class SyntaxDataArena: @unchecked Sendable {
deinit {
// Debug print for re-evaluating `slabSize(for:)`
// print("nodeCount: \(root.pointee.raw.totalNodes), slabSize: \(Self.slabSize(for: root.pointee.raw)), allocated: \(allocator.totalByteSizeAllocated), overflowed: \(Self.slabSize(for: root.pointee.raw) < allocator.totalByteSizeAllocated)")
self.mutex.destroy()
self.mutex().destroy()
}

/// Return the childen data of the given node.
Expand All @@ -431,8 +434,8 @@ final class SyntaxDataArena: @unchecked Sendable {
return SyntaxDataReferenceBuffer(UnsafeBufferPointer(start: baseAddress, count: childCount))
}

mutex.lock()
defer { mutex.unlock() }
mutex().lock()
defer { mutex().unlock() }

// Recheck, maybe some other thread has populated the buffer during acquiring the lock.
if let baseAddress = swiftsyntax_atomic_pointer_get(baseAddressRef)?.assumingMemoryBound(
Expand Down