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

Open
rogpeppe opened this issue Nov 12, 2021 · 3 comments
Open

Comments

@rogpeppe
Copy link
Contributor

@rogpeppe rogpeppe commented Nov 12, 2021

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

@rogpeppe rogpeppe commented Nov 12, 2021

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.

Loading

@cagedmantis
Copy link
Contributor

@cagedmantis cagedmantis commented Nov 12, 2021

Loading

@griesemer
Copy link
Contributor

@griesemer 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.

cc: @findleyr

Loading

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
4 participants