Skip to content

Commit aa8d4df

Browse files
mateusz834gopherbot
authored andcommitted
go/types: propagate *ast.LabeledStmt in blockBranches properly
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]>
1 parent e4b12eb commit aa8d4df

File tree

4 files changed

+71
-18
lines changed

4 files changed

+71
-18
lines changed

Diff for: src/cmd/compile/internal/types2/labels.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab
112112
return varDeclPos.IsKnown() && slices.Contains(badJumps, jmp)
113113
}
114114

115-
var stmtBranches func(syntax.Stmt)
116-
stmtBranches = func(s syntax.Stmt) {
115+
var stmtBranches func(*syntax.LabeledStmt, syntax.Stmt)
116+
stmtBranches = func(lstmt *syntax.LabeledStmt, s syntax.Stmt) {
117117
switch s := s.(type) {
118118
case *syntax.DeclStmt:
119119
for _, d := range s.DeclList {
@@ -163,7 +163,7 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab
163163
fwdJumps = fwdJumps[:i]
164164
lstmt = s
165165
}
166-
stmtBranches(s.Stmt)
166+
stmtBranches(lstmt, s.Stmt)
167167

168168
case *syntax.BranchStmt:
169169
if s.Label == nil {
@@ -232,9 +232,9 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab
232232
fwdJumps = append(fwdJumps, check.blockBranches(all, b, lstmt, s.List)...)
233233

234234
case *syntax.IfStmt:
235-
stmtBranches(s.Then)
235+
stmtBranches(lstmt, s.Then)
236236
if s.Else != nil {
237-
stmtBranches(s.Else)
237+
stmtBranches(lstmt, s.Else)
238238
}
239239

240240
case *syntax.SwitchStmt:
@@ -250,12 +250,12 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab
250250
}
251251

252252
case *syntax.ForStmt:
253-
stmtBranches(s.Body)
253+
stmtBranches(lstmt, s.Body)
254254
}
255255
}
256256

257257
for _, s := range list {
258-
stmtBranches(s)
258+
stmtBranches(nil, s)
259259
}
260260

261261
return fwdJumps

Diff for: src/go/types/labels.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *ast.Labele
119119
fwdJumps = append(fwdJumps, check.blockBranches(all, b, lstmt, list)...)
120120
}
121121

122-
var stmtBranches func(ast.Stmt)
123-
stmtBranches = func(s ast.Stmt) {
122+
var stmtBranches func(*ast.LabeledStmt, ast.Stmt)
123+
stmtBranches = func(lstmt *ast.LabeledStmt, s ast.Stmt) {
124124
switch s := s.(type) {
125125
case *ast.DeclStmt:
126126
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
168168
fwdJumps = fwdJumps[:i]
169169
lstmt = s
170170
}
171-
stmtBranches(s.Stmt)
171+
stmtBranches(lstmt, s.Stmt)
172172

173173
case *ast.BranchStmt:
174174
if s.Label == nil {
@@ -235,36 +235,36 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *ast.Labele
235235
blockBranches(lstmt, s.List)
236236

237237
case *ast.IfStmt:
238-
stmtBranches(s.Body)
238+
stmtBranches(lstmt, s.Body)
239239
if s.Else != nil {
240-
stmtBranches(s.Else)
240+
stmtBranches(lstmt, s.Else)
241241
}
242242

243243
case *ast.CaseClause:
244244
blockBranches(nil, s.Body)
245245

246246
case *ast.SwitchStmt:
247-
stmtBranches(s.Body)
247+
stmtBranches(lstmt, s.Body)
248248

249249
case *ast.TypeSwitchStmt:
250-
stmtBranches(s.Body)
250+
stmtBranches(lstmt, s.Body)
251251

252252
case *ast.CommClause:
253253
blockBranches(nil, s.Body)
254254

255255
case *ast.SelectStmt:
256-
stmtBranches(s.Body)
256+
stmtBranches(lstmt, s.Body)
257257

258258
case *ast.ForStmt:
259-
stmtBranches(s.Body)
259+
stmtBranches(lstmt, s.Body)
260260

261261
case *ast.RangeStmt:
262-
stmtBranches(s.Body)
262+
stmtBranches(lstmt, s.Body)
263263
}
264264
}
265265

266266
for _, s := range list {
267-
stmtBranches(s)
267+
stmtBranches(nil, s)
268268
}
269269

270270
return fwdJumps

Diff for: src/internal/types/testdata/check/doubled_labels.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2014 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
func _() {
8+
outer:
9+
inner:
10+
for {
11+
continue inner
12+
break inner
13+
}
14+
goto outer
15+
}
16+
17+
func _() {
18+
outer:
19+
inner:
20+
for {
21+
continue inner
22+
continue outer /* ERROR "invalid continue label outer" */
23+
break outer /* ERROR "invalid break label outer" */
24+
}
25+
goto outer
26+
}

Diff for: src/internal/types/testdata/check/issue70974.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2014 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
func _() {
8+
outer:
9+
for {
10+
break outer
11+
}
12+
13+
for {
14+
break outer /* ERROR "invalid break label outer" */
15+
}
16+
}
17+
18+
func _() {
19+
outer:
20+
for {
21+
continue outer
22+
}
23+
24+
for {
25+
continue outer /* ERROR "invalid continue label outer" */
26+
}
27+
}

0 commit comments

Comments
 (0)