Skip to content

Commit d0f73ac

Browse files
authored
Merge pull request github#17709 from owen-mc/go/extractor/objecttypes-consistency-generics
Go: extractor/objecttypes consistency generics
2 parents 5f353b7 + 513efe2 commit d0f73ac

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

go/extractor/extractor.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ func extractMethod(tw *trap.Writer, meth *types.Func) trap.Label {
518518
// For more information on objects, see:
519519
// https://github.com/golang/example/blob/master/gotypes/README.md#objects
520520
func extractObject(tw *trap.Writer, obj types.Object, lbl trap.Label) {
521+
checkObjectNotSpecialized(obj)
521522
name := obj.Name()
522523
isBuiltin := obj.Parent() == types.Universe
523524
var kind int
@@ -1637,7 +1638,7 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
16371638
// Note that methods coming from embedded interfaces can be
16381639
// accessed through `Method(i)`, so there is no need to
16391640
// deal with them separately.
1640-
meth := tp.Method(i)
1641+
meth := tp.Method(i).Origin()
16411642

16421643
// Note that methods do not have a parent scope, so they are
16431644
// not dealt with by `extractScopes`
@@ -1707,15 +1708,15 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
17071708
// ensure all methods have labels - note that methods do not have a
17081709
// parent scope, so they are not dealt with by `extractScopes`
17091710
for i := 0; i < origintp.NumMethods(); i++ {
1710-
meth := origintp.Method(i)
1711+
meth := origintp.Method(i).Origin()
17111712

17121713
extractMethod(tw, meth)
17131714
}
17141715

17151716
// associate all methods of underlying interface with this type
17161717
if underlyingInterface, ok := underlying.(*types.Interface); ok {
17171718
for i := 0; i < underlyingInterface.NumMethods(); i++ {
1718-
methlbl := extractMethod(tw, underlyingInterface.Method(i))
1719+
methlbl := extractMethod(tw, underlyingInterface.Method(i).Origin())
17191720
dbscheme.MethodHostsTable.Emit(tw, methlbl, lbl)
17201721
}
17211722
}
@@ -1787,7 +1788,7 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
17871788
case *types.Interface:
17881789
var b strings.Builder
17891790
for i := 0; i < tp.NumMethods(); i++ {
1790-
meth := tp.Method(i)
1791+
meth := tp.Method(i).Origin()
17911792
methLbl := extractType(tw, meth.Type())
17921793
if i > 0 {
17931794
b.WriteString(",")
@@ -2143,3 +2144,20 @@ func skipExtractingValueForLeftOperand(tw *trap.Writer, be *ast.BinaryExpr) bool
21432144
}
21442145
return true
21452146
}
2147+
2148+
// checkObjectNotSpecialized exits the program if `obj` is specialized. Note
2149+
// that specialization is only possible for function objects and variable
2150+
// objects.
2151+
func checkObjectNotSpecialized(obj types.Object) {
2152+
if funcObj, ok := obj.(*types.Func); ok && funcObj != funcObj.Origin() {
2153+
log.Fatalf("Encountered unexpected specialization %s of generic function object %s", funcObj.FullName(), funcObj.Origin().FullName())
2154+
}
2155+
if varObj, ok := obj.(*types.Var); ok && varObj != varObj.Origin() {
2156+
log.Fatalf("Encountered unexpected specialization %s of generic variable object %s", varObj.String(), varObj.Origin().String())
2157+
}
2158+
if typeNameObj, ok := obj.(*types.TypeName); ok {
2159+
if namedType, ok := typeNameObj.Type().(*types.Named); ok && namedType != namedType.Origin() {
2160+
log.Fatalf("Encountered type object for specialization %s of named type %s", namedType.String(), namedType.Origin().String())
2161+
}
2162+
}
2163+
}

0 commit comments

Comments
 (0)