Skip to content

Commit 4fd15a3

Browse files
committed
compiler: include the scope in the type code name
Fixes #5180
1 parent 707d37a commit 4fd15a3

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

compiler/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ func getTypeCodeName(t types.Type) (string, bool) {
518518
switch t := types.Unalias(t).(type) {
519519
case *types.Named:
520520
if t.Obj().Parent() != t.Obj().Pkg().Scope() {
521-
return "named:" + t.String() + "$local", true
521+
return "named:" + t.String() + "$local:" + fmt.Sprintf("%p", t.Obj().Parent()), true
522522
}
523523
return "named:" + t.String(), false
524524
case *types.Array:

testdata/interface.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

3-
import "time"
3+
import (
4+
"reflect"
5+
"time"
6+
)
47

58
func main() {
69
thing := &Thing{"foo"}
@@ -119,6 +122,10 @@ func main() {
119122

120123
// check that type asserts to interfaces with no methods work
121124
emptyintfcrash()
125+
126+
// These are part of a test that checks that `main.Foo` can refer to 2+ different entities without getting them confused.
127+
namedFoo()
128+
namedFoo2Nested()
122129
}
123130

124131
func printItf(val interface{}) {
@@ -343,3 +350,41 @@ func emptyintfcrash() {
343350
println("x is", x.(int))
344351
}
345352
}
353+
354+
func namedFoo() {
355+
type Foo struct {
356+
A int
357+
}
358+
f1 := &Foo{}
359+
fcopy := copyOf(f1)
360+
f2 := fcopy.(*Foo)
361+
println(f2.A)
362+
}
363+
364+
func namedFoo2Nested() {
365+
type Foo struct {
366+
A *int
367+
}
368+
f1 := &Foo{}
369+
fcopy := copyOf(f1)
370+
f2 := fcopy.(*Foo)
371+
println(f2.A == nil)
372+
373+
if f2.A == nil {
374+
type Foo struct {
375+
A *int
376+
}
377+
nestedf1 := &Foo{}
378+
fcopy := copyOf(nestedf1)
379+
nestedf2 := fcopy.(*Foo)
380+
println(nestedf2.A == nil)
381+
}
382+
}
383+
384+
func copyOf(src interface{}) (dst interface{}) {
385+
t := reflect.TypeOf(src)
386+
println(t.String())
387+
dsti := reflect.New(t.Elem())
388+
dst = dsti.Interface()
389+
return dst
390+
}

testdata/interface.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ type is int
2828
type is *int
2929
type is **int
3030
x is 5
31+
*main.Foo
32+
0
33+
*main.Foo
34+
true
35+
*main.Foo
36+
true

0 commit comments

Comments
 (0)