Skip to content

Commit 1fb46d2

Browse files
cuonglmheschi
authored andcommitted
[release-branch.go1.18] cmd/compile: set correct package for vars/params/results from nested instantiation
Fixes #52606 Change-Id: Ib5b2cdbdbce1d516aa10a0df349449b756f2f404 Reviewed-on: https://go-review.googlesource.com/c/go/+/398474 Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/403174
1 parent 9247bdd commit 1fb46d2

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ func (g *genInst) buildClosure(outer *ir.Func, x ir.Node) ir.Node {
417417
var dictAssign *ir.AssignStmt
418418
if outer != nil {
419419
dictVar = ir.NewNameAt(pos, typecheck.LookupNum(typecheck.LocalDictName, g.dnum))
420+
dictVar.SetSym(outer.Sym().Pkg.Lookup(dictVar.Sym().Name))
420421
g.dnum++
421422
dictVar.Class = ir.PAUTO
422423
typed(types.Types[types.TUINTPTR], dictVar)
@@ -431,6 +432,9 @@ func (g *genInst) buildClosure(outer *ir.Func, x ir.Node) ir.Node {
431432
var rcvrAssign ir.Node
432433
if rcvrValue != nil {
433434
rcvrVar = ir.NewNameAt(pos, typecheck.LookupNum(".rcvr", g.dnum))
435+
if outer != nil {
436+
rcvrVar.SetSym(outer.Sym().Pkg.Lookup(rcvrVar.Sym().Name))
437+
}
434438
g.dnum++
435439
typed(rcvrValue.Type(), rcvrVar)
436440
rcvrAssign = ir.NewAssignStmt(pos, rcvrVar, rcvrValue)
@@ -2111,6 +2115,9 @@ func startClosure(pos src.XPos, outer *ir.Func, typ *types.Type) (*ir.Func, []*t
21112115
for i := 0; i < typ.NumParams(); i++ {
21122116
t := typ.Params().Field(i).Type
21132117
arg := ir.NewNameAt(pos, typecheck.LookupNum("a", i))
2118+
if outer != nil {
2119+
arg.SetSym(outer.Sym().Pkg.Lookup(arg.Sym().Name))
2120+
}
21142121
arg.Class = ir.PPARAM
21152122
typed(t, arg)
21162123
arg.Curfn = fn
@@ -2123,6 +2130,9 @@ func startClosure(pos src.XPos, outer *ir.Func, typ *types.Type) (*ir.Func, []*t
21232130
for i := 0; i < typ.NumResults(); i++ {
21242131
t := typ.Results().Field(i).Type
21252132
result := ir.NewNameAt(pos, typecheck.LookupNum("r", i)) // TODO: names not needed?
2133+
if outer != nil {
2134+
result.SetSym(outer.Sym().Pkg.Lookup(result.Sym().Name))
2135+
}
21262136
result.Class = ir.PPARAMOUT
21272137
typed(t, result)
21282138
result.Curfn = fn

test/typeparam/issue52117.dir/a.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 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 a
6+
7+
func Compare[T int | uint](a, b T) int {
8+
return 0
9+
}
10+
11+
type Slice[T int | uint] struct{}
12+
13+
func (l Slice[T]) Comparator() func(v1, v2 T) int {
14+
return Compare[T]
15+
}

test/typeparam/issue52117.dir/b.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package b
2+
3+
import "./a"
4+
5+
func Test() {
6+
var _ a.Slice[uint]
7+
}

test/typeparam/issue52117.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compiledir -G=3
2+
3+
// Copyright 2022 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 ignored

0 commit comments

Comments
 (0)