-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Enforce Sendable constraints of Regex passed to ValidationRule #44
Comments
Thank you for writing this up @Supereg! I agree that just rejecting every element that might not be sendable is a clear limitation that would be great to be avoided. I think the clearer way forward would be to clearly document that a Regex should be sendable (even when using transformers) and assert this in the documentation. When we encounter a regex that might not to be sendable it might make sense to emit a runtime warning in the console pointing the developer to ensure that the regex is actually sendable. An other alternative would be to add a more sophisticated check that would test this for, e.g., |
I agree, that rejecting every element that might not be sendable is way too strict and defeats the purpose of having such a nice, type-safe Regex API. SF-0004 had the additional challenge that they needed Regex to be Just to provide full context what the issue with let doubleValueRegex = Regex {
"$"
Capture {
OneOrMore(.digit)
"."
Repeat(.digit, count: 2)
} transform: { Double($0)! }
Anchor.endOfLine
}
for match in transactions.matches(of: doubleValueRegex) {
if match.1 >= 100.0 {
print("Large amount: \(match.1)")
}
} Another possibility would be to delay the creation of the Regex to have it created in the isolation context the ValidationRule is used. E.g., an escaping, sendable auto-closure or even an escaping |
I don't think there is a good way for checking that. A race condition doesn't necessarily lead to a crash. They typically just break the data integrity (e.g., two threads incrementing the same mutable state). |
Thanks for the context @Supereg! Given the input above I would suggest that we allow non-sendable regex even if it might contain unsafe code. It should be something that we mention in the documentation; add a warning, and maybe even see if we can push for this on a Swift-level in the long run, but I see your point that the ship might have already sailed on this. Agree that some automated checks and even a delay might not be worth the tradeoff here if we generally expect the these transformers will be very isolated. |
Sounds good. I would consider this issue to be complete if we add elaborate documentation around the topic. Another solution could be to explore the |
Problem
Regex
is currently not sendable. We currently just suppress the compiler warning that is generated when creating aValidationRule
from aRegex
instance as Regex is most likely sendable even when used with transformers (transformers are basically mapping functions, e.g., mapping a String input to a type by calling the rawValue initializer). However, these operations are not required to be Sendable and Apple is seemingly missing the last point where they can change Sendable constraints of transformers without inducing breaking changes. Therefore, we will need to think how we want to treat Sendability of Regex in the future.Solution
One solution could be to just accept a subset of valid Regex expressions. Making the initializer failable. Swift Foundation used a similar approach when they introduced Regex support for the
#Predicate
macro. We cite from SF-0004:What they decided for was to just not support any Regex that might contain concurrency-unsafe code. So any Regex that contain transform closures even if they would be technically
@Sendable
and concurrency safe, greatly limiting the usefulness of the RegexBuilder.Additional context
We could generally investigate if we would want to support accepting
Predicate
instances as an input to aValidationRule
. We could then use the Regex support for the#Predicate
macro to automatically convert regex to a Predicate instance.Note:
wholeMatch
is not available as of right now: see here. The regex support forPredicate
only supports callingcontains
which would require to specify start and end anchors.What do you think @PSchmiedmayer
Code of Conduct
The text was updated successfully, but these errors were encountered: