]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/syntax: provide BadExpr where needed, call correct error handler
authorRobert Griesemer <gri@golang.org>
Sat, 10 Oct 2020 00:42:41 +0000 (17:42 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 12 Oct 2020 18:01:22 +0000 (18:01 +0000)
- For "if" statements without a condition, provide a BadExpr rather than nil
  (clients expect IfStmt.Cond != nil since the parser is taking care of
  reporting a missing condition).

- For 3-index slice expressions, also provide BadExpr where an index is
  required but missing.

- Declare a parser-local error method to hide the embedded error method
  so we don't use it by mistake.

Accidentally found while adjusting prototype parser to work for generics.

Change-Id: Iacc211cc60869be05efe9ae630d65dff1dac00a0
Reviewed-on: https://go-review.googlesource.com/c/go/+/261218
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/syntax/parser.go

index 9601fab9e0d998ce07fd9db4a6b0cf7896f73714..1485b700596aa47617dd92453ba23881d7855ce3 100644 (file)
@@ -287,6 +287,7 @@ func tokstring(tok token) string {
 
 // Convenience methods using the current token position.
 func (p *parser) pos() Pos               { return p.posAt(p.line, p.col) }
+func (p *parser) error(msg string)       { p.errorAt(p.pos(), msg) }
 func (p *parser) syntaxError(msg string) { p.syntaxErrorAt(p.pos(), msg) }
 
 // The stopset contains keywords that start a statement.
@@ -997,17 +998,20 @@ loop:
                                // x[i:j...
                                t.Index[1] = p.expr()
                        }
-                       if p.got(_Colon) {
+                       if p.tok == _Colon {
                                t.Full = true
                                // x[i:j:...]
                                if t.Index[1] == nil {
                                        p.error("middle index required in 3-index slice")
+                                       t.Index[1] = p.badExpr()
                                }
+                               p.next()
                                if p.tok != _Rbrack {
                                        // x[i:j:k...
                                        t.Index[2] = p.expr()
                                } else {
                                        p.error("final index required in 3-index slice")
+                                       t.Index[2] = p.badExpr()
                                }
                        }
                        p.want(_Rbrack)
@@ -1836,6 +1840,7 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS
        if p.tok == _Lbrace {
                if keyword == _If {
                        p.syntaxError("missing condition in if statement")
+                       cond = p.badExpr()
                }
                return
        }
@@ -1907,6 +1912,9 @@ done:
                        } else {
                                p.syntaxErrorAt(semi.pos, "missing condition in if statement")
                        }
+                       b := new(BadExpr)
+                       b.pos = semi.pos
+                       cond = b
                }
        case *ExprStmt:
                cond = s.X