1
0
mirror of https://github.com/golang/go synced 2024-11-22 03:34:40 -07:00

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) {
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)
}

View 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 \]
]
}

View 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 (

View 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]

View 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" */

View 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

View 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)
}

View File

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

View 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"
{}
}

View 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 .*{"

View 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 {"
}
}

View 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"
}

View 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 }"
}
}

View 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 \("
}

View 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"