-
Notifications
You must be signed in to change notification settings - Fork 256
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
Warning when constructor name is used as variable in match #1600
Comments
I agree. My issue with "redundant branch" wording is that it makes me think that I can clean something up because these branches "do the same thing". But as you point out, in this case Dafny means "I will never go into that branch". |
Oh, it looks like my recent issue #1789 is a duplicate of this one. |
I thought of another way we could attack this. The code sample should also produce a style warning something along the lines of:
That's a lot shorter than trying to explain the specific situation. |
A related but slightly different issue: #1991 Which gives the following example:
The bigger issue seems to be that the syntax for capturing a variable and matching a datatype constructor is too similar. The imagined warning:
Works for the case where the captured variable has the exact name of the constructor, but not for the case where a name that's not a constructor is captured as a variable, even though the programmer intended to match on a constructor. An alternative solution would be a flag "strictMatchCases" that gives a warning for any case where a datatype constructor is not followed by parenthesis.
I like that but maybe that would make most sense if the language also came with a standard code style that's enforced through compiler warnings and for which there's an automated formatter. |
…iable in match dafny-lang#1600. The code will produce a warning whenever a variable in a case expression starts with a capital letter.
Consider the following program, which contains a simple mistake:
For this program, Dafny issues a "redundant branch" warning for the second
case
. Why? Because the constructorShadesOfGray
takes a parameter, and since the patternShadesOfGray
does not list a parameter, Dafny interprets theShadesOfGray
in the first pattern as a variable (which will match anyc
, just like the variableanythingElse
in the second pattern would match anything that does not match in a priorcase
).It would be nice if Dafny issued a warning in this case. I can imagine
Such a warning could be emitted once all resolution/type inference/type checking has been done. The check would then traverse through all
case
patterns. If inside a pattern it finds a variableX
of typeT
, whereT
is a datatype that contains a constructor (with 1 or more parameters) namedX
, then the warning above is generated. Alternatively, this check could be implemented earlier, at the point where the Resolver today distinguishes between a nullary constructor (likeRed
) and a variable.The text was updated successfully, but these errors were encountered: