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
[SR-10118] Warn when OptionSet static constants are initialized with a zero value #52520
Comments
One problem is that there has to be a way to silence the warning. For the importer, that's providing an explicit |
There are a number of ways you could write zero that we could intentionally not detect, such as |
Ah, good idea with |
Comment by Miguel Salinas (JIRA) Just checking in. I got started with this after try! Swift SJ a couple weeks ago. Working slowly but steadily. One question I’ve had, however, is why the strange behavior isn’t changed instead of emitting a warning. |
The behavior is "correct" by the definition of OptionSet: any particular set contains all zero of the elements in the |
Comment by Miguel Salinas (JIRA) Ah ok![]( That makes sense. I misunderstood the behavior. I thought that the none set would return true for any set. e.g. MyOptions.none.contains( x ) would return true for all x. A simple test proved my assumption wrong. Thanks) |
Did you end up putting up a PR for this, Vercantez (JIRA User)? (I'm checking in on all the StarterBugs that haven't been touched in over a month; it's totally fine if you just haven't had time but still want to keep it.) |
Comment by Miguel Salinas (JIRA) Haven't put out a PR yet. Sorry for the late response. |
Comment by Miguel Salinas (JIRA) Almost done. I'll put out a PR pretty soon. |
Comment by Miguel Salinas (JIRA) PR: #27089 |
Comment by Miguel Salinas (JIRA) |
Comment by Robert (JIRA) Hello, why it doesn't work in case: (rawValue: 1 << 999999) ? |
Additional Detail from JIRA
md5: dd3960163b3287c2517c33c41718adda
Issue Description:
An OptionSet-conforming instance with a value of 0 behaves very strangely; for instance,
contains(_:)
will always return true for it. Because of this, and because[]
is the preferred syntax for an empty option set, ClangImporter removesnone
members when it imports anNS_OPTIONS
enum.However, if you write an OptionSet-conforming type in Swift, there's nothing stopping you from adding one yourself:
It would be nice if the compiler emitted a warning when you did this. The criteria would probably be something like:
A call to init(rawValue:)
Passing the literal 0
In an initial value expression (Initializer, in the compiler's parlance)
For a static let variable
In a type conforming to OptionSet
When the variable belongs to the type it's declared within
The text was updated successfully, but these errors were encountered: