From e5e59bcc69a9cd146002b05afb5956de0c401a6a Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Mon, 23 Dec 2024 20:20:56 +0100 Subject: [PATCH 1/4] go/types: propagate lstmt in blockBranches properly Change-Id: Ibf108a0a7c533260d3264695c2429d6f4b6a75e1 --- src/go/types/labels.go | 22 +++++++-------- .../types/testdata/check/issue70974.go | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 src/internal/types/testdata/check/issue70974.go diff --git a/src/go/types/labels.go b/src/go/types/labels.go index 97b753581aedd9..7b6324880af6c0 100644 --- a/src/go/types/labels.go +++ b/src/go/types/labels.go @@ -119,8 +119,8 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *ast.Labele fwdJumps = append(fwdJumps, check.blockBranches(all, b, lstmt, list)...) } - var stmtBranches func(ast.Stmt) - stmtBranches = func(s ast.Stmt) { + var stmtBranches func(*ast.LabeledStmt, ast.Stmt) + stmtBranches = func(lstmt *ast.LabeledStmt, s ast.Stmt) { switch s := s.(type) { case *ast.DeclStmt: if d, _ := s.Decl.(*ast.GenDecl); d != nil && d.Tok == token.VAR { @@ -168,7 +168,7 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *ast.Labele fwdJumps = fwdJumps[:i] lstmt = s } - stmtBranches(s.Stmt) + stmtBranches(lstmt, s.Stmt) case *ast.BranchStmt: if s.Label == nil { @@ -235,36 +235,36 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *ast.Labele blockBranches(lstmt, s.List) case *ast.IfStmt: - stmtBranches(s.Body) + stmtBranches(lstmt, s.Body) if s.Else != nil { - stmtBranches(s.Else) + stmtBranches(lstmt, s.Else) } case *ast.CaseClause: blockBranches(nil, s.Body) case *ast.SwitchStmt: - stmtBranches(s.Body) + stmtBranches(lstmt, s.Body) case *ast.TypeSwitchStmt: - stmtBranches(s.Body) + stmtBranches(lstmt, s.Body) case *ast.CommClause: blockBranches(nil, s.Body) case *ast.SelectStmt: - stmtBranches(s.Body) + stmtBranches(lstmt, s.Body) case *ast.ForStmt: - stmtBranches(s.Body) + stmtBranches(lstmt, s.Body) case *ast.RangeStmt: - stmtBranches(s.Body) + stmtBranches(lstmt, s.Body) } } for _, s := range list { - stmtBranches(s) + stmtBranches(nil, s) } return fwdJumps diff --git a/src/internal/types/testdata/check/issue70974.go b/src/internal/types/testdata/check/issue70974.go new file mode 100644 index 00000000000000..13d79130b423ab --- /dev/null +++ b/src/internal/types/testdata/check/issue70974.go @@ -0,0 +1,27 @@ +// Copyright 2014 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 _() { +outer: + for { + break outer + } + + for { + continue outer /* ERROR "invalid continue label outer" */ + } +} + +func _() { +outer: + for { + continue outer + } + + for { + continue outer /* ERROR "invalid continue label outer" */ + } +} From 8e3956839442c45d8255b39da24132548674b1c9 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Mon, 23 Dec 2024 20:35:13 +0100 Subject: [PATCH 2/4] typo Change-Id: I8337d67744a8dff95dfa002f1d304e7ee66f3d98 --- src/internal/types/testdata/check/issue70974.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/types/testdata/check/issue70974.go b/src/internal/types/testdata/check/issue70974.go index 13d79130b423ab..59b11653cee18f 100644 --- a/src/internal/types/testdata/check/issue70974.go +++ b/src/internal/types/testdata/check/issue70974.go @@ -11,7 +11,7 @@ outer: } for { - continue outer /* ERROR "invalid continue label outer" */ + break outer /* ERROR "invalid break label outer" */ } } From f1dc0597947aa4215268b2e11feae266d74cddc1 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Mon, 23 Dec 2024 21:01:39 +0100 Subject: [PATCH 3/4] also fix cmd/compile/internal/types2 Change-Id: I42799dd22fc326057736710588fbb383aad729fa --- src/cmd/compile/internal/types2/labels.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cmd/compile/internal/types2/labels.go b/src/cmd/compile/internal/types2/labels.go index e44b7c7f70f3cd..6a6f64b4b8a7b2 100644 --- a/src/cmd/compile/internal/types2/labels.go +++ b/src/cmd/compile/internal/types2/labels.go @@ -112,8 +112,8 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab return varDeclPos.IsKnown() && slices.Contains(badJumps, jmp) } - var stmtBranches func(syntax.Stmt) - stmtBranches = func(s syntax.Stmt) { + var stmtBranches func(*syntax.LabeledStmt, syntax.Stmt) + stmtBranches = func(lstmt *syntax.LabeledStmt, s syntax.Stmt) { switch s := s.(type) { case *syntax.DeclStmt: for _, d := range s.DeclList { @@ -163,7 +163,7 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab fwdJumps = fwdJumps[:i] lstmt = s } - stmtBranches(s.Stmt) + stmtBranches(lstmt, s.Stmt) case *syntax.BranchStmt: if s.Label == nil { @@ -232,9 +232,9 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab fwdJumps = append(fwdJumps, check.blockBranches(all, b, lstmt, s.List)...) case *syntax.IfStmt: - stmtBranches(s.Then) + stmtBranches(lstmt, s.Then) if s.Else != nil { - stmtBranches(s.Else) + stmtBranches(lstmt, s.Else) } case *syntax.SwitchStmt: @@ -250,12 +250,12 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab } case *syntax.ForStmt: - stmtBranches(s.Body) + stmtBranches(lstmt, s.Body) } } for _, s := range list { - stmtBranches(s) + stmtBranches(nil, s) } return fwdJumps From 7c2b740da6d6e94ac8787f04ad8942f3776ac56c Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Tue, 24 Dec 2024 08:17:33 +0100 Subject: [PATCH 4/4] add doubled labels test case Change-Id: I82d3c5c2ef73e026347e94df9a499ba714f5e971 --- .../types/testdata/check/doubled_labels.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/internal/types/testdata/check/doubled_labels.go diff --git a/src/internal/types/testdata/check/doubled_labels.go b/src/internal/types/testdata/check/doubled_labels.go new file mode 100644 index 00000000000000..f3de27020ba35b --- /dev/null +++ b/src/internal/types/testdata/check/doubled_labels.go @@ -0,0 +1,26 @@ +// Copyright 2014 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 _() { +outer: +inner: + for { + continue inner + break inner + } + goto outer +} + +func _() { +outer: +inner: + for { + continue inner + continue outer /* ERROR "invalid continue label outer" */ + break outer /* ERROR "invalid break label outer" */ + } + goto outer +}