]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: adjustments to error messages
authorRobert Griesemer <gri@golang.org>
Thu, 25 Aug 2022 23:41:35 +0000 (16:41 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 1 Sep 2022 22:37:04 +0000 (22:37 +0000)
- Use "expected X" rather then "expecting X".
- Report a better error when a type argument list is expected.
- Adjust various tests.

For #54511.

Change-Id: I0c5ca66ecbbdcae1a8f67377682aae6b0b6ab89a
Reviewed-on: https://go-review.googlesource.com/c/go/+/425734
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>

15 files changed:
src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/syntax/testdata/issue47704.go
src/cmd/compile/internal/syntax/testdata/tparams.go
src/cmd/compile/internal/types2/testdata/check/typeinst0.go
src/cmd/compile/internal/types2/testdata/check/vardecl.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue45635.go
test/fixedbugs/issue13273.go
test/fixedbugs/issue18092.go
test/fixedbugs/issue18747.go
test/fixedbugs/issue19667.go
test/fixedbugs/issue23664.go
test/fixedbugs/issue33386.go
test/switch2.go
test/syntax/ddd.go
test/syntax/semi4.go

index 8ae2ebbe7656dd30ceb4f08cf14bf9e1238c1cdc..d86fe1b72e43f31230c33236731c8a8d16a6350b 100644 (file)
@@ -190,7 +190,7 @@ func (p *parser) got(tok token) bool {
 
 func (p *parser) want(tok token) {
        if !p.got(tok) {
-               p.syntaxError("expecting " + tokstring(tok))
+               p.syntaxError("expected " + tokstring(tok))
                p.advance()
        }
 }
@@ -200,7 +200,7 @@ func (p *parser) want(tok token) {
 func (p *parser) gotAssign() bool {
        switch p.tok {
        case _Define:
-               p.syntaxError("expecting =")
+               p.syntaxError("expected =")
                fallthrough
        case _Assign:
                p.next()
@@ -246,7 +246,7 @@ func (p *parser) syntaxErrorAt(pos Pos, msg string) {
                // nothing to do
        case strings.HasPrefix(msg, "in "), strings.HasPrefix(msg, "at "), strings.HasPrefix(msg, "after "):
                msg = " " + msg
-       case strings.HasPrefix(msg, "expecting "):
+       case strings.HasPrefix(msg, "expected "):
                msg = ", " + msg
        default:
                // plain error - we don't care about current token
@@ -272,6 +272,8 @@ func (p *parser) syntaxErrorAt(pos Pos, msg string) {
                tok = tokstring(p.tok)
        }
 
+       // TODO(gri) This may print "unexpected X, expected Y".
+       //           Consider "got X, expected Y" in this case.
        p.errorAt(pos, "syntax error: unexpected "+tok+msg)
 }
 
@@ -774,7 +776,7 @@ func (p *parser) funcDeclOrNil() *FuncDecl {
        }
 
        if p.tok != _Name {
-               p.syntaxError("expecting name or (")
+               p.syntaxError("expected name or (")
                p.advance(_Lbrace, _Semi)
                return nil
        }
@@ -904,7 +906,7 @@ func (p *parser) unaryExpr() Expr {
                                if dir == RecvOnly {
                                        // t is type <-chan E but <-<-chan E is not permitted
                                        // (report same error as for "type _ <-<-chan E")
-                                       p.syntaxError("unexpected <-, expecting chan")
+                                       p.syntaxError("unexpected <-, expected chan")
                                        // already progressed, no need to advance
                                }
                                c.Dir = RecvOnly
@@ -913,7 +915,7 @@ func (p *parser) unaryExpr() Expr {
                        if dir == SendOnly {
                                // channel dir is <- but channel element E is not a channel
                                // (report same error as for "type _ <-chan<-E")
-                               p.syntaxError(fmt.Sprintf("unexpected %s, expecting chan", String(t)))
+                               p.syntaxError(fmt.Sprintf("unexpected %s, expected chan", String(t)))
                                // already progressed, no need to advance
                        }
                        return x
@@ -1038,7 +1040,7 @@ func (p *parser) operand(keep_parens bool) Expr {
 
        default:
                x := p.badExpr()
-               p.syntaxError("expecting expression")
+               p.syntaxError("expected expression")
                p.advance(_Rparen, _Rbrack, _Rbrace)
                return x
        }
@@ -1109,7 +1111,7 @@ loop:
                                p.want(_Rparen)
 
                        default:
-                               p.syntaxError("expecting name or (")
+                               p.syntaxError("expected name or (")
                                p.advance(_Semi, _Rparen)
                        }
 
@@ -1121,7 +1123,7 @@ loop:
                                var comma bool
                                if p.tok == _Rbrack {
                                        // invalid empty instance, slice or index expression; accept but complain
-                                       p.syntaxError("expecting operand")
+                                       p.syntaxError("expected operand")
                                        i = p.badExpr()
                                } else {
                                        i, comma = p.typeList()
@@ -1141,7 +1143,7 @@ loop:
                        // x[i:...
                        // For better error message, don't simply use p.want(_Colon) here (issue #47704).
                        if !p.got(_Colon) {
-                               p.syntaxError("expecting comma, : or ]")
+                               p.syntaxError("expected comma, : or ]")
                                p.advance(_Comma, _Colon, _Rbrack)
                        }
                        p.xnest++
@@ -1293,7 +1295,7 @@ func (p *parser) type_() Expr {
        typ := p.typeOrNil()
        if typ == nil {
                typ = p.badExpr()
-               p.syntaxError("expecting type")
+               p.syntaxError("expected type")
                p.advance(_Comma, _Colon, _Semi, _Rparen, _Rbrack, _Rbrace)
        }
 
@@ -1405,7 +1407,7 @@ func (p *parser) typeInstance(typ Expr) Expr {
        x.pos = pos
        x.X = typ
        if p.tok == _Rbrack {
-               p.syntaxError("expecting type")
+               p.syntaxError("expected type argument list")
                x.Index = p.badExpr()
        } else {
                x.Index, _ = p.typeList()
@@ -1460,7 +1462,7 @@ func (p *parser) arrayType(pos Pos, len Expr) Expr {
                // Trailing commas are accepted in type parameter
                // lists but not in array type declarations.
                // Accept for better error handling but complain.
-               p.syntaxError("unexpected comma; expecting ]")
+               p.syntaxError("unexpected comma; expected ]")
                p.next()
        }
        p.want(_Rbrack)
@@ -1660,7 +1662,7 @@ func (p *parser) fieldDecl(styp *StructType) {
                p.addField(styp, pos, nil, typ, tag)
 
        default:
-               p.syntaxError("expecting field name or embedded type")
+               p.syntaxError("expected field name or embedded type")
                p.advance(_Semi, _Rbrace)
        }
 }
@@ -1850,7 +1852,7 @@ func (p *parser) embeddedTerm() Expr {
        t := p.typeOrNil()
        if t == nil {
                t = p.badExpr()
-               p.syntaxError("expecting ~ term or type")
+               p.syntaxError("expected ~ term or type")
                p.advance(_Operator, _Semi, _Rparen, _Rbrack, _Rbrace)
        }
 
@@ -1949,7 +1951,7 @@ func (p *parser) paramDeclOrNil(name *Name, follow token) *Field {
                return f
        }
 
-       p.syntaxError("expecting " + tokstring(follow))
+       p.syntaxError("expected " + tokstring(follow))
        p.advance(_Comma, follow)
        return nil
 }
@@ -2155,7 +2157,7 @@ func (p *parser) simpleStmt(lhs Expr, keyword token) SimpleStmt {
                return p.newAssignStmt(pos, op, lhs, rhs)
 
        default:
-               p.syntaxError("expecting := or = or comma")
+               p.syntaxError("expected := or = or comma")
                p.advance(_Semi, _Rbrace)
                // make the best of what we have
                if x, ok := lhs.(*ListExpr); ok {
@@ -2230,7 +2232,7 @@ func (p *parser) blockStmt(context string) *BlockStmt {
 
        // people coming from C may forget that braces are mandatory in Go
        if !p.got(_Lbrace) {
-               p.syntaxError("expecting { after " + context)
+               p.syntaxError("expected { after " + context)
                p.advance(_Name, _Rbrace)
                s.Rbrace = p.pos() // in case we found "}"
                if p.got(_Rbrace) {
@@ -2321,7 +2323,7 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS
                if keyword == _For {
                        if p.tok != _Semi {
                                if p.tok == _Lbrace {
-                                       p.syntaxError("expecting for loop condition")
+                                       p.syntaxError("expected for loop condition")
                                        goto done
                                }
                                condStmt = p.simpleStmt(nil, 0 /* range not permitted */)
@@ -2347,7 +2349,7 @@ done:
        case nil:
                if keyword == _If && semi.pos.IsKnown() {
                        if semi.lit != "semicolon" {
-                               p.syntaxErrorAt(semi.pos, fmt.Sprintf("unexpected %s, expecting { after if clause", semi.lit))
+                               p.syntaxErrorAt(semi.pos, fmt.Sprintf("unexpected %s, expected { after if clause", semi.lit))
                        } else {
                                p.syntaxErrorAt(semi.pos, "missing condition in if statement")
                        }
@@ -2466,7 +2468,7 @@ func (p *parser) caseClause() *CaseClause {
                p.next()
 
        default:
-               p.syntaxError("expecting case or default or }")
+               p.syntaxError("expected case or default or }")
                p.advance(_Colon, _Case, _Default, _Rbrace)
        }
 
@@ -2506,7 +2508,7 @@ func (p *parser) commClause() *CommClause {
                p.next()
 
        default:
-               p.syntaxError("expecting case or default or }")
+               p.syntaxError("expected case or default or }")
                p.advance(_Colon, _Case, _Default, _Rbrace)
        }
 
@@ -2683,7 +2685,7 @@ func (p *parser) name() *Name {
        }
 
        n := NewName(p.pos(), "_")
-       p.syntaxError("expecting name")
+       p.syntaxError("expected name")
        p.advance()
        return n
 }
@@ -2721,7 +2723,7 @@ func (p *parser) qualifiedName(name *Name) Expr {
                x = p.name()
        default:
                x = NewName(p.pos(), "_")
-               p.syntaxError("expecting name")
+               p.syntaxError("expected name")
                p.advance(_Dot, _Semi, _Rbrace)
        }
 
index 2f2e29b693427907d933b62c9c94d5fa457233e0..e4cdad148f62c01c8767b8c3c96cd0851f3ac6c4 100644 (file)
@@ -5,13 +5,13 @@
 package p
 
 func _() {
-       _ = m[] // ERROR expecting operand
+       _ = m[] // ERROR expected operand
        _ = m[x,]
        _ = m[x /* ERROR unexpected a */ a b c d]
 }
 
 // test case from the issue
 func f(m map[int]int) int {
-       return m[0 // ERROR expecting comma, \: or \]
+       return m[0 // ERROR expected comma, \: or \]
                ]
 }
index 671833f9310dbd444cbdb6d18128341596a6f586..646fbbebc8940886ad8c753b2d713b811aa12f88 100644 (file)
@@ -21,7 +21,7 @@ func f[ /* ERROR empty type parameter list */ ]()
 func f[a, b /* ERROR missing type constraint */ ]()
 func f[a t, b t, c /* ERROR missing type constraint */ ]()
 
-func f[a b,  /* ERROR expecting ] */ 0] ()
+func f[a b,  /* ERROR expected ] */ 0] ()
 
 // issue #49482
 type (
index 0e6dc0a98f328db98796fdee7fe0c47e8ef9d8bd..6423cb801f9971d69ea6f07eed270aca3b220f9a 100644 (file)
@@ -36,11 +36,11 @@ var _ A3
 var x int
 type _ x /* ERROR not a type */ [int]
 
-type _ int /* ERROR not a generic type */ [] // ERROR expecting type
-type _ myInt /* ERROR not a generic type */ [] // ERROR expecting type
+type _ int /* ERROR not a generic type */ [] // ERROR expected type argument list
+type _ myInt /* ERROR not a generic type */ [] // ERROR expected type argument list
 
 // TODO(gri) better error messages
-type _ T1[] // ERROR expecting type
+type _ T1[] // ERROR expected type argument list
 type _ T1[x /* ERROR not a type */ ]
 type _ T1 /* ERROR got 2 arguments but 1 type parameters */ [int, float32]
 
index c3fe61c3d4901d4d651bf11f278e4b87967f5686..19ccc98009adae939a31ea87588881acaa905d08 100644 (file)
@@ -14,9 +14,9 @@ var m map[string]int
 var _ int
 var _, _ int
 
-var _ /* ERROR "expecting type" */
-var _, _ /* ERROR "expecting type" */
-var _, _, _ /* ERROR "expecting type" */
+var _ /* ERROR "expected type" */
+var _, _ /* ERROR "expected type" */
+var _, _, _ /* ERROR "expected type" */
 
 // The initializer must be an expression.
 var _ = int /* ERROR "not an expression" */
index 29379591057a6148a38cc0ff6ede246c527ee79e..3d8e3453edc4821c8c2177f91584697911ecc11a 100644 (file)
@@ -10,7 +10,7 @@ func main() {
 
 type N[T any] struct{}
 
-var _ N[] /* ERROR expecting type */
+var _ N[] /* ERROR expected type */
 
 type I interface {
        ~[]int
index 2498da4d47161aa7c585bbee08387bf36ba44d28..ea729d6080324514983f6927112dee253681b3c0 100644 (file)
@@ -47,9 +47,9 @@ func f() {
        <-(<-chan (<-chan (<-chan (<-chan int))))(nil)
        <-(<-chan (<-chan (<-chan (<-chan (<-chan int)))))(nil)
 
-       type _ <-<-chan int // ERROR "unexpected <-, expecting chan|expected .*chan.*"
-       <-<-chan int // ERROR "unexpected <-, expecting chan|expecting {" (new parser: same error as for type decl)
+       type _ <-<-chan int // ERROR "unexpected <-, expected chan|expected .*chan.*"
+       <-<-chan int // ERROR "unexpected <-, expected chan|expecting {" (new parser: same error as for type decl)
 
-       type _ <-chan<-int // ERROR "unexpected int, expecting chan|expected .*chan.*|expecting chan|expected .*;.* or .*}.* or newline"
-       <-chan<-int // ERROR "unexpected int, expecting chan|expecting {" (new parser: same error as for type decl)
+       type _ <-chan<-int // ERROR "unexpected int, expected chan|expected .*chan.*|expected chan|expected .*;.* or .*}.* or newline"
+       <-chan<-int // ERROR "unexpected int, expected chan|expecting {" (new parser: same error as for type decl)
 }
index a0f7eddda5a773b56e4af0c58bd29aa732f7c29c..c8e60f31c515fecbbb9c3120094c96843639302b 100644 (file)
@@ -11,5 +11,5 @@ func _() {
        select {
        default:
        case <-ch { // GCCGO_ERROR "expected colon"
-       }           // GC_ERROR "expecting :"
+       }           // GC_ERROR "expected :"
 }
index fb8331fcc9eeeddcd80bb726c67ed738358965e3..4eabe0e61df5983ef6670f2bd33263fbe00ef567 100644 (file)
@@ -23,6 +23,6 @@ func _ () {
 
        if ; foo {}
 
-       if foo // ERROR "unexpected newline, expecting { after if clause"
+       if foo // ERROR "unexpected newline, expected { after if clause"
        {}
 }
index e33e35048752dfb4f4c3110e4fba31791e822d98..4b0925add24719c43e003ab05596ddcf997fd877 100644 (file)
@@ -10,4 +10,4 @@ package p
 
 func f() {
        if err := http.ListenAndServe( // GCCGO_ERROR "undefined name"
-} // ERROR "unexpected }, expecting expression|expected operand|missing .*\)|expected .*;|expected .*{"
+} // ERROR "unexpected }, expected expression|expected operand|missing .*\)|expected .*;|expected .*{"
index 1925ebffe73778691b619895ac4e86ae0ff1249b..715654be70deb59ae48009dd3d8d9b8b29d1808d 100644 (file)
@@ -9,9 +9,9 @@
 package p
 
 func f() {
-       if f() true { // ERROR "unexpected true, expecting {"
+       if f() true { // ERROR "unexpected true, expected {"
        }
        
-       switch f() true { // ERROR "unexpected true, expecting {"
+       switch f() true { // ERROR "unexpected true, expected {"
        }
 }
index 7b2f565285e6f8290979fcd69d4dcc830b08c9d9..a7074069febca11ac669f4f41de8046228e7f2ad 100644 (file)
@@ -13,17 +13,17 @@ package p
 func _() {
        go func() {     // no error here about goroutine
                send <- // GCCGO_ERROR "undefined name"
-       }()             // ERROR "expecting expression|expected operand"
+       }()             // ERROR "expected expression|expected operand"
 }
 
 func _() {
        defer func() { // no error here about deferred function
                1 +    // GCCGO_ERROR "value computed is not used"
-       }()            // ERROR "expecting expression|expected operand"
+       }()            // ERROR "expected expression|expected operand"
 }
 
 func _() {
-       _ = (1 +)             // ERROR "expecting expression|expected operand"
-       _ = a[2 +]            // ERROR "expecting expression|expected operand|undefined name"
-       _ = []int{1, 2, 3 + } // ERROR "expecting expression|expected operand"
+       _ = (1 +)             // ERROR "expected expression|expected operand"
+       _ = a[2 +]            // ERROR "expected expression|expected operand|undefined name"
+       _ = []int{1, 2, 3 + } // ERROR "expected expression|expected operand"
 }
index 11b85d3692ceadd45f88a4cec72de436dd4aff0e..66e89fda19e2c3079b5b453d1ed5d2df7a25bca5 100644 (file)
@@ -11,11 +11,11 @@ package main
 
 func f() {
        switch {
-       case 0; // ERROR "expecting := or = or : or comma|expecting :"
+       case 0; // ERROR "expecting := or = or : or comma|expected :"
        }
 
        switch {
-       case 0; // ERROR "expecting := or = or : or comma|expecting :"
+       case 0; // ERROR "expecting := or = or : or comma|expected :"
        default:
        }
 
@@ -34,6 +34,6 @@ func f() {
        }
 
        switch {
-       if x: // ERROR "expecting case or default or }"
+       if x: // ERROR "expected case or default or }"
        }
 }
index 476ae22793d3ff2097494e35ed35537184e5c77b..8d1e4d19035dcc06792c0ed9f49b878d7c5ad31f 100644 (file)
@@ -7,5 +7,5 @@
 package main
 
 func f() {
-       g(f..3) // ERROR "unexpected literal \.3, expecting name or \("
+       g(f..3) // ERROR "unexpected literal \.3, expected name or \("
 }
index 08c354751b6da12f5c0004cb5c3ab34f3b565d70..62a511e6104e47ce0835417c7a452b71226b2c59 100644 (file)
@@ -8,5 +8,5 @@ package main
 
 func main() {
        for x           // GCCGO_ERROR "undefined"
-       {               // ERROR "unexpected {, expecting for loop condition|expecting .*{.* after for clause"
+       {               // ERROR "unexpected {, expected for loop condition|expecting .*{.* after for clause"
                z       // GCCGO_ERROR "undefined"