Skip to content

Commit 51dec25

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/golang: highlight typeswitch break correctly
Fixes golang/go#65752 Change-Id: I99a44c85c92615fbf3f4b1d34ab63a24454c8e85 Reviewed-on: https://go-review.googlesource.com/c/tools/+/564955 Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 3ac77cb commit 51dec25

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

gopls/internal/golang/highlight.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func highlightPath(path []ast.Node, file *ast.File, info *types.Info) (map[posRa
103103
highlightIdentifier(node, file, info, result)
104104
case *ast.ForStmt, *ast.RangeStmt:
105105
highlightLoopControlFlow(path, info, result)
106-
case *ast.SwitchStmt:
106+
case *ast.SwitchStmt, *ast.TypeSwitchStmt:
107107
highlightSwitchFlow(path, info, result)
108108
case *ast.BranchStmt:
109109
// BREAK can exit a loop, switch or select, while CONTINUE exit a loop so
@@ -309,7 +309,7 @@ func highlightUnlabeledBreakFlow(path []ast.Node, info *types.Info, result map[p
309309
case *ast.ForStmt, *ast.RangeStmt:
310310
highlightLoopControlFlow(path, info, result)
311311
return // only highlight the innermost statement
312-
case *ast.SwitchStmt:
312+
case *ast.SwitchStmt, *ast.TypeSwitchStmt:
313313
highlightSwitchFlow(path, info, result)
314314
return
315315
case *ast.SelectStmt:
@@ -331,7 +331,7 @@ func highlightLabeledFlow(path []ast.Node, info *types.Info, stmt *ast.BranchStm
331331
switch label.Stmt.(type) {
332332
case *ast.ForStmt, *ast.RangeStmt:
333333
highlightLoopControlFlow([]ast.Node{label.Stmt, label}, info, result)
334-
case *ast.SwitchStmt:
334+
case *ast.SwitchStmt, *ast.TypeSwitchStmt:
335335
highlightSwitchFlow([]ast.Node{label.Stmt, label}, info, result)
336336
}
337337
return
@@ -381,7 +381,7 @@ Outer:
381381
switch n.(type) {
382382
case *ast.ForStmt, *ast.RangeStmt:
383383
return loop == n
384-
case *ast.SwitchStmt, *ast.SelectStmt:
384+
case *ast.SwitchStmt, *ast.TypeSwitchStmt, *ast.SelectStmt:
385385
return false
386386
}
387387
b, ok := n.(*ast.BranchStmt)
@@ -434,7 +434,7 @@ Outer:
434434
// Reverse walk the path till we get to the switch statement.
435435
for i := range path {
436436
switch n := path[i].(type) {
437-
case *ast.SwitchStmt:
437+
case *ast.SwitchStmt, *ast.TypeSwitchStmt:
438438
switchNodeLabel = labelFor(path[i:])
439439
if stmtLabel == nil || switchNodeLabel == stmtLabel {
440440
switchNode = n
@@ -457,7 +457,7 @@ Outer:
457457
// Traverse AST to find break statements within the same switch.
458458
ast.Inspect(switchNode, func(n ast.Node) bool {
459459
switch n.(type) {
460-
case *ast.SwitchStmt:
460+
case *ast.SwitchStmt, *ast.TypeSwitchStmt:
461461
return switchNode == n
462462
case *ast.ForStmt, *ast.RangeStmt, *ast.SelectStmt:
463463
return false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This is a regression test for issue 65752: a break in a switch should
2+
highlight the switch, not the enclosing loop.
3+
4+
-- a.go --
5+
package a
6+
7+
func _(x any) {
8+
for {
9+
// type switch
10+
switch x.(type) { //@loc(tswitch, "switch")
11+
default:
12+
break //@highlight("break", tswitch, "break")
13+
}
14+
15+
// value switch
16+
switch { //@loc(vswitch, "switch")
17+
default:
18+
break //@highlight("break", vswitch, "break")
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)