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

cmd/go2go: infinite recursion in generics type checker #39695

Open
ianlancetaylor opened this issue Jun 18, 2020 · 2 comments
Open

cmd/go2go: infinite recursion in generics type checker #39695

ianlancetaylor opened this issue Jun 18, 2020 · 2 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@ianlancetaylor
Copy link
Contributor

Passing this program through the go2go tool produces an infinite recursion in go/types.

package main

import (
	"fmt"
	"unsafe"
)

type EmbedringConstraint(type T interface{}) interface {
	Parent() *T
}

type Deembeder(type T interface{}, ER EmbedringConstraint(T)) interface {
	Deembed(*ER) *T
}

type Embedring(type T interface{}, DE Deembeder(T, EmbedringConstraint(T))) struct {
	prev, next *Embedring(T, DE)
}

func (ring *Embedring(T, DE)) Parent() *T {
	var de DE
	return de.Deembed(ring)
}

type Foo struct {
	name  string
	ring1 Embedring(Foo, FooRing1)
	ring2 Embedring(Foo, FooRing2)
}

type FooRing1 struct{}

func (_ FooRing1) Deembed(ring *Embedring(Foo, FooRing1)) *Foo {
	return deembed(ring, unsafe.Offsetof(Foo{}.ring1))
}

type FooRing2 struct{}

func (_ FooRing2) Deembed(ring *Embedring(Foo, FooRing2)) *Foo {
	return deembed(ring, unsafe.Offsetof(Foo{}.ring2))
}

func deembed(type T interface{}, ER Embedring(T))(ring *ER, offsetof uintptr) *T {
	return (*T)(unsafe.Pointer(uintptr(unsafe.Pointer(ring)) - offsetof))
}

func main() {
	var foo Foo
	foo.name = "Hello"

	fmt.Println(foo.ring1.Parent().name)
	fmt.Println(foo.ring2.Parent().name)
}

Partial stack trace:

