Improve diagnostic for @Flag Bool? without inversion:#892
Merged
Conversation
rauhul
approved these changes
Apr 21, 2026
Collaborator
rauhul
left a comment
There was a problem hiding this comment.
I want @natecook1000's eyes on this as it affects the public api, albeit with an unusable symbol
When a user declares `@Flag(...) var x: Bool?` without the required `inversion:` parameter, overload resolution falls through to the `Flag where Value == Int` initializer and the compiler reports "requires the types 'Bool?' and 'Int' be equivalent", which does not point the user at the real cause. Add an `@available(*, unavailable)` init overload on `Flag where Value == Bool?` with a message that names the missing parameter. Follows the same pattern used for the unavailable no-arg `Flag.init()` at the top of the file. Resolves apple#835.
f27a3d1 to
796f502
Compare
Member
natecook1000
left a comment
There was a problem hiding this comment.
Thanks, @qflen! LGTM with one minor change.
Co-authored-by: Nate Cook <natecook@apple.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #835.
Problem
When a user writes the following, which looks reasonable:
the compiler produces a confusing diagnostic:
The actual cause is that every
Flag where Value == Bool?init requires aninversion:parameter. Without one, overload resolution falls through to theFlag where Value == Intcounter initializer atFlag.swift:385, which is the firstinit(name:help:)the compiler can see, and theBool?vsIntmismatch surfaces from there.Fix
Add an
@available(*, unavailable)init overload onFlag where Value == Bool?whose signature matches the offending call shape. Overload resolution now picks this one because itswhereclause is more specific, and the custommessage:points the user at the real cause:This follows the same pattern used for the existing unavailable no-arg
Flag.init()atFlag.swift:94-101, whose doc comment already explains this technique:Test plan
swift testpasses (495 tests, no failures)main@Flag(help: "...") var x: Bool?@Flag(inversion: .prefixedEnableDisable) var x: Bool?still compiles and runs correctly