Skip to content

Commit

Permalink
go/types: propagate *ast.LabeledStmt in blockBranches properly
Browse files Browse the repository at this point in the history
Fixes #70974

Change-Id: I330c0ae53dcbcdb173ab514ee94f2ca53944df09
GitHub-Last-Rev: 7c2b740
GitHub-Pull-Request: #70976
Reviewed-on: https://go-review.googlesource.com/c/go/+/638257
Reviewed-by: Michael Knyszek <[email protected]>
Auto-Submit: Alan Donovan <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Commit-Queue: Alan Donovan <[email protected]>
Reviewed-by: Alan Donovan <[email protected]>
  • Loading branch information
mateusz834 authored and gopherbot committed Feb 13, 2025
1 parent e4b12eb commit aa8d4df
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/cmd/compile/internal/types2/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions src/go/types/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/internal/types/testdata/check/doubled_labels.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions src/internal/types/testdata/check/issue70974.go
Original file line number Diff line number Diff line change
@@ -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 {
break outer /* ERROR "invalid break label outer" */
}
}

func _() {
outer:
for {
continue outer
}

for {
continue outer /* ERROR "invalid continue label outer" */
}
}

0 comments on commit aa8d4df

Please sign in to comment.