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 results if any function parameter changes from a named type to a basic type #61385

Closed
maxb opened this issue Jul 16, 2023 · 5 comments
Assignees
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@maxb
Copy link

maxb commented Jul 16, 2023

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

$ go version
go version go1.20.6 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/maxb/.local/bin"
GOCACHE="/home/maxb/.cache/go-build"
GOENV="/home/maxb/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/maxb/.cache/go-modules"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/maxb/.cache/go-path"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.6"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/maxb/code/exp/go.mod"
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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3661324250=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I observed bizarre output from the x/exp/cmd/apidiff CLI. I investigated and produced a test reproducing the behaviour. Append this additional test case to https://github.com/golang/exp/blob/master/apidiff/testdata/tests.go and run the tests :

//////////////// A discovered bug

// old
func BugWithFuncParamFromNamedTypeToBasicType(closer io.ReadCloser) {}

// new
// i BugWithFuncParamFromNamedTypeToBasicType: changed from func(io.ReadCloser) to func(int)
func BugWithFuncParamFromNamedTypeToBasicType(integer int) {}

// both
func UnchangedFuncTakingSameNamedInterface(closer io.ReadCloser) {}

What did you expect to see?

You would reasonably expect the tests to pass, as the expected new message about the incompatible change was added to the test data (the // i line).

What did you see instead?

Not only does the expected message about the incompatibility not get generated, but two bizarre and untrue incompatibilities are now reported:

ReadCloser: changed from interface{io.Reader; io.Closer} to int

apidiff is claiming that the type of the stdlib io.ReadCloser has changed to int!

UnchangedFuncTakingSameNamedInterface: changed from func(io.ReadCloser) to func(io.ReadCloser)

and that this function has changed, despite it not changing!

Investigation reveals this defective code:

https://github.com/golang/exp/blob/613f0c0eb8a17a98ecdb096a7f9f7d5053c1c963/apidiff/correspondence.go#L159-L210

If you look carefully, you'll see that if this function is called with the new input not being a *types.Named, it will falsely assume correspondence with no further checks at all - and here it is being called with a *types.Basic.

@gopherbot gopherbot added this to the Unreleased milestone Jul 16, 2023
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 17, 2023
@seankhliao seankhliao changed the title x/exp/apidiff: Incorrect results if any function parameter changes from a named type to a basic type x/exp/apidiff: incorrect results if any function parameter changes from a named type to a basic type Jul 17, 2023
@bcmills
Copy link
Contributor

bcmills commented Jul 17, 2023

(attn @jba per https://dev.golang.org/owners; CC @matloob)

@bcmills
Copy link
Contributor

bcmills commented Jul 17, 2023

Investigation reveals this defective code:

@maxb, want to send a fix?

See https://go.dev/doc/contribute for instructions; I would strongly recommend using the Gerrit workflow rather than a GitHub PR.

@maxb
Copy link
Author

maxb commented Jul 17, 2023

I can tell it is defective, but I'm not confident what the correct logic would be.

For my own temporary purposes, I've cludged it to never treat basic types as corresponding, but the original authors seem to have intended to handle some possible cases.

I've tried to follow the description of "correspondence" described at https://github.com/golang/exp/blob/master/apidiff/README.md, but it is seriously complicated, and I don't feel I understand it all.

I'm hoping the information I have managed to lay out here will assist someone better acquainted with the code developing a true fix.

@maxb
Copy link
Author

maxb commented Jul 17, 2023

Actually it looks like @jba touched this code last week - golang/exp@2a6b8fd - so may be well-placed to figure out what it should actually be doing.

@jba jba self-assigned this Jul 17, 2023
@jba jba removed help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jul 17, 2023
@seankhliao seankhliao added the NeedsFix The path to resolution is known, but the work has not been done. label Jul 25, 2023
@gopherbot
Copy link

Change https://go.dev/cl/513675 mentions this issue: apidiff: fix comparison of a type in an other package with a basic type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants