-
Notifications
You must be signed in to change notification settings - Fork 65
SyntaxError if 'case NAME:' is not the last case #148
Comments
I am also fine with this, and think it actually has merit to point out obvious mistakes. And to be fair: some syntax errors cannot be sensibly expressed by a context-free language, anyway. Take the rule that no two parameters in a function can share the same name, for instance. So, we do not necessarily have to put that into the grammar. |
I'm okay with this as well. People ignore warnings, and I cannot imagine this ever being "correct" (except perhaps in poorly-generated code). |
If we ever add an else clause, an irrefutable case must not be followed by any else clause. #147 |
Generally, I agree with this. But I would be careful with the wording. Irrefutable cases can actually take quite complicated forms, making it impossible for the compiler to decide whether they really are irrefutable or not. So, in short: the rule as it stands cannot be checked. Perhaps it would be enough to talk about the irrefutable case and define somewhere that it refers to the |
Hm, in my naïveté I think irrefutability is easily checked recursively.
That’s it. Presence of a guard makes a case non-irrefutable. What am I missing? |
I was primarily thinking of the last case you mention:
This is not necessarily the case semantically as in, e.g., The second objection to the list would be class patterns. In the context with OR, I would think of a silly example like Long story short: what we need is syntactic irrefutability (irrefutability that can be decided purely based on the syntax), which is naturally a subset of semantic irrefutability (everything that is 'logically' irrefutable). Obviously the compiler can only really check the former. Personally, I would not have a problem with defining what we mean by syntactic irrefutability and then state that any such case must be the last branch in a match statement (including the potential EDIT: We could also just say that the compiler will reject any case branch (again, including What speaks against this, of course, is that if the compiler gets better in detecting such cases we loose backward-compatibility. A program that runs with Python 3.10.0 might not run with 3.10.1 because the latter rejects an unreachable case branch. If pattern matching is used in libraries, this might lead to quite a mess. Hence not a good idea! |
I think it is worth it defining which patterns are syntactically irrefutable (basically my definition) and stating that any guard-less case whose pattern is syntactically irrefutable must not be followed by another Compilers that are smarter than that can issue SyntaxWarnings. Note that I am not worried that adding this rule will be considered as a problem (and if it is, we can offer only to declare |
Having an or-pattern where one in the left is irrefutable is a good candidate for errors too. For example in:
|
Agreed. |
Does this sound good, then? If so, I'll update the spec and implementation. |
Yup, still sounds good to me. Let's do it. |
See language in python/peps#1649 -- please review. |
(Landed.) |
Just got back from a trip. Sorry for the delayed review... but this looks good to me anyways! My one lingering question about patterns like |
Labeling as fully pepped -- this is now incorporated in PEP 634. (And also implemented, thanks Brandt!) |
There were some concerns over the typical footgun of writing
(with the intention that
FOO
andBAR
are global constants, not capture patterns).The CPython implementation issues a
SyntaxWarning
for this. Maybe we should just make that aSyntaxError
. (Even though it's hard to express even in the PEG grammar.) Mark Shannon also wants this.I am fine with this.
The text was updated successfully, but these errors were encountered: