aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateusz Poliwczak <mpoliwczak34@gmail.com>2025-01-07 18:28:10 +0000
committerRobert Griesemer <gri@google.com>2025-02-25 09:14:35 -0800
commitbdef1778311c19c997d4fb14a4374bd712014d13 (patch)
tree8fa75b952bc1c8f8404a1e4e7528b39f8e6897e0
parent2f036e1475f9d794451927d90c07d9f8c258db77 (diff)
downloadgo-bdef1778311c19c997d4fb14a4374bd712014d13.tar.xz
go/parser: require label after goto
Fixes #70957 Change-Id: Ied7cf29ea3e02bb71ddce8a19ddd381ce5991ed1 GitHub-Last-Rev: 310bd1537b7a36758f3fbf8db476fa68e1a11599 GitHub-Pull-Request: golang/go#70958 Reviewed-on: https://go-review.googlesource.com/c/go/+/638395 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Alan Donovan <adonovan@google.com>
-rw-r--r--src/cmd/compile/internal/syntax/testdata/issue70957.go9
-rw-r--r--src/go/parser/parser.go2
-rw-r--r--src/go/parser/short_test.go4
3 files changed, 14 insertions, 1 deletions
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 0000000000..921478f67c
--- /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 c2906c5bda..c31b65bb53 100644
--- a/src/go/parser/parser.go
+++ b/src/go/parser/parser.go
@@ -2066,7 +2066,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 9465fe0e47..422d1b38c3 100644
--- a/src/go/parser/short_test.go
+++ b/src/go/parser/short_test.go
@@ -212,6 +212,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) {