Skip to content
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

support nested type-parameterized declarations #200

Merged
merged 13 commits into from
Jan 30, 2023
6 changes: 4 additions & 2 deletions cmd/igoptest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func init() {

gorootTestSkips["typeparam/chans.go"] = "runtime.SetFinalizer"
// gorootTestSkips["typeparam/issue376214.go"] = "build SSA package error: variadic parameter must be of unnamed slice type"
gorootTestSkips["typeparam/nested.go"] = "FAIL"
if ver != "go1.20" {
gorootTestSkips["typeparam/nested.go"] = "skip, run pass but output same as go1.20"
}
//go1.20
gorootTestSkips["fixedbugs/bug514.go"] = "skip cgo"
gorootTestSkips["fixedbugs/issue40954.go"] = "skip cgo"
Expand Down Expand Up @@ -178,7 +180,7 @@ func getGorootTestRuns() (dir string, run []runfile, runoutput []string) {
case "typeparam":
ver := runtime.Version()[:6]
switch ver {
case "go1.18", "go1.19":
case "go1.18", "go1.19", "go1.20":
return nil
default:
return filepath.SkipDir
Expand Down
28 changes: 28 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Context struct {
pkgs map[string]*sourcePackage // imports
override map[string]reflect.Value // override function
evalInit map[string]bool // eval init check
nestedMap map[*types.Named]int // nested named index
root string // project root
callForPool int // least call count for enable function pool
Mode Mode // mode
Expand Down Expand Up @@ -103,6 +104,7 @@ func NewContext(mode Mode) *Context {
BuildContext: build.Default,
pkgs: make(map[string]*sourcePackage),
override: make(map[string]reflect.Value),
nestedMap: make(map[*types.Named]int),
callForPool: 64,
}
if mode&EnableDumpInstr != 0 {
Expand Down Expand Up @@ -576,6 +578,7 @@ func (ctx *Context) buildPackage(sp *sourcePackage) (pkg *ssa.Package, err error
}
}
prog.CreatePackage(p, pkg.Files, pkg.Info, true).Build()
ctx.checkNested(pkg.Info)
} else {
var indirect bool
if !p.Complete() {
Expand Down Expand Up @@ -617,9 +620,34 @@ func (ctx *Context) buildPackage(sp *sourcePackage) (pkg *ssa.Package, err error
// Create and build the primary package.
pkg = prog.CreatePackage(sp.Package, sp.Files, sp.Info, false)
pkg.Build()
ctx.checkNested(sp.Info)
return
}

func (ctx *Context) checkNested(info *types.Info) {
var nestedList []*types.Named
for k, v := range info.Scopes {
switch k.(type) {
case *ast.BlockStmt, *ast.FuncType:
for _, name := range v.Names() {
obj := v.Lookup(name)
if named, ok := obj.Type().(*types.Named); ok {
nestedList = append(nestedList, named)
}
}
}
}
if len(nestedList) == 0 {
return
}
sort.Slice(nestedList, func(i, j int) bool {
return nestedList[i].Obj().Pos() < nestedList[j].Obj().Pos()
})
for i, named := range nestedList {
ctx.nestedMap[named] = i + 1
}
}

func RunFile(filename string, src interface{}, args []string, mode Mode) (exitCode int, err error) {
ctx := NewContext(mode)
return ctx.RunFile(filename, src, args)
Expand Down
25 changes: 13 additions & 12 deletions interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ func (p *_panic) isNil() bool {

// runDefer runs a deferred call d.
// It always returns normally, but may set or clear fr.panic.
//
func (fr *frame) runDefer(d *_defer) (ok bool) {
defer func() {
if !ok {
Expand Down Expand Up @@ -359,7 +358,6 @@ func (fr *frame) runDefer(d *_defer) (ok bool) {
//
// If there was no initial state of panic, or it was recovered from,
// runDefers returns normally.
//
func (fr *frame) runDefers() {
interp := fr.pfn.Interp
atomic.AddInt32(&interp.deferCount, 1)
Expand Down Expand Up @@ -429,7 +427,6 @@ func (i *DebugInfo) AsFunc() (*types.Func, bool) {
// prepareCall determines the function value and argument values for a
// function call in a Call, Go or Defer instruction, performing
// interface method lookup if needed.
//
func (i *Interp) prepareCall(fr *frame, call *ssa.CallCommon, iv register, ia []register, ib []register) (fv value, args []value) {
if call.Method == nil {
switch f := call.Value.(type) {
Expand Down Expand Up @@ -493,7 +490,6 @@ func (i *Interp) prepareCall(fr *frame, call *ssa.CallCommon, iv register, ia []
// call interprets a call to a function (function, builtin or closure)
// fn with arguments args, returning its result.
// callpos is the position of the callsite.
//
func (i *Interp) call(caller *frame, fn value, args []value, ssaArgs []ssa.Value) value {
switch fn := fn.(type) {
case *ssa.Function:
Expand All @@ -512,7 +508,6 @@ func (i *Interp) call(caller *frame, fn value, args []value, ssaArgs []ssa.Value
// call interprets a call to a function (function, builtin or closure)
// fn with arguments args, returning its result.
// callpos is the position of the callsite.
//
func (i *Interp) callDiscardsResult(caller *frame, fn value, args []value, ssaArgs []ssa.Value) {
switch fn := fn.(type) {
case *ssa.Function:
Expand Down Expand Up @@ -894,7 +889,6 @@ func (i *Interp) callExternalWithFrameByStack(caller *frame, fn reflect.Value, i
// After a recovered panic in a function with NRPs, fr.result is
// undefined and fr.block contains the block at which to resume
// control.
//
func (fr *frame) run() {
if fr.pfn.Recover != nil {
defer func() {
Expand Down Expand Up @@ -997,7 +991,7 @@ func newInterp(ctx *Context, mainpkg *ssa.Package, globals map[string]interface{
chexit: make(chan int),
mainid: goroutineID(),
}
i.record = NewTypesRecord(i.ctx.Loader, i)
i.record = NewTypesRecord(ctx.Loader, i, ctx.nestedMap)
i.record.Load(mainpkg)

var pkgs []*ssa.Package
Expand Down Expand Up @@ -1034,17 +1028,23 @@ func newInterp(ctx *Context, mainpkg *ssa.Package, globals map[string]interface{

func (i *Interp) loadType(typ types.Type) {
if _, ok := i.preloadTypes[typ]; !ok {
i.preloadTypes[typ] = i.record.ToType(typ)
rt, nested := i.record.ToType(typ)
if nested {
return
}
i.preloadTypes[typ] = rt
}
}

func (i *Interp) preToType(typ types.Type) reflect.Type {
if t, ok := i.preloadTypes[typ]; ok {
return t
}
t := i.record.ToType(typ)
i.preloadTypes[typ] = t
return t
rt, nested := i.record.ToType(typ)
if !nested {
i.preloadTypes[typ] = rt
}
return rt
}

func (i *Interp) toType(typ types.Type) reflect.Type {
Expand All @@ -1054,7 +1054,8 @@ func (i *Interp) toType(typ types.Type) reflect.Type {
// log.Panicf("toType %v %p\n", typ, typ)
i.typesMutex.Lock()
defer i.typesMutex.Unlock()
return i.record.ToType(typ)
rt, _ := i.record.ToType(typ)
return rt
}

func (i *Interp) RunFunc(name string, args ...Value) (r Value, err error) {
Expand Down
158 changes: 158 additions & 0 deletions typeparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package igop_test

import (
"bytes"
"fmt"
"testing"

"github.com/goplus/igop"
Expand Down Expand Up @@ -38,3 +40,159 @@ func main() {
t.Fatal(err)
}
}

func TestNestedTypeParameterized(t *testing.T) {
src := `// run

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This test case stress tests a number of subtle cases involving
// nested type-parameterized declarations. At a high-level, it
// declares a generic function that contains a generic type
// declaration:
//
// func F[A intish]() {
// type T[B intish] struct{}
//
// // store reflect.Type tuple (A, B, F[A].T[B]) in tests
// }
//
// It then instantiates this function with a variety of type arguments
// for A and B. Particularly tricky things like shadowed types.
//
// From this data it tests two things:
//
// 1. Given tuples (A, B, F[A].T[B]) and (A', B', F[A'].T[B']),
// F[A].T[B] should be identical to F[A'].T[B'] iff (A, B) is
// identical to (A', B').
//
// 2. A few of the instantiations are constructed to be identical, and
// it tests that exactly these pairs are duplicated (by golden
// output comparison to nested.out).
//
// In both cases, we're effectively using the compiler's existing
// runtime.Type handling (which is well tested) of type identity of A
// and B as a way to help bootstrap testing and validate its new
// runtime.Type handling of F[A].T[B].
//
// This isn't perfect, but it smoked out a handful of issues in
// gotypes2 and unified IR.

package main

import (
"fmt"
"reflect"
)

type test struct {
TArgs [2]reflect.Type
Instance reflect.Type
}

var tests []test

type intish interface{ ~int }

type Int int
type GlobalInt = Int // allow access to global Int, even when shadowed

func F[A intish]() {
add := func(B, T interface{}) {
tests = append(tests, test{
TArgs: [2]reflect.Type{
reflect.TypeOf(A(0)),
reflect.TypeOf(B),
},
Instance: reflect.TypeOf(T),
})
}

type Int int

type T[B intish] struct{}

add(int(0), T[int]{})
add(Int(0), T[Int]{})
add(GlobalInt(0), T[GlobalInt]{})
add(A(0), T[A]{}) // NOTE: intentionally dups with int and GlobalInt

type U[_ any] int
type V U[int]
type W V

add(U[int](0), T[U[int]]{})
add(U[Int](0), T[U[Int]]{})
add(U[GlobalInt](0), T[U[GlobalInt]]{})
add(U[A](0), T[U[A]]{}) // NOTE: intentionally dups with U[int] and U[GlobalInt]
add(V(0), T[V]{})
add(W(0), T[W]{})
}

func main() {
type Int int

F[int]()
F[Int]()
F[GlobalInt]()

type U[_ any] int
type V U[int]
type W V

F[U[int]]()
F[U[Int]]()
F[U[GlobalInt]]()
F[V]()
F[W]()

type X[A any] U[X[A]]

F[X[int]]()
F[X[Int]]()
F[X[GlobalInt]]()

for j, tj := range tests {
for i, ti := range tests[:j+1] {
if (ti.TArgs == tj.TArgs) != (ti.Instance == tj.Instance) {
fmt.Printf("FAIL: %d,%d: %s, but %s\n", i, j, eq(ti.TArgs, tj.TArgs), eq(ti.Instance, tj.Instance))
}

// The test is constructed so we should see a few identical types.
// See "NOTE" comments above.
if i != j && ti.Instance == tj.Instance {
fmt.Printf("%d,%d: %v\n", i, j, ti.Instance)
}
}
}
}

func eq(a, b interface{}) string {
op := "=="
if a != b {
op = "!="
}
return fmt.Sprintf("%v %s %v", a, op, b)
}
`
out := `0,3: main.T[int;int]
4,7: main.T[int;main.U[int;int]·3]
22,23: main.T[main.Int;main.Int]
26,27: main.T[main.Int;main.U[main.Int;main.Int]·3]
`
ctx := igop.NewContext(0)
var buf bytes.Buffer
ctx.SetOverrideFunction("fmt.Printf", func(format string, a ...interface{}) (n int, err error) {
fmt.Fprintf(&buf, format, a...)
return fmt.Printf(format, a...)
})
_, err := ctx.RunFile("main.go", src, nil)
if err != nil {
t.Fatal(err)
}
if buf.String() != out {
t.Fatal("error output", buf.String())
}
}
29 changes: 27 additions & 2 deletions types_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package igop
import (
"go/ast"
"go/types"
"reflect"

"golang.org/x/tools/go/ssa"
)

const (
Expand All @@ -16,15 +19,37 @@ func hasTypeParam(t types.Type) bool {
return false
}

func extractNamed(named *types.Named) (pkgpath string, name string) {
type nestedStack struct {
}

func (r *TypesRecord) EnterInstance(fn *ssa.Function) {
}
func (r *TypesRecord) LeaveInstance(fn *ssa.Function) {
}

func (r *TypesRecord) extractNamed(named *types.Named, totype bool) (pkgpath string, name string, typeargs bool, nested bool) {
obj := named.Obj()
if pkg := obj.Pkg(); pkg != nil {
pkgpath = pkg.Path()
if pkg.Name() == "main" {
pkgpath = "main"
} else {
pkgpath = pkg.Path()
}
}
name = obj.Name()
return
}

func (r *TypesRecord) LookupReflect(typ types.Type) (rt reflect.Type, ok bool, nested bool) {
rt, ok = r.loader.LookupReflect(typ)
if !ok {
if rt := r.tcache.At(typ); rt != nil {
return rt.(reflect.Type), true, false
}
}
return
}

func (sp *sourcePackage) Load() (err error) {
if sp.Info == nil {
sp.Info = &types.Info{
Expand Down
Loading