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: Bad error message on missing type constraint #55961

Closed
varungandhi-src opened this issue Sep 30, 2022 · 7 comments
Closed

cmd/compile: Bad error message on missing type constraint #55961

varungandhi-src opened this issue Sep 30, 2022 · 7 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@varungandhi-src
Copy link

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

$ go version
1.19

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="arm64"
GOBIN=""
GOCACHE="/Users/varun/Library/Caches/go-build"
GOENV="/Users/varun/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/varun/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/varun/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/varun/.asdf/installs/golang/1.19/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/varun/.asdf/installs/golang/1.19/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/varun/Code/scip/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/tr/gylnxkts3bn0gyhhlrv8z9_m0000gn/T/go-build325381962=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Try to compile this file:

type X[T] struct {
	t T
}
func main() {}

What did you expect to see?

A useful error message, something like: (IDK if 'constraint' is the right verbiage)

./tmp.go:2:8: missing type constraint for 'T'; explicitly specify 'T any' if T doesn't have any constraints

There shouldn't be an error on line 3 because it is not useful.

What did you see instead?

An error which doesn't clearly indicate the problem:

./tmp.go:2:8: undeclared name T for array length
./tmp.go:3:4: undefined: T
@varungandhi-src varungandhi-src changed the title cmd/go: Bad diagnostic on missing type constraint cmd/compile: Bad error message on missing type constraint Sep 30, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Sep 30, 2022
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 30, 2022
@dmitshur dmitshur added this to the Backlog milestone Sep 30, 2022
@prattmic
Copy link
Member

prattmic commented Oct 5, 2022

cc @griesemer @findleyr

@griesemer
Copy link
Contributor

Sorry, but this is working as intended. What you wrote is an array declaration (and if you use an IDE it may even format it accordingly):

type X [T]struct {
	t T
}

This is an array of structs. It reports an error because the length T is not defined, but if T existed as in:

const T = 10
type X [T]struct {
	t T
}

you'd just get an error for the field (T is not a type). The compiler cannot reasonably know (or "guess") that you meant a generic type X instead. The error message could perhaps also say something like: missing type constraint? but the compiler doesn't usually try to guess because it might just guess wrong.

And exactly because we are aware of this potential problem, the error message is explicitly saying undefined T for array length (emphasis on array length). This makes it clear that you have declared an array.

@findleyr findleyr closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2022
@griesemer
Copy link
Contributor

See also #51145.

@varungandhi-src
Copy link
Author

Sorry, but this is working as intended. What you wrote is an array declaration (and if you use an IDE it may even format it accordingly):

Even the IDE formats it (and the error message indicates something), it is not obvious as to why this is getting parsed as an array instead of a generic type. In other languages (Java, TypeScript, Rust etc.), one generally doesn't need to explicitly specify the equivalent of an any constraint (if no constraint is specified, the type parameter is unconstrained), so it's not necessarily obvious that the missing constraint is the source of the problem.

The compiler cannot reasonably know (or "guess") that you meant a generic type X instead.

IIUC, the error is happening during name resolution (it is complaining about a missing definition), which means the code was already parsed. When emitting the error, would it possible to check that the next token (or the start of the next node in the parse tree), see if it is a struct, and if it is, then emit a hint for the missing any? I would guess that there is a much higher likelihood that you forgot to write any (one missing token) rather than meaning to write an array type (multiple missing tokens)...

@griesemer
Copy link
Contributor

griesemer commented Oct 5, 2022

type X [T]someType

was always an array declaration in Go (for the last 15 years), it's not new syntax. Go is not other languages (where arrays may look differently). Maybe it's not obvious, which is why the array message explicitly says array length.

Yes, the error is happening during name resolution - because the code has already been parsed as an array declaration (because that is what it is, syntactically). Checking the next token doesn't help because

type X [T]struct{}

is a syntactically correct array declaration. The parser cannot know if T was defined and where. The type checker cannot assume that if T is not defined it's not an array declaration: the error might well be that T should have been defined.

About the likelihood of the error: I don't know that it's more common to have a generic type than an array type now. Note that there are no missing tokens in the array case, there's just a missing declaration (perhaps in a separate, missing file).

Anyway, I filed #56064.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/439476 mentions this issue: go/types, types2: better error for generic type decl. with missing constraint

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/439559 mentions this issue: go/types, types2: better error for generic type decl. with missing constraint

gopherbot pushed a commit that referenced this issue Oct 10, 2022
…nstraint

If a generic type declaration is missing a constraint, syntactically
it is an array type declaration with an undefined array length.
Mention the possibility of a missing constraint in the error message
for the undefined array length.

For #56064.
For #55961.
For #51145.

Change-Id: Ic161aeda9ea44faa8aa3bf3e9d62b3b13a95d4c5
Reviewed-on: https://go-review.googlesource.com/c/go/+/439559
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
romaindoumenc pushed a commit to TroutSoftware/go that referenced this issue Nov 3, 2022
…nstraint

If a generic type declaration is missing a constraint, syntactically
it is an array type declaration with an undefined array length.
Mention the possibility of a missing constraint in the error message
for the undefined array length.

For golang#56064.
For golang#55961.
For golang#51145.

Change-Id: Ic161aeda9ea44faa8aa3bf3e9d62b3b13a95d4c5
Reviewed-on: https://go-review.googlesource.com/c/go/+/439559
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
@golang golang locked and limited conversation to collaborators Oct 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge 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

6 participants