Skip to content

Commit 69fa54d

Browse files
committed
Suppress compiler warnings about Errors with non-Sendable members.
A type conforming to `Error` must be `Sendable`, but `DelimiterLexError` and `CompilerLexError` carry source locations represented by `UnsafeRawPointer`, which is inherently non-`Sendable`. The compiler diagnoses the failure to meet `Sendable` requirements on these error types, which will prevent Swift 6 adoption in `_RegexParser` and also generates build log spam every time the compiler and standard library are built. There are a couple ways to resolve this problem: 1. Refactor so that the errors to not need to carry source locations. 2. Refactor to avoid using `Error` (the code using these errors would not require `Sendable` if it were not for `Error`'s inherent `Sendable` requirement). 3. Suppress the warnings with a `@uncheck Sendable` wrapper. Option 1 probably requires significant refactoring and option 2 seems undesirable since it means that the implementation must forgo the convenience of Swift's error handling model. Therefore this PR implements option 3 as the most expedient way to stop the diagnostic spam, leaving improvement on the status quo as a future exercise.
1 parent dc95688 commit 69fa54d

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Sources/_RegexParser/Regex/Parse/CompilerInterface.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ public let currentRegexLiteralFormatVersion = 1
1717

1818
@_spi(CompilerInterface)
1919
public struct CompilerLexError: Error {
20+
var underlyingLocation: UnsafeSourceLocation
21+
2022
public var message: String
21-
public var location: UnsafeRawPointer
23+
public var location: UnsafeRawPointer { return underlyingLocation.ptr }
2224
public var completelyErroneous: Bool
25+
26+
init(
27+
message: String, location: UnsafeRawPointer, completelyErroneous: Bool
28+
) {
29+
self.message = message
30+
self.underlyingLocation = UnsafeSourceLocation(location)
31+
self.completelyErroneous = completelyErroneous
32+
}
2333
}
2434

2535
/// Interface for the Swift compiler.

Sources/_RegexParser/Regex/Parse/DelimiterLexing.swift

+18-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ extension Delimiter {
7070
}
7171
}
7272

73+
/// A wrapper for an `UnsafeRawPointer` representing a source location, marked
74+
/// `@unchecked Sendable` to help errors carrying such information satisfy their
75+
/// `Sendable` requirement.
76+
///
77+
/// TODO: consider further refactoring to avoid needing to lie to the compiler
78+
/// about Sendability.
79+
struct UnsafeSourceLocation: @unchecked Sendable {
80+
let ptr: UnsafeRawPointer
81+
82+
init(_ ptr: UnsafeRawPointer) {
83+
self.ptr = ptr
84+
}
85+
}
86+
7387
public struct DelimiterLexError: Error, CustomStringConvertible {
7488
public enum Kind: Hashable {
7589
case unterminated
@@ -81,12 +95,14 @@ public struct DelimiterLexError: Error, CustomStringConvertible {
8195

8296
public var kind: Kind
8397

98+
var resumeLocation: UnsafeSourceLocation
99+
84100
/// The pointer at which to resume lexing.
85-
public var resumePtr: UnsafeRawPointer
101+
public var resumePtr: UnsafeRawPointer { resumeLocation.ptr }
86102

87103
init(_ kind: Kind, resumeAt resumePtr: UnsafeRawPointer) {
88104
self.kind = kind
89-
self.resumePtr = resumePtr
105+
self.resumeLocation = UnsafeSourceLocation(resumePtr)
90106
}
91107

92108
public var description: String {

0 commit comments

Comments
 (0)