go/parser: adjustments to error messages

- 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>
This commit is contained in:
Robert Griesemer 2022-08-25 16:41:35 -07:00 committed by Gopher Robot
parent c801e4b10f
commit aa5ff29dab
15 changed files with 55 additions and 53 deletions

View file

@ -190,7 +190,7 @@ func (p *parser) got(tok token) bool {
func (p *parser) want(tok token) { func (p *parser) want(tok token) {
if !p.got(tok) { if !p.got(tok) {
p.syntaxError("expecting " + tokstring(tok)) p.syntaxError("expected " + tokstring(tok))
p.advance() p.advance()
} }
} }
@ -200,7 +200,7 @@ func (p *parser) want(tok token) {
func (p *parser) gotAssign() bool { func (p *parser) gotAssign() bool {
switch p.tok { switch p.tok {
case _Define: case _Define:
p.syntaxError("expecting =") p.syntaxError("expected =")
fallthrough fallthrough
case _Assign: case _Assign:
p.next() p.next()
@ -246,7 +246,7 @@ func (p *parser) syntaxErrorAt(pos Pos, msg string) {
// nothing to do // nothing to do
case strings.HasPrefix(msg, "in "), strings.HasPrefix(msg, "at "), strings.HasPrefix(msg, "after "): case strings.HasPrefix(msg, "in "), strings.HasPrefix(msg, "at "), strings.HasPrefix(msg, "after "):
msg = " " + msg msg = " " + msg
case strings.HasPrefix(msg, "expecting "): case strings.HasPrefix(msg, "expected "):
msg = ", " + msg msg = ", " + msg
default: default:
// plain error - we don't care about current token // 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) 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) p.errorAt(pos, "syntax error: unexpected "+tok+msg)
} }
@ -774,7 +776,7 @@ func (p *parser) funcDeclOrNil() *FuncDecl {
} }
if p.tok != _Name { if p.tok != _Name {
p.syntaxError("expecting name or (") p.syntaxError("expected name or (")
p.advance(_Lbrace, _Semi) p.advance(_Lbrace, _Semi)
return nil return nil
} }
@ -904,7 +906,7 @@ func (p *parser) unaryExpr() Expr {
if dir == RecvOnly { if dir == RecvOnly {
// t is type <-chan E but <-<-chan E is not permitted // t is type <-chan E but <-<-chan E is not permitted
// (report same error as for "type _ <-<-chan E") // (report same error as for "type _ <-<-chan E")
p.syntaxError("unexpected <-, expecting chan") p.syntaxError("unexpected <-, expected chan")
// already progressed, no need to advance // already progressed, no need to advance
} }
c.Dir = RecvOnly c.Dir = RecvOnly
@ -913,7 +915,7 @@ func (p *parser) unaryExpr() Expr {
if dir == SendOnly { if dir == SendOnly {
// channel dir is <- but channel element E is not a channel // channel dir is <- but channel element E is not a channel
// (report same error as for "type _ <-chan<-E") // (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 // already progressed, no need to advance
} }
return x return x
@ -1038,7 +1040,7 @@ func (p *parser) operand(keep_parens bool) Expr {
default: default:
x := p.badExpr() x := p.badExpr()
p.syntaxError("expecting expression") p.syntaxError("expected expression")
p.advance(_Rparen, _Rbrack, _Rbrace) p.advance(_Rparen, _Rbrack, _Rbrace)
return x return x
} }
@ -1109,7 +1111,7 @@ loop:
p.want(_Rparen) p.want(_Rparen)
default: default:
p.syntaxError("expecting name or (") p.syntaxError("expected name or (")
p.advance(_Semi, _Rparen) p.advance(_Semi, _Rparen)
} }
@ -1121,7 +1123,7 @@ loop:
var comma bool var comma bool
if p.tok == _Rbrack { if p.tok == _Rbrack {
// invalid empty instance, slice or index expression; accept but complain // invalid empty instance, slice or index expression; accept but complain
p.syntaxError("expecting operand") p.syntaxError("expected operand")
i = p.badExpr() i = p.badExpr()
} else { } else {
i, comma = p.typeList() i, comma = p.typeList()
@ -1141,7 +1143,7 @@ loop:
// x[i:... // x[i:...
// For better error message, don't simply use p.want(_Colon) here (issue #47704). // For better error message, don't simply use p.want(_Colon) here (issue #47704).
if !p.got(_Colon) { if !p.got(_Colon) {
p.syntaxError("expecting comma, : or ]") p.syntaxError("expected comma, : or ]")
p.advance(_Comma, _Colon, _Rbrack) p.advance(_Comma, _Colon, _Rbrack)
} }
p.xnest++ p.xnest++
@ -1293,7 +1295,7 @@ func (p *parser) type_() Expr {
typ := p.typeOrNil() typ := p.typeOrNil()
if typ == nil { if typ == nil {
typ = p.badExpr() typ = p.badExpr()
p.syntaxError("expecting type") p.syntaxError("expected type")
p.advance(_Comma, _Colon, _Semi, _Rparen, _Rbrack, _Rbrace) p.advance(_Comma, _Colon, _Semi, _Rparen, _Rbrack, _Rbrace)
} }
@ -1405,7 +1407,7 @@ func (p *parser) typeInstance(typ Expr) Expr {
x.pos = pos x.pos = pos
x.X = typ x.X = typ
if p.tok == _Rbrack { if p.tok == _Rbrack {
p.syntaxError("expecting type") p.syntaxError("expected type argument list")
x.Index = p.badExpr() x.Index = p.badExpr()
} else { } else {
x.Index, _ = p.typeList() x.Index, _ = p.typeList()
@ -1460,7 +1462,7 @@ func (p *parser) arrayType(pos Pos, len Expr) Expr {
// Trailing commas are accepted in type parameter // Trailing commas are accepted in type parameter
// lists but not in array type declarations. // lists but not in array type declarations.
// Accept for better error handling but complain. // Accept for better error handling but complain.
p.syntaxError("unexpected comma; expecting ]") p.syntaxError("unexpected comma; expected ]")
p.next() p.next()
} }
p.want(_Rbrack) p.want(_Rbrack)
@ -1660,7 +1662,7 @@ func (p *parser) fieldDecl(styp *StructType) {
p.addField(styp, pos, nil, typ, tag) p.addField(styp, pos, nil, typ, tag)
default: default:
p.syntaxError("expecting field name or embedded type") p.syntaxError("expected field name or embedded type")
p.advance(_Semi, _Rbrace) p.advance(_Semi, _Rbrace)
} }
} }
@ -1850,7 +1852,7 @@ func (p *parser) embeddedTerm() Expr {
t := p.typeOrNil() t := p.typeOrNil()
if t == nil { if t == nil {
t = p.badExpr() t = p.badExpr()
p.syntaxError("expecting ~ term or type") p.syntaxError("expected ~ term or type")
p.advance(_Operator, _Semi, _Rparen, _Rbrack, _Rbrace) p.advance(_Operator, _Semi, _Rparen, _Rbrack, _Rbrace)
} }
@ -1949,7 +1951,7 @@ func (p *parser) paramDeclOrNil(name *Name, follow token) *Field {
return f return f
} }
p.syntaxError("expecting " + tokstring(follow)) p.syntaxError("expected " + tokstring(follow))
p.advance(_Comma, follow) p.advance(_Comma, follow)
return nil return nil
} }
@ -2155,7 +2157,7 @@ func (p *parser) simpleStmt(lhs Expr, keyword token) SimpleStmt {
return p.newAssignStmt(pos, op, lhs, rhs) return p.newAssignStmt(pos, op, lhs, rhs)
default: default:
p.syntaxError("expecting := or = or comma") p.syntaxError("expected := or = or comma")
p.advance(_Semi, _Rbrace) p.advance(_Semi, _Rbrace)
// make the best of what we have // make the best of what we have
if x, ok := lhs.(*ListExpr); ok { 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 // people coming from C may forget that braces are mandatory in Go
if !p.got(_Lbrace) { if !p.got(_Lbrace) {
p.syntaxError("expecting { after " + context) p.syntaxError("expected { after " + context)
p.advance(_Name, _Rbrace) p.advance(_Name, _Rbrace)
s.Rbrace = p.pos() // in case we found "}" s.Rbrace = p.pos() // in case we found "}"
if p.got(_Rbrace) { if p.got(_Rbrace) {
@ -2321,7 +2323,7 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS
if keyword == _For { if keyword == _For {
if p.tok != _Semi { if p.tok != _Semi {
if p.tok == _Lbrace { if p.tok == _Lbrace {
p.syntaxError("expecting for loop condition") p.syntaxError("expected for loop condition")
goto done goto done
} }
condStmt = p.simpleStmt(nil, 0 /* range not permitted */) condStmt = p.simpleStmt(nil, 0 /* range not permitted */)
@ -2347,7 +2349,7 @@ done:
case nil: case nil:
if keyword == _If && semi.pos.IsKnown() { if keyword == _If && semi.pos.IsKnown() {
if semi.lit != "semicolon" { 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 { } else {
p.syntaxErrorAt(semi.pos, "missing condition in if statement") p.syntaxErrorAt(semi.pos, "missing condition in if statement")
} }
@ -2466,7 +2468,7 @@ func (p *parser) caseClause() *CaseClause {
p.next() p.next()
default: default:
p.syntaxError("expecting case or default or }") p.syntaxError("expected case or default or }")
p.advance(_Colon, _Case, _Default, _Rbrace) p.advance(_Colon, _Case, _Default, _Rbrace)
} }
@ -2506,7 +2508,7 @@ func (p *parser) commClause() *CommClause {
p.next() p.next()
default: default:
p.syntaxError("expecting case or default or }") p.syntaxError("expected case or default or }")
p.advance(_Colon, _Case, _Default, _Rbrace) p.advance(_Colon, _Case, _Default, _Rbrace)
} }
@ -2683,7 +2685,7 @@ func (p *parser) name() *Name {
} }
n := NewName(p.pos(), "_") n := NewName(p.pos(), "_")
p.syntaxError("expecting name") p.syntaxError("expected name")
p.advance() p.advance()
return n return n
} }
@ -2721,7 +2723,7 @@ func (p *parser) qualifiedName(name *Name) Expr {
x = p.name() x = p.name()
default: default:
x = NewName(p.pos(), "_") x = NewName(p.pos(), "_")
p.syntaxError("expecting name") p.syntaxError("expected name")
p.advance(_Dot, _Semi, _Rbrace) p.advance(_Dot, _Semi, _Rbrace)
} }

View file

@ -5,13 +5,13 @@
package p package p
func _() { func _() {
_ = m[] // ERROR expecting operand _ = m[] // ERROR expected operand
_ = m[x,] _ = m[x,]
_ = m[x /* ERROR unexpected a */ a b c d] _ = m[x /* ERROR unexpected a */ a b c d]
} }
// test case from the issue // test case from the issue
func f(m map[int]int) int { func f(m map[int]int) int {
return m[0 // ERROR expecting comma, \: or \] return m[0 // ERROR expected comma, \: or \]
] ]
} }

View file

@ -21,7 +21,7 @@ func f[ /* ERROR empty type parameter list */ ]()
func f[a, b /* ERROR missing type constraint */ ]() func f[a, b /* ERROR missing type constraint */ ]()
func f[a t, b t, c /* 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 // issue #49482
type ( type (

View file

@ -36,11 +36,11 @@ var _ A3
var x int var x int
type _ x /* ERROR not a type */ [int] type _ x /* ERROR not a type */ [int]
type _ int /* 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 expecting type type _ myInt /* ERROR not a generic type */ [] // ERROR expected type argument list
// TODO(gri) better error messages // 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[x /* ERROR not a type */ ]
type _ T1 /* ERROR got 2 arguments but 1 type parameters */ [int, float32] type _ T1 /* ERROR got 2 arguments but 1 type parameters */ [int, float32]

View file

@ -14,9 +14,9 @@ var m map[string]int
var _ int var _ int
var _, _ int var _, _ int
var _ /* ERROR "expecting type" */ var _ /* ERROR "expected type" */
var _, _ /* ERROR "expecting type" */ var _, _ /* ERROR "expected type" */
var _, _, _ /* ERROR "expecting type" */ var _, _, _ /* ERROR "expected type" */
// The initializer must be an expression. // The initializer must be an expression.
var _ = int /* ERROR "not an expression" */ var _ = int /* ERROR "not an expression" */

View file

@ -10,7 +10,7 @@ func main() {
type N[T any] struct{} type N[T any] struct{}
var _ N[] /* ERROR expecting type */ var _ N[] /* ERROR expected type */
type I interface { type I interface {
~[]int ~[]int

View file

@ -47,9 +47,9 @@ func f() {
<-(<-chan (<-chan (<-chan (<-chan int))))(nil) <-(<-chan (<-chan (<-chan (<-chan int))))(nil)
<-(<-chan (<-chan (<-chan (<-chan (<-chan int)))))(nil) <-(<-chan (<-chan (<-chan (<-chan (<-chan int)))))(nil)
type _ <-<-chan int // ERROR "unexpected <-, expecting chan|expected .*chan.*" type _ <-<-chan int // ERROR "unexpected <-, expected chan|expected .*chan.*"
<-<-chan int // ERROR "unexpected <-, expecting chan|expecting {" (new parser: same error as for type decl) <-<-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" type _ <-chan<-int // ERROR "unexpected int, expected chan|expected .*chan.*|expected chan|expected .*;.* or .*}.* or newline"
<-chan<-int // ERROR "unexpected int, expecting chan|expecting {" (new parser: same error as for type decl) <-chan<-int // ERROR "unexpected int, expected chan|expecting {" (new parser: same error as for type decl)
} }

View file

@ -11,5 +11,5 @@ func _() {
select { select {
default: default:
case <-ch { // GCCGO_ERROR "expected colon" case <-ch { // GCCGO_ERROR "expected colon"
} // GC_ERROR "expecting :" } // GC_ERROR "expected :"
} }

View file

@ -23,6 +23,6 @@ func _ () {
if ; foo {} if ; foo {}
if foo // ERROR "unexpected newline, expecting { after if clause" if foo // ERROR "unexpected newline, expected { after if clause"
{} {}
} }

View file

@ -10,4 +10,4 @@ package p
func f() { func f() {
if err := http.ListenAndServe( // GCCGO_ERROR "undefined name" 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 .*{"

View file

@ -9,9 +9,9 @@
package p package p
func f() { 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 {"
} }
} }

View file

@ -13,17 +13,17 @@ package p
func _() { func _() {
go func() { // no error here about goroutine go func() { // no error here about goroutine
send <- // GCCGO_ERROR "undefined name" send <- // GCCGO_ERROR "undefined name"
}() // ERROR "expecting expression|expected operand" }() // ERROR "expected expression|expected operand"
} }
func _() { func _() {
defer func() { // no error here about deferred function defer func() { // no error here about deferred function
1 + // GCCGO_ERROR "value computed is not used" 1 + // GCCGO_ERROR "value computed is not used"
}() // ERROR "expecting expression|expected operand" }() // ERROR "expected expression|expected operand"
} }
func _() { func _() {
_ = (1 +) // ERROR "expecting expression|expected operand" _ = (1 +) // ERROR "expected expression|expected operand"
_ = a[2 +] // ERROR "expecting expression|expected operand|undefined name" _ = a[2 +] // ERROR "expected expression|expected operand|undefined name"
_ = []int{1, 2, 3 + } // ERROR "expecting expression|expected operand" _ = []int{1, 2, 3 + } // ERROR "expected expression|expected operand"
} }

View file

@ -11,11 +11,11 @@ package main
func f() { func f() {
switch { switch {
case 0; // ERROR "expecting := or = or : or comma|expecting :" case 0; // ERROR "expecting := or = or : or comma|expected :"
} }
switch { switch {
case 0; // ERROR "expecting := or = or : or comma|expecting :" case 0; // ERROR "expecting := or = or : or comma|expected :"
default: default:
} }
@ -34,6 +34,6 @@ func f() {
} }
switch { switch {
if x: // ERROR "expecting case or default or }" if x: // ERROR "expected case or default or }"
} }
} }

View file

@ -7,5 +7,5 @@
package main package main
func f() { func f() {
g(f..3) // ERROR "unexpected literal \.3, expecting name or \(" g(f..3) // ERROR "unexpected literal \.3, expected name or \("
} }

View file

@ -8,5 +8,5 @@ package main
func main() { func main() {
for x // GCCGO_ERROR "undefined" 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" z // GCCGO_ERROR "undefined"