Skip to content

Commit 6a4f2ca

Browse files
Working on resolver improvements
1 parent b788e19 commit 6a4f2ca

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

compiler/internal/typeparams/collect.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
type Resolver struct {
1616
tParams *types.TypeParamList
1717
tArgs []types.Type
18+
root Instance // The root instance that this resolver is based on.
1819

1920
// subster is the substitution helper that will perform the actual
2021
// substitutions. This maybe nil when there are no substitutions but
@@ -34,6 +35,7 @@ func NewResolver(tc *types.Context, root Instance) *Resolver {
3435

3536
switch typ := root.Object.Type().(type) {
3637
case *types.Signature:
38+
fn = root.Object.(*types.Func)
3739
tParams = SignatureTypeParams(typ)
3840

3941
case *types.Named:
@@ -65,6 +67,7 @@ func NewResolver(tc *types.Context, root Instance) *Resolver {
6567
return &Resolver{
6668
tParams: tParams,
6769
tArgs: root.TArgs,
70+
root: root,
6871
subster: subst.New(tc, fn, replacements),
6972
selMemo: map[typesutil.Selection]typesutil.Selection{},
7073
}
@@ -254,7 +257,11 @@ func (c *visitor) addInstance(obj types.Object, tArgList *types.TypeList, tNest
254257
TArgs: tArgs,
255258
TNest: tNest,
256259
}.String()) // TODO(grantnelson-wf): REMOVE
257-
fmt.Printf("\t%d: %s\n", obj.Pos(), c.resolver.String()) // TODO(grantnelson-wf): REMOVE
260+
fmt.Printf("\tresolver: %s\n", c.resolver.String()) // TODO(grantnelson-wf): REMOVE
261+
fmt.Printf("\troot: %s\n", c.resolver.root.String()) // TODO(grantnelson-wf): REMOVE
262+
for i := 0; i < len(tArgs); i++ {
263+
fmt.Printf("\t%d: (%T) %s => %s\n", i, tArgs[i], tArgs[i], tArgs[i].Underlying()) // TODO(grantnelson-wf): REMOVE
264+
}
258265

259266
return
260267
}

internal/govendor/subst/export.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ func New(tc *types.Context, fn *types.Func, replacements map[*types.TypeParam]ty
2222
return nil
2323
}
2424

25-
if fn == nil {
26-
// If `fn` is nil, we create a fake function to avoid nil pointer dereference.
27-
// `fn` can be nil when we are substituting with a generic type at the package level.
28-
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
29-
fn = types.NewFunc(0, nil, `*fakeFn*`, sig)
30-
}
31-
3225
subst := makeSubster(tc, fn, nil, nil, false)
3326
subst.replacements = replacements
3427
return &Subster{impl: subst}

internal/govendor/subst/subst.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,16 @@ type subster struct {
6363
func makeSubster(ctxt *types.Context, fn *types.Func, tparams *types.TypeParamList, targs []types.Type, debug bool) *subster {
6464
assert(tparams.Len() == len(targs), "makeSubster argument count must match")
6565

66+
// GOPHERJS: Made `fn` optional so that we can use this on package level types too.
67+
var origin *types.Func
68+
if fn != nil {
69+
origin = fn.Origin()
70+
}
71+
6672
subst := &subster{
6773
replacements: make(map[*types.TypeParam]types.Type, tparams.Len()),
6874
cache: make(map[types.Type]types.Type),
69-
origin: fn.Origin(),
75+
origin: origin,
7076
ctxt: ctxt,
7177
}
7278
for i := 0; i < tparams.Len(); i++ {

internal/govendor/subst/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func changeRecv(s *types.Signature, recv *types.Var) *types.Signature {
2828
// obj must not be a method or a field.
2929
// From https://cs.opensource.google/go/x/tools/+/refs/tags/v0.33.0:go/ssa/util.go;l=145
3030
func declaredWithin(obj types.Object, fn *types.Func) bool {
31+
// GOPHERJS: Made `fn` optional so that we can use this on package level types too.
32+
if fn == nil {
33+
return false
34+
}
35+
3136
if obj.Pos() != token.NoPos {
3237
return fn.Scope().Contains(obj.Pos()) // trust the positions if they exist.
3338
}

0 commit comments

Comments
 (0)