Skip to content

Commit cff09d2

Browse files
committed
Test if Origin() works (for Var and Func)
1 parent 1767ca2 commit cff09d2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

go/extractor/extractor.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,18 @@ func populateTypeParamParents(tw *trap.Writer, typeparams *types.TypeParamList,
19461946
// some changes to the object to avoid returning objects relating to instantiated
19471947
// types.
19481948
func getObjectBeingUsed(tw *trap.Writer, ident *ast.Ident) types.Object {
1949+
obj1 := getObjectBeingUsed1(tw, ident)
1950+
obj2 := getObjectBeingUsed2(tw, ident)
1951+
if obj1 != obj2 {
1952+
log.Fatalf("different results!\nobj1 = %s\nobj2=%s\n", obj1.String(), obj2.String())
1953+
}
1954+
return obj2
1955+
}
1956+
1957+
// getobjectBeingUsed looks up `ident` in `tw.Package.TypesInfo.Uses` and makes
1958+
// some changes to the object to avoid returning objects relating to instantiated
1959+
// types.
1960+
func getObjectBeingUsed1(tw *trap.Writer, ident *ast.Ident) types.Object {
19491961
obj := tw.Package.TypesInfo.Uses[ident]
19501962
if obj == nil {
19511963
return nil
@@ -1990,6 +2002,17 @@ func getObjectBeingUsed(tw *trap.Writer, ident *ast.Ident) types.Object {
19902002
return obj
19912003
}
19922004

2005+
func getObjectBeingUsed2(tw *trap.Writer, ident *ast.Ident) types.Object {
2006+
switch obj := tw.Package.TypesInfo.Uses[ident].(type) {
2007+
case *types.Var:
2008+
return obj.Origin()
2009+
case *types.Func:
2010+
return obj.Origin()
2011+
default:
2012+
return obj
2013+
}
2014+
}
2015+
19932016
// tryGetGenericType returns the generic type of `tp`, and a boolean indicating
19942017
// whether it is the same as `tp`.
19952018
func tryGetGenericType(tp types.Type) (*types.Named, bool) {

0 commit comments

Comments
 (0)