Warn on literal patterns found anywhere in a constructor's arguments.#2133
Warn on literal patterns found anywhere in a constructor's arguments.#2133gasche merged 5 commits intoocaml:trunkfrom
Conversation
c641d1d to
e86a150
Compare
gasche
left a comment
There was a problem hiding this comment.
Thanks for working on this!
I think that the behavior with the PR is better than without. The design is still not fully satisfying however: having the attribute on the constructor (rather than on a parameter or possibly sub-types of the parameter) forces a very coarse-grained behavior, with some risk of users being unable to express their intent on which part they would like to avoid warnings about.
Could you maybe add a couple examples test-cases that did not warn before and now correctly warn with the PR?
e86a150 to
3d18694
Compare
gasche
left a comment
There was a problem hiding this comment.
I approve of the PR, but:
-
you must add a Changes entry (and possibly check in the description of the warning in the manual whether it needs an update), and
-
I think that the testsuite file that you touched would be much nicer with expect-style test, it would be nice if you updated it to this style. (I think it's a general good-hygiene principle to update each file we touch to gradually migrate.)
Done (5f14aa1).
Also done (894df10). The existing text is mostly okay, but I added a short paragraph to make it clear that the warning applies where the pattern is not a literal itself, but contains literals as sub-patterns.
Also done (5521787). |
|
Thanks! This is good to go if/when CI passes. If I were to forget to come check it again, please (anyone) feel free to ping or merge. |
|
Thanks, @gasche. CI is passing. |
The attribute
@ocaml.warn_on_literal_patternenables a warning for code that uses a literal pattern to match a constructor's arguments.Predefined exceptions, such as
Failure, are marked with the attribute to dissuade people writing exception-handling code from relying on the value of the exception's arguments:However, the warning is currently arbitrarily restricted to constructors of a single non-tuple argument, and so no warning is emitted for more complex patterns:
This PR extends the definition of 'literal pattern' so that warnings are emitted in these cases, too.
#254 has some previous discussion of the attribute/warning, and the possibility of extending them beyond a single constructor. @gasche's comment is particularly relevant: