Skip to content

[WIP] Generic type nesting #1368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 59 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
72498bf
Adding test to demonstrate issue in nesting
grantnelson-wf Apr 15, 2025
e82fa78
working on identifying where nesting types should get injected
grantnelson-wf Apr 23, 2025
d81a16a
Merge branch 'master' of github.com:gopherjs/gopherjs into genNestedT…
grantnelson-wf Apr 24, 2025
7f949b7
trying to inject nesting types into visitor
grantnelson-wf Apr 25, 2025
624f159
trying to inject nesting types into visitor
grantnelson-wf Apr 25, 2025
9783473
Updating subst
grantnelson-wf May 1, 2025
6581339
Using TypeParamLists
grantnelson-wf May 2, 2025
79a034b
working on nesting types
grantnelson-wf May 5, 2025
e671c77
working on nesting types
grantnelson-wf May 5, 2025
c642f18
working on nesting types
grantnelson-wf May 5, 2025
7e10671
working on nesting types
grantnelson-wf May 6, 2025
0991c16
working on nesting types
grantnelson-wf May 6, 2025
b706b60
working on nesting types
grantnelson-wf May 6, 2025
329e497
working on nesting types
grantnelson-wf May 6, 2025
a94000c
working on nesting types
grantnelson-wf May 6, 2025
c477608
working on nesting types
grantnelson-wf May 6, 2025
16089bc
working on nesting types
grantnelson-wf May 6, 2025
963fe43
working on nesting types
grantnelson-wf May 6, 2025
8073c31
working on nesting types
grantnelson-wf May 7, 2025
c70f619
working on nesting types
grantnelson-wf May 7, 2025
d280c4e
working on nesting types
grantnelson-wf May 7, 2025
d17b6d2
working on nesting types
grantnelson-wf May 7, 2025
925c2bc
working on nesting types
grantnelson-wf May 7, 2025
9cf91d8
working on nesting types
grantnelson-wf May 8, 2025
adcc226
working on nesting types
grantnelson-wf May 8, 2025
23bf10f
working on nesting types
grantnelson-wf May 8, 2025
65600a4
working on nesting types
grantnelson-wf May 9, 2025
2774069
working on nesting types
grantnelson-wf May 9, 2025
f838cea
working on nesting types
grantnelson-wf May 9, 2025
fc32f21
working on nesting types
grantnelson-wf May 9, 2025
f554343
working on nesting types
grantnelson-wf May 9, 2025
79ea77f
working on nesting types
grantnelson-wf May 9, 2025
e8387aa
working on nesting types
grantnelson-wf May 9, 2025
acc62b7
working on nesting types
grantnelson-wf May 9, 2025
9ffd9e4
working on nesting types
grantnelson-wf May 9, 2025
0a8fb71
working on nesting types
grantnelson-wf May 12, 2025
79abcdd
fixing issues in nesting
grantnelson-wf May 12, 2025
b20cb21
fixing some issues in nesting
grantnelson-wf May 14, 2025
7982109
Merge branch 'nestedTypes' of github.com:Workiva/gopherjs into genNes…
grantnelson-wf May 14, 2025
f2aa738
fixing some issues in nesting
grantnelson-wf May 14, 2025
479ff41
Working on resolver improvements
grantnelson-wf May 20, 2025
b00dc85
Working on resolver improvements
grantnelson-wf May 20, 2025
b788e19
Working on resolver improvements
grantnelson-wf May 20, 2025
6a4f2ca
Working on resolver improvements
grantnelson-wf May 20, 2025
51c6a03
Working on resolver improvements
grantnelson-wf May 21, 2025
576e45d
Working on resolver improvements
grantnelson-wf May 21, 2025
67f9293
Merge branch 'master' of github.com:gopherjs/gopherjs into genNestedT…
grantnelson-wf May 22, 2025
7fad33d
Working on resolver improvements
grantnelson-wf May 22, 2025
5ad861b
Working on resolver improvements
grantnelson-wf May 22, 2025
f5ca11f
Handling deep nested types
grantnelson-wf May 28, 2025
fae1ff3
Merge branch 'nestedTypes2' of github.com:Workiva/gopherjs into genNe…
grantnelson-wf May 29, 2025
3426eb5
clean up after merge
grantnelson-wf May 29, 2025
6700467
working on DCE
grantnelson-wf May 29, 2025
d4d3cf7
Updated subst to v0.33.0
grantnelson-wf May 29, 2025
eccb518
Merge branch 'nestedTypes2' of github.com:Workiva/gopherjs into genNe…
grantnelson-wf May 30, 2025
a11512a
Working on DCE filter
grantnelson-wf May 30, 2025
ff9d0bd
Working on DCE filter
grantnelson-wf May 30, 2025
2e9d8b8
Working on DCE filter
grantnelson-wf Jun 2, 2025
d7490d2
Working on DCE filter
grantnelson-wf Jun 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
working on nesting types
  • Loading branch information
grantnelson-wf committed May 6, 2025
commit 963fe43e02d0a1750af48c1f040ed91659c06f8c
33 changes: 28 additions & 5 deletions compiler/internal/typeparams/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ func (c *visitor) Visit(n ast.Node) (w ast.Visitor) {
return
}

if instance, ok := c.info.Instances[ident]; ok {
if inst, ok := c.info.Instances[ident]; ok {
// Found instance of a generic type or function.
obj := c.info.ObjectOf(ident)
c.addInstance(obj, instance.TypeArgs)
c.addInstance(obj, inst.TypeArgs)
return
}

if len(c.resolver.TypeArgs()) > 0 {
if obj, ok := c.info.Uses[ident]; ok {
// Found instance of a concrete type or function in a generic context.
if obj, ok := c.info.Defs[ident]; ok && isConcreteType(obj) {
// Found instance of a concrete type defined inside a generic context.
c.addInstance(obj, nil)
return
}
Expand All @@ -174,6 +174,20 @@ func (c *visitor) Visit(n ast.Node) (w ast.Visitor) {
return
}

// isConcreteType returns true if the object is a non-generic named type.
func isConcreteType(obj types.Object) bool {
if obj == nil {
return false
}
typ := obj.Type()
if ptr, ok := typ.(*types.Pointer); ok {
typ = ptr.Elem()
}

t, ok := typ.(*types.Named)
return ok && t.TypeParams().Len() == 0
}

func (c *visitor) addInstance(obj types.Object, tArgs *types.TypeList) {
// For types embedded in structs, the object the identifier resolves to is a
// *types.Var representing the implicitly declared struct field. However, the
Expand Down Expand Up @@ -297,6 +311,7 @@ func (c *Collector) Scan(pkg *types.Package, files ...*ast.File) {

for iset := c.Instances.Pkg(pkg); !iset.exhausted(); {
inst, _ := iset.next()

switch typ := inst.Object.Type().(type) {
case *types.Signature:
tParams := SignatureTypeParams(typ)
Expand All @@ -306,14 +321,22 @@ func (c *Collector) Scan(pkg *types.Package, files ...*ast.File) {
info: c.Info,
}
ast.Walk(&v, objMap[inst.Object])

case *types.Named:
obj := typ.Obj()
node := objMap[obj]
if node == nil {
// Types without an entry in objMap are concrete types
// that are defined in a generic context. Skip them.
continue
}

v := visitor{
instances: c.Instances,
resolver: NewResolver(c.TContext, typ.TypeParams(), inst.TArgs, nil),
info: c.Info,
}
ast.Walk(&v, objMap[obj])
ast.Walk(&v, node)
}
}
}
17 changes: 8 additions & 9 deletions compiler/internal/typeparams/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package typeparams
import (
"fmt"
"go/types"
"strings"

"github.com/gopherjs/gopherjs/compiler/internal/symbol"
"github.com/gopherjs/gopherjs/compiler/typesutil"
Expand Down Expand Up @@ -54,21 +55,19 @@ func (i *Instance) qualifiedName() string {
func (i *Instance) typeParamsString(open, close string) string {
hasNest := len(i.TNest) > 0
hasArgs := len(i.TArgs) > 0
tArgs := ""
buf := strings.Builder{}
if hasNest || hasArgs {
tArgs = open
buf.WriteString(open)
if hasNest {
tArgs += i.TNest.String()
if hasArgs {
tArgs += ";"
}
buf.WriteString(i.TNest.String())
buf.WriteRune(';')
}
if hasArgs {
tArgs += i.TArgs.String()
buf.WriteString(i.TArgs.String())
}
tArgs += close
buf.WriteString(close)
}
return tArgs
return buf.String()
}

// IsTrivial returns true if this is an instance of a non-generic object.
Expand Down
9 changes: 7 additions & 2 deletions internal/govendor/subst/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// type arguments.
package subst

import "go/types"
import (
"fmt"
"go/types"
)

// To simplify future updates of the borrowed code, we minimize modifications
// to it as much as possible. This file implements an exported interface to the
Expand All @@ -16,7 +19,9 @@ type Subster struct {

// New creates a new Subster with a given list of type parameters and matching args.
func New(tc *types.Context, tParams *types.TypeParamList, tArgs []types.Type) *Subster {
assert(tParams.Len() == len(tArgs), "New() argument count must match")
if tParams.Len() != len(tArgs) {
panic(fmt.Errorf(`New() argument count must match (%d != %d)`, tParams.Len(), len(tArgs)))
}

if tParams.Len() == 0 && len(tArgs) == 0 {
return nil
Expand Down
Loading