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: identical generic types compare unequal #49245

Closed
rogpeppe opened this issue Oct 31, 2021 · 3 comments
Closed

cmd/compile: identical generic types compare unequal #49245

rogpeppe opened this issue Oct 31, 2021 · 3 comments
Labels
FrozenDueToAge generics Issue is related to generics

Comments

@rogpeppe
Copy link
Contributor

commit 8e3d5f0

This bug was found by @FiloSottile in https://go-review.googlesource.com/c/go/+/360015.

Here's a smaller testscript reproducer:

go run ./pkg1

-- go.mod --
module m

go 1.18
-- pkg1/main.go --
package main

import (
	"fmt"
	"os"
	"reflect"

	"m/pkg2"
	"m/pkg3"
)

func main() {
	i1 := pkg3.GetI()
	i2 := pkg2.Inst
	instEqual := i1 == i2
	if !instEqual {
		v1, v2 := reflect.ValueOf(i1), reflect.ValueOf(i2)
		ptrEqual := v1.Pointer() == v2.Pointer()
		typeEqual := v1.Type() == v2.Type()
		if !ptrEqual {
			fmt.Printf("pointer values are unequal\n")
		}
		if !typeEqual {
			fmt.Printf("types are not equal\n\t%v\n\t%v\n", v1.Type(), v2.Type())
		}
		os.Exit(1)
	}
}

-- pkg2/x.go --
package pkg2

import "m/pkg3"

var Inst = pkg3.GetI()

-- pkg3/x.go --
package pkg3

type I interface{}

type impl[P any] struct{
	_ byte
}

var instance = &impl[int]{}

func GetI() I {
	return instance
}

Running this prints the following:

> go run ./pkg1
[stdout]
types are not equal
	*pkg3.impl[int]
	*pkg3.impl[int]

[stderr]
exit status 1

The values are not equal when they should be. The pointer values are identical but the types are not.

@rogpeppe rogpeppe added the generics Issue is related to generics label Oct 31, 2021
@seankhliao
Copy link
Member

is this the same as #49241 ?

@mvdan
Copy link
Member

mvdan commented Oct 31, 2021

I think it's the same issue, but @rogpeppe wrote a minimal reproducer, so perhaps this is still useful. cc @randall77 @griesemer

@FiloSottile
Copy link
Contributor

Yep, merging into #49241 which has more discussion, but thank you @rogpeppe for preparing a much better issue and reproducer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge generics Issue is related to generics
Projects
None yet
Development

No branches or pull requests

5 participants