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

x/exp/apidiff: incorrect behaviour with user defined type constraints #60911

Closed
MadhavJivrajani opened this issue Jun 21, 2023 · 4 comments
Closed
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@MadhavJivrajani
Copy link

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

$ go version
go version go1.20.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes.
Also reproducible with master.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/mjivrajani/Library/Caches/go-build"
GOENV="/Users/mjivrajani/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/mjivrajani/gocode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/mjivrajani/gocode"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/r7/f6kyp7vs0sv90105pjh1t8340000gp/T/go-build3338208334=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

We run apidiff as part of our CI in kubernetes/utils.
Recently kubernetes/utils#243 was merged which introduced a type constraint called ordered.
(comparable is what was hoped to be used but since we weren't able to bump to go1.20 back then, we couldn't use it just yet)

apidiff ran successfully in the CI for that PR since it was a new and compatible addition, and hence deemed to be compatible.
However, it now fails to run against itself.
Reproducer:

git clone git@github.com:kubernetes/utils.git && cd utils
make verify-apidiff

What did you expect to see?

apidiff runs successfully when run against no new changes.

What did you see instead?

FAIL: k8s.io/utils/set contains incompatible changes:
- KeySet: changed from func(map[E]A) Set[E] to func(map[E]A) Set[E]
- New: changed from func(...E) Set[E] to func(...E) Set[E]
- Set: changed from Set[E ordered] to Set[E ordered]

Potential Reason

I see that apidiff was recently updated to support generics: https://go-review.googlesource.com/c/exp/+/411076
However, as part of this change, we check that 2 named types have an identical type parameter list:
https://github.com/golang/exp/blob/2e198f4a06a1643ba6d13f4823bebb46216bcb41/apidiff/correspondence.go#L209-L211

The issue that arises here is that since constraints are considered named types, and we are using types.Identical for comparison here, they are defined to be identical if they are part of the same type declaration, and since we now essentially end up with the same type but in two different packages (old and new), and hence are not identical.
See here: https://go.dev/play/p/z4AHbUXWA3D

However, on the other hand, if we used something like comparable as the type constraint instead of defining our own, things work fine since we no longer end up in the 2 package situation.
See here: https://go.dev/play/p/4X2t3TOx-UR

@ianlancetaylor
Copy link
Contributor

CC @jba

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 22, 2023
@dmitshur dmitshur changed the title x/exp/apidiff: Incorrect behaviour with user defined type constraints x/exp/apidiff: incorrect behaviour with user defined type constraints Jun 22, 2023
@gopherbot
Copy link

Change https://go.dev/cl/505276 mentions this issue: apidiff: match type parameter lists using correspondence

@jba
Copy link
Contributor

jba commented Jun 23, 2023

Thank you for the clear reproduction.

@MadhavJivrajani
Copy link
Author

Thank you @jba!
Verified that this fixes the issue:

❯ make verify-apidiff
GO111MODULE=on ./hack/verify-apidiff.sh -r master
Inspecting API of master...
Comparing with master...
Ignoring internal package k8s.io/utils/internal/third_party/forked/golang/golang-lru
Ignoring internal package k8s.io/utils/internal/third_party/forked/golang/net
❯ echo $?
0

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

5 participants