Skip to content

Commit be571a3

Browse files
WangLeonarddanscales
authored andcommitted
cmd/compile: fix the index variable is shadowed in dictPass
The CL 349613 causes this problem. In fact, we want to use the outer i to find m.List[i], but the newly created index variable i in the nearest for range shadow the outer i. Fixes #48838. Change-Id: I10f0bd985340f9443eefaadda6fc56e4e7e9a10c Reviewed-on: https://go-review.googlesource.com/c/go/+/354549 Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Dan Scales <[email protected]> Trust: Dan Scales <[email protected]>
1 parent ebeab63 commit be571a3

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/cmd/compile/internal/noder/stencil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,9 +1298,9 @@ func (g *irgen) dictPass(info *instInfo) {
12981298
// Type switch from nonempty interface. We need a *runtime.itab
12991299
// for the dynamic type.
13001300
ix := -1
1301-
for i, ic := range info.dictInfo.itabConvs {
1301+
for j, ic := range info.dictInfo.itabConvs {
13021302
if ic == m.List[i] {
1303-
ix = info.dictInfo.startItabConv + i
1303+
ix = info.dictInfo.startItabConv + j
13041304
break
13051305
}
13061306
}

test/typeparam/issue48838.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run -gcflags=-G=3
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
func main() {
10+
check[string]()
11+
}
12+
13+
func check[T any]() {
14+
var result setter[T]
15+
switch result.(type) {
16+
case fooA[T]:
17+
case fooB[T]:
18+
}
19+
}
20+
21+
type setter[T any] interface {
22+
Set(T)
23+
}
24+
25+
type fooA[T any] struct{}
26+
27+
func (fooA[T]) Set(T) {}
28+
29+
type fooB[T any] struct{}
30+
31+
func (fooB[T]) Set(T) {}

0 commit comments

Comments
 (0)