-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Statics shadow local variables causing "refutable pattern error", and non-obvious bugs. #7526
Comments
visiting for triage, email from 2013 sep 30. still relevant. The lint was turned to |
(maybe there should be a separate lint for this match pattern issue, ((my initial idea above was dumb: Its doubtlessly by design that statics can appear as constants in patterns. The issue is that they are easily mistaken for binding identifiers. So the lint could instead warn about lowercase static constants that are occurring as pattern bindings; its again a refinement of the aforementioned non-uppercase-statics lint, but one that we can probably stand behind turning on by default.)) |
(I think adding such a lint for non-uppercase statics in match arms may not be so hard. Giving it a shot now.) |
@pnkfelix checking function/closure arguments and let variables would be good, since they're pattern contexts too. (Although they "just" give weird error messages about refutable patterns, not silently changing program behaviour, so it isn't as important.) |
@huonw yeah I'm more concerned about the bug you outlined in the description. From what I saw in the code, its not necessarily "natural" to put this check into a place that would cover all of the cases you listed. (oops, hit close-and-comment by accident.) |
cc #3070 |
(and likewise, it would be good if the new lint, or another new one, also warned upon encountering all-uppercase identifier patterns that are not references to a static, but instead are binding patterns, since that is probably a sign of bug such as an alpha-renaming gone wrong.) |
I understand now that it's a bit more of a problem, but I still think that by-default all-caps statics may not be the best solution. I agree that a smarter lint mode would be a good candidate for warn-by-default. |
This tries to warn about code like: ```rust match (0,0) { (0, aha) => { ... }, ... } ``` where `aha` is actually a static constant, not a binding.
…nuc-statics-in-match-patterns, r=alexcrichton r? anyone Address scariest part of #7526 by adding a new more specific lint (that is set to warn by default, rather than allow).
This is pretty close to being closeable now that PR #9638 has landed. The main thing left would be to improve the error message for the "refutable pattern error" (sic) to point at the span for the static that is causing the rebinding attempt to fail. (I wrote "(sic)" above because I'm pretty sure we should be saying that only irrefutable patterns are allowed in e.g. |
(also, I think my interpretation of "refutable" and "irrefutable" is consistent with the documentation in rust.md. So I think I'll probably change the text here to say "irrefutable" in the error message.) |
There's also the issue with shadowing even when the name shouldn't be visible. For example this case:
Compiler fails like this:
|
I hit a variant of this today:
This gives the following error:
Which is a very confusing error message. |
triage: same irreffutable pattern error, which is understandable but sad. |
I'm going to add the 'lint' tag in case this is something we want to lint, but it may just need a better error. |
Triage: no longer happens for statics. Still happens for consts. |
A particularly insidious example is:
since one could easily imagine not noticing that the first arm doesn't run, and/or spending hours debugging it.
(#7523 added an lint enforcing statics as uppercase,
warn
'd by default, which makes this less likely to occur. There was some discussion about turning this toallow
by default if/when the error messages are improved. Update 2013-7-22: has been turned toallow
but there is no improvement in error messages.)The text was updated successfully, but these errors were encountered: