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: misleading error message when struct lacks expected type parameters #49541

Closed
rogpeppe opened this issue Nov 12, 2021 · 4 comments
Closed
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rogpeppe
Copy link
Contributor

commit 1ec5108

I compiled the following (incorrect) code: https://gotipplay.golang.org/p/NF9UzMFrJG1

type S[A, B any] struct {
	F int
}

func f[A any](s S[A]) {
	s.F = 1
}

I see these error messages:

./prog.go:10:17: cannot infer B (prog.go:6:11)
./prog.go:10:17: got 1 arguments but 2 type parameters
./prog.go:11:4: s.F undefined (type S[A] has no field or method F)

The last error message is misleading because S actually does have a field named F even if it hasn't been instantiated with the required type parameters.

@rogpeppe
Copy link
Contributor Author

Here another example, probably related: https://gotipplay.golang.org/p/FBFHzcZOTmX

package main

func main() {
	X(Interface[*F[string]](Impl{}))
}

func X[Q Qer](fs Interface[Q]) {
}

type Impl struct{}

func (Impl) M() {}

type Interface[Q Qer] interface {
	M()
}

type Qer interface {
	Q()
}

type F[A, B any] struct{}

func (f *F[A, B]) Q() {}

This produces the errors:

./prog.go:4:3: *F[string] does not satisfy Qer (missing method Q)
./prog.go:4:14: *F[string] does not satisfy Qer (missing method Q)
./prog.go:4:15: cannot infer B (prog.go:22:11)
./prog.go:4:15: got 1 arguments but 2 type parameters

The type F does have a method Q so the first two error messages are misleading.
Also, those messages come before the actual cause of the error (that we're only passing
one type parameter when we should be passing two), which adds to the confusion.

@rogpeppe rogpeppe added the generics Issue is related to generics label Nov 12, 2021
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 12, 2021
@cagedmantis cagedmantis added this to the Go1.18 milestone Nov 12, 2021
@cagedmantis
Copy link
Contributor

/cc @griesemer

@griesemer
Copy link
Contributor

griesemer commented Nov 13, 2021

The underlying issue here is that some of the checks must be done in a delayed fashion, leading to some confusing scenarios like this one.

This is just a bug.

cc: @findleyr

@gopherbot
Copy link

Change https://golang.org/cl/378175 mentions this issue: go/types, types2: avoid field/method lookup error on invalid types

@griesemer griesemer added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. generics Issue is related to generics labels Jan 12, 2022
jproberts pushed a commit to jproberts/go that referenced this issue Jun 21, 2022
Fixes golang#49541.

Change-Id: I27a52d0722a7408758682e7ddcd608c0a6c4881b
Reviewed-on: https://go-review.googlesource.com/c/go/+/378175
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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