diff --git a/src/cmd/compile/internal/syntax/testdata/issue70957.go b/src/cmd/compile/internal/syntax/testdata/issue70957.go new file mode 100644 index 00000000000000..921478f67c86c7 --- /dev/null +++ b/src/cmd/compile/internal/syntax/testdata/issue70957.go @@ -0,0 +1,9 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package p + +func f() { goto /* ERROR syntax error: unexpected semicolon, expected name */ ;} + +func f() { goto } // ERROR syntax error: unexpected }, expected name diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go index 7260e963041871..6061c10a31cfd4 100644 --- a/src/go/parser/parser.go +++ b/src/go/parser/parser.go @@ -2050,7 +2050,7 @@ func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt { pos := p.expect(tok) var label *ast.Ident - if tok != token.FALLTHROUGH && p.tok == token.IDENT { + if tok == token.GOTO || ((tok == token.CONTINUE || tok == token.BREAK) && p.tok == token.IDENT) { label = p.parseIdent() } p.expectSemi() diff --git a/src/go/parser/short_test.go b/src/go/parser/short_test.go index 3a34e8c216bf96..913b96a95fcd7f 100644 --- a/src/go/parser/short_test.go +++ b/src/go/parser/short_test.go @@ -204,6 +204,10 @@ var invalids = []string{ `package p; func (T) _[ /* ERROR "must have no type parameters" */ A, B C[A, B]](a A) B`, `package p; func(*T[e, e /* ERROR "e redeclared" */ ]) _()`, + + // go.dev/issue/70957 + `package p; func f() {goto; /* ERROR "expected 'IDENT', found ';'" */ }`, + `package p; func f() {goto} /* ERROR "expected 'IDENT', found '}'" */ }`, } func TestInvalid(t *testing.T) {