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: erroneous "inconsistent use of () or [] for type parameters" #40806

Closed
lolbinarycat opened this issue Aug 14, 2020 · 5 comments
Closed

Comments

@lolbinarycat
Copy link

@lolbinarycat lolbinarycat commented Aug 14, 2020

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

$ go version
go version devel +722af87f62 Fri Aug 14 05:30:01 2020 +0000 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=""
GOCACHE="/home/binarycat/.cache/go-build"
GOENV="/home/binarycat/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/binarycat/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/binarycat/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/binarycat/goroot"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/binarycat/goroot/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build338557042=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I have 2 files in a directory:
a.go:

package a
type A[type T] T

a_test.go

package a
import "testing"
func TestA(_ testing.T) {}

What did you expect to see?

PASS
ok      github.com/lolbinarycat/go2go-bug       0.007s

What did you see instead?

type checking failed for a
a_test.go2:1:1: inconsistent use of () or [] for type parameters

I'm not sure if this will replicate (it seemed inconsistent to me), so I'm also linking the actual project (emacs autosaves and all) here. run the command withing the ll directory.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 15, 2020

The workaround is to use go tool go2go -brackets.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 15, 2020

It's a problem in the go2go tool. It processes a_test.go2 first, and decides that the package doesn't use square brackets. Then it processes a.go2 and decides that it does need square brackets. Then the type checker reports an inconsistency.

I think we're going to settle on a syntax soon so I don't think this is worth fixing.

@lolbinarycat
Copy link
Author

@lolbinarycat lolbinarycat commented Aug 15, 2020

Ah, that makes sense. That would explain why it was so inconsistant, as it depends on the order in which files are parsed. Also, I was going to mention this in the post, I found a workaroung which was func TestA[](t *testing.T). Is this syntax (an empy type paramaters list) planned to be valid in the final release?

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 16, 2020

I expect the final release will permit an empty type parameter list yes, although it is useless.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Aug 28, 2020

We have settled for a syntax of type parameters that matches the syntax of regular parameters except that we use [] rather than (). The current prototype (dev.go2go branch, the playground may be a few days behind) now uses

  • [] for type parameters;
  • the type keyword is not needed (and not permitted);
  • each type parameter needs a constraint, which may be the predeclared identifier any (== interface{});
  • parameterized constraints must be fully instantiated.

See the latest draft design for details. Note that any is only valid in constraint position (it cannot be used as a general replacement for interface{} for now).

Your package a will need to be written

package a
type A[T any] T

Also, with this new syntax, an empty type parameter list in a generic type declaration would mean a slice type declaration and thus is not possible. For symmetry, a generic function declaration cannot have an empty type parameter list either (e.g., func f[](x int) is not permitted). Analogously, an empty instantiation such as f[](42) is not permitted.

Since the prototype has switched to the new syntax I will close this issue. Thanks.

@griesemer griesemer closed this Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.