From e48d7099b80c5aadbde9124f5bbe6ae07d8adb1a Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sun, 22 Dec 2024 11:46:12 +0100 Subject: [PATCH 1/3] go/parser: require label after goto Change-Id: I0209f9d1870a2932dfbd347c252e3f9af055afd3 --- src/go/parser/parser.go | 2 +- src/go/parser/short_test.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go index 7260e963041871..1207b5f24b98e3 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.FALLTHROUGH && p.tok == token.IDENT) || tok == token.GOTO { 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) { From 0d340b1c52224c6ae07f15c00ed7aa3629e5015d Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sun, 22 Dec 2024 11:52:22 +0100 Subject: [PATCH 2/3] also test syntax package Change-Id: I41098d8c1f0aa4beb4098c7ade475807c80ab0af --- src/cmd/compile/internal/syntax/testdata/issue70957.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/cmd/compile/internal/syntax/testdata/issue70957.go 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 From 310bd1537b7a36758f3fbf8db476fa68e1a11599 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sun, 22 Dec 2024 17:53:13 +0100 Subject: [PATCH 3/3] update Change-Id: I1e5f3283b3967c5737a8892b8ef9f9817d792d68 --- src/go/parser/parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go index 1207b5f24b98e3..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) || tok == token.GOTO { + if tok == token.GOTO || ((tok == token.CONTINUE || tok == token.BREAK) && p.tok == token.IDENT) { label = p.parseIdent() } p.expectSemi()