]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44792: Improve syntax errors for if expressions (GH-27506) (GH-27565)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 2 Aug 2021 19:05:33 +0000 (12:05 -0700)
committerGitHub <noreply@github.com>
Mon, 2 Aug 2021 19:05:33 +0000 (20:05 +0100)
(cherry picked from commit 28b6dc9dd5d1ce6f8aff7e06d4ef9afdc2bc8332)

Co-authored-by: Miguel Brito <5544985+miguendes@users.noreply.github.com>
Grammar/python.gram
Lib/test/test_syntax.py
Misc/ACKS
Misc/NEWS.d/next/Core and Builtins/2021-07-31-12-12-57.bpo-44792.mOReTW.rst [new file with mode: 0644]
Parser/parser.c

index 057bcb641a6666c3e900520b1a3d6e991afa0023..3e7475a3fa6f4720d0baccb69e474196c20dd70d 100644 (file)
@@ -859,6 +859,7 @@ invalid_expression:
     # Soft keywords need to also be ignored because they can be parsed as NAME NAME
    | !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
         RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
+   | a=disjunction 'if' b=disjunction !'else' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
 
 invalid_named_expression:
     | a=expression ':=' expression {
index 6e1531e8b25fc38c011d520bd52570ebff6fc4e5..d5a52ba628a8169e619bfb3aeec38a65a3c48089 100644 (file)
@@ -140,6 +140,18 @@ SyntaxError: cannot assign to expression
 Traceback (most recent call last):
 SyntaxError: cannot assign to conditional expression
 
+>>> a = 42 if True
+Traceback (most recent call last):
+SyntaxError: expected 'else' after 'if' expression
+
+>>> a = (42 if True)
+Traceback (most recent call last):
+SyntaxError: expected 'else' after 'if' expression
+
+>>> a = [1, 42 if True, 4]
+Traceback (most recent call last):
+SyntaxError: expected 'else' after 'if' expression
+
 >>> True = True = 3
 Traceback (most recent call last):
 SyntaxError: cannot assign to True
index 385d7caf1775b5583dae2de03539b6eab56785d6..3f0506ecf758e5c9d3b9e913495361316135d5d7 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -227,6 +227,7 @@ Tom Bridgman
 Anthony Briggs
 Keith Briggs
 Tobias Brink
+Miguel Brito
 Dillon Brock
 Richard Brodie
 Michael Broghton
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-31-12-12-57.bpo-44792.mOReTW.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-31-12-12-57.bpo-44792.mOReTW.rst
new file mode 100644 (file)
index 0000000..2e9000d
--- /dev/null
@@ -0,0 +1 @@
+Improve syntax errors for if expressions. Patch by Miguel Brito
index c4a4eb651b3e78e327bbe047145bdd6348b2f0ee..b37e3d6855ce9c058bdcda8e643750d248479ecd 100644 (file)
@@ -18229,6 +18229,7 @@ invalid_legacy_expression_rule(Parser *p)
 // invalid_expression:
 //     | invalid_legacy_expression
 //     | !(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid
+//     | disjunction 'if' disjunction !'else'
 static void *
 invalid_expression_rule(Parser *p)
 {
@@ -18287,6 +18288,38 @@ invalid_expression_rule(Parser *p)
         D(fprintf(stderr, "%*c%s invalid_expression[%d-%d]: %s failed!\n", p->level, ' ',
                   p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid"));
     }
+    { // disjunction 'if' disjunction !'else'
+        if (p->error_indicator) {
+            D(p->level--);
+            return NULL;
+        }
+        D(fprintf(stderr, "%*c> invalid_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "disjunction 'if' disjunction !'else'"));
+        Token * _keyword;
+        expr_ty a;
+        expr_ty b;
+        if (
+            (a = disjunction_rule(p))  // disjunction
+            &&
+            (_keyword = _PyPegen_expect_token(p, 510))  // token='if'
+            &&
+            (b = disjunction_rule(p))  // disjunction
+            &&
+            _PyPegen_lookahead_with_int(0, _PyPegen_expect_token, p, 516)  // token='else'
+        )
+        {
+            D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "disjunction 'if' disjunction !'else'"));
+            _res = RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "expected 'else' after 'if' expression" );
+            if (_res == NULL && PyErr_Occurred()) {
+                p->error_indicator = 1;
+                D(p->level--);
+                return NULL;
+            }
+            goto done;
+        }
+        p->mark = _mark;
+        D(fprintf(stderr, "%*c%s invalid_expression[%d-%d]: %s failed!\n", p->level, ' ',
+                  p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "disjunction 'if' disjunction !'else'"));
+    }
     _res = NULL;
   done:
     D(p->level--);