``` runtime: goroutine stack exceeds 1000000000-byte limit runtime: sp=0xc0205804a0 stack=[0xc020580000, 0xc040580000] fatal error: stack overflow

runtime stack:
runtime.throw(0x69320d, 0xe)
/home/iant/gogen/src/runtime/panic.go:1116 +0x72
runtime.newstack()
/home/iant/gogen/src/runtime/stack.go:1059 +0x78d
runtime.morestack()
/home/iant/gogen/src/runtime/asm_amd64.s:449 +0x8f

goroutine 1 [running]:
go/types.(*subster).typ(0xc020581128, 0x6ded40, 0xc000124bd0, 0x0, 0x0)
/home/iant/gogen/src/go/types/subst.go:239 +0x226c fp=0xc0205804b0 sp=0xc0205804a8 pc=0x56ab6c
go/types.(*subster).typ(0xc020581128, 0x6dede0, 0xc0001aee20, 0x0, 0x0)
/home/iant/gogen/src/go/types/subst.go:265 +0x2106 fp=0xc020580828 sp=0xc0205804b0 pc=0x56aa06
go/types.(*subster).var_(0xc020581128, 0xc0001ba500, 0x40d3b0)
/home/iant/gogen/src/go/types/subst.go:438 +0x5a fp=0xc020580878 sp=0xc020580828 pc=0x56ae3a
go/types.(*subster).varList(0xc020581128, 0xc000102438, 0x1, 0x1, 0xc020580948, 0x55267a, 0xc00ad2c8d0, 0x4cf699)
/home/iant/gogen/src/go/types/subst.go:459 +0x98 fp=0xc0205808e8 sp=0xc020580878 pc=0x56b0f8
go/types.(*subster).tuple(0xc020581128, 0xc0001aee40, 0xc00011e4d8)
/home/iant/gogen/src/go/types/subst.go:449 +0x65 fp=0xc020580950 sp=0xc0205808e8 pc=0x56afc5
go/types.(*subster).typ(0xc020581128, 0x6dee80, 0xc0001b3020, 0x0, 0x0)
/home/iant/gogen/src/go/types/subst.go:277 +0x154a fp=0xc020580cc8 sp=0xc020580950 pc=0x569e4a
go/types.(*subster).func_(0xc020581128, 0xc0001ba5a0, 0x0)
/home/iant/gogen/src/go/types/subst.go:476 +0x5a fp=0xc020580d18 sp=0xc020580cc8 pc=0x56b27a
go/types.(*subster).funcList(0xc020581128, 0xc000102448, 0x1, 0x1, 0x0, 0x8, 0x0, 0xc00d5eba40)
/home/iant/gogen/src/go/types/subst.go:488 +0x98 fp=0xc020580d88 sp=0xc020580d18 pc=0x56b438
go/types.(*subster).typ(0xc020581128, 0x6dec00, 0xc000130c00, 0x0, 0x0)
/home/iant/gogen/src/go/types/subst.go:301 +0x175e fp=0xc020581100 sp=0xc020580d88 pc=0x56a05e
go/types.(*Checker).subst(0xc0001265a0, 0x246, 0x6dec00, 0xc000130c00, 0xc00d692d00, 0x2, 0xc00d692d00)
/home/iant/gogen/src/go/types/subst.go:229 +0x11b fp=0xc020581158 sp=0xc020581100 pc=0x56885b
go/types.(*Checker).instantiate(0xc0001265a0, 0x237, 0x6ded40, 0xc000124b40, 0xc0001af4e0, 0x2, 0x2, 0xc00011eff0, 0x2, 0x2, ...)
/home/iant/gogen/src/go/types/subst.go:138 +0x29f fp=0xc020581350 sp=0xc020581158 pc=0x5678bf
go/types.(*instance).expand(0xc0001b33e0, 0x0, 0x0)
/home/iant/gogen/src/go/types/type.go:890 +0xed fp=0xc0205813c0 sp=0xc020581350 pc=0x56e12d
go/types.expand(...)
/home/iant/gogen/src/go/types/type.go:908
go/types.(*unifier).nify(0xc00d682b40, 0x6df2e0, 0xc0001b33e0, 0x6ded40, 0xc000125560, 0x0, 0x0)
/home/iant/gogen/src/go/types/unify.go:94 +0x17c6 fp=0xc020581568 sp=0xc0205813c0 pc=0x57bfa6
go/types.(*unifier).nify(0xc00d682b40, 0x6dede0, 0xc0001af4c0, 0x6dede0, 0xc00d692c60, 0x0, 0xc0205817a8)
/home/iant/gogen/src/go/types/unify.go:233 +0x106a fp=0xc020581710 sp=0xc020581568 pc=0x57b84a
go/types.(*unifier).nify(0xc00d682b40, 0x6df100, 0xc0001af560, 0x6df100, 0xc00d692c80, 0x0, 0x0)
/home/iant/gogen/src/go/types/unify.go:244 +0xd87 fp=0xc0205818b8 sp=0xc020581710 pc=0x57b567
go/types.(*unifier).nify(0xc00d682b40, 0x6dee80, 0xc0001b3380, 0x6dee80, 0xc00eb66000, 0x0, 0xc00d682b40)
/home/iant/gogen/src/go/types/unify.go:261 +0xbbf fp=0xc020581a60 sp=0xc0205818b8 pc=0x57b39f
go/types.(*unifier).unify(0xc00d682b40, 0x6dee80, 0xc0001b3380, 0x6dee80, 0xc00eb66000, 0xc00011e439)
/home/iant/gogen/src/go/types/unify.go:38 +0x5c fp=0xc020581aa8 sp=0xc020581a60 pc=0x57a7bc
go/types.(*Checker).missingMethod(0xc0001265a0, 0x6ded40, 0xc000125320, 0xc00d66da00, 0xc00d692c01, 0x6dec00, 0xc00d66da00)
/home/iant/gogen/src/go/types/lookup.go:409 +0x305 fp=0xc020581ba8 sp=0xc020581aa8 pc=0x5508e5
go/types.(*Checker).instantiate(0xc0001265a0, 0x237, 0x6ded40, 0xc000124b40, 0xc0001af4e0, 0x2, 0x2, 0xc00011eff0, 0x2, 0x2, ...)
/home/iant/gogen/src/go/types/subst.go:164 +0x896 fp=0xc020581da0 sp=0xc020581ba8 pc=0x567eb6
go/types.(*instance).expand(0xc0001b33e0, 0x0, 0x0)
/home/iant/gogen/src/go/types/type.go:890 +0xed fp=0xc020581e10 sp=0xc020581da0 pc=0x56e12d
go/types.expand(...)
/home/iant/gogen/src/go/types/type.go:908
go/types.(*unifier).nify(0xc00d682aa0, 0x6df2e0, 0xc0001b33e0, 0x6ded40, 0xc000125560, 0x0, 0x0)
/home/iant/gogen/src/go/types/unify.go:94 +0x17c6 fp=0xc020581fb8 sp=0xc020581e10 pc=0x57bfa6
...

</details>
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 18, 2020
@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Jun 18, 2020
@beoran
Copy link

beoran commented Jun 18, 2020

Perhaps this is related to #39688?

@griesemer
Copy link
Contributor

Not related to #39688 .

Simpler reproducer:

package p

type D interface {
	m(*int)
}

type E(type _ D) struct {}

type F struct{}

func (F) m(*E(F))

The code type-checks if we remove the two *.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants