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/compile: unexpected error from constraint type inference #53650

Open
RelicOfTesla opened this issue Jul 1, 2022 · 1 comment
Open

cmd/compile: unexpected error from constraint type inference #53650

RelicOfTesla opened this issue Jul 1, 2022 · 1 comment
Labels
compiler/runtime NeedsInvestigation
Milestone

Comments

@RelicOfTesla
Copy link

@RelicOfTesla RelicOfTesla commented Jul 1, 2022

What version of Go are you using (go version)?

$ go version
go version go1.18.3 windows/amd64

What did you do?

type BaseT interface {
	Type1 | Type2
}
type BaseType int
type Type1 BaseType
type Type2 BaseType // float64

type ValueT[T BaseT] struct {
	A1 T
}

func NewType1() *ValueT[Type1] {
	r := NewT[Type1]()
	return r
}
func NewType2() *ValueT[Type2] {
	r := NewT[Type2]()
	return r
}

func NewT[TBase BaseT, TVal ValueT[TBase]]() *TVal {
	ret := TVal{}
	return &ret
}
func TestGoType(t *testing.T) {
	r1 := NewType1()
	r2 := NewType2()
	t.Log(r1, r2)
	t.Log(reflect.TypeOf(r1), reflect.TypeOf(r2))
	fooT1(r1.A1)
	fooT2(r2.A1)
}

func fooT1(t1 Type1) {

}
func fooT2(t2 Type2) {

}

What did you expect to see?

Compile success

What did you see instead?

error:

TBase does not match int
TBase does not match int
@ianlancetaylor ianlancetaylor changed the title Type Parameters Inference dose not support same base type cmd/compile: unexpected error from constraint type inference Jul 1, 2022
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jul 1, 2022

CC @griesemer

Standalone reproducer:

package p

type BaseT interface {
	Type1 | Type2
}
type BaseType int
type Type1 BaseType
type Type2 BaseType

type ValueT[T BaseT] struct {
	A1 T
}

func NewType1() *ValueT[Type1] {
	r := NewT[Type1]()
	return r
}

func NewT[TInt BaseT, TVal ValueT[TInt]]() *TVal {
	ret := TVal{}
	return &ret
}
foo.go:15:18: TInt does not match int

@ianlancetaylor ianlancetaylor added the NeedsInvestigation label Jul 1, 2022
@ianlancetaylor ianlancetaylor added this to the Go1.20 milestone Jul 1, 2022
@gopherbot gopherbot added the compiler/runtime label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime NeedsInvestigation
Projects
Status: Triage Backlog
Development

No branches or pull requests

3 participants