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: confusing error when trying to instantiate type parameter on other-package constraint with unexported fields #51942

Open
ALTree opened this issue Mar 25, 2022 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@ALTree
Copy link
Member

ALTree commented Mar 25, 2022

Consider the following 3-files program:

go.mod:

module example.com/mod

go 1.18

a/a.go

package a

type I interface {
	struct{ i int }
}

func F[T I]() {}

test.go

package main

import "example.com/mod/a"

func main() {
	a.F[struct{ i int }]()
}

With go1.18 and tip, the program does not compile:

 $ go build
# example.com/mod
.\test.go:6:6: struct{i int} does not implement a.I

playground link: https://go.dev/play/p/yvRVxYbdkpK

To to me it looks like it should(?). The same constraint and function work fine when they're part of the main package. Am I missing something?

EDIT: I had the wrong mental model, it's correct that this doesn't work but the error message is slightly confusing. See below.

cc @findleyr @griesemer @mdempsky

@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 25, 2022
@ALTree ALTree added this to the Go1.19 milestone Mar 25, 2022
@seankhliao
Copy link
Member

This looks like visibility rules? exporting the struct field works: https://go.dev/play/p/S7_h3tqaeC7

@ALTree
Copy link
Member Author

ALTree commented Mar 25, 2022

Mmh, indeed. If you change the signature to use an unnamed constraint, as: func F[T struct{ i int }]() {} the error message is quite puzzling:

./prog.go:6:6: struct{i int} does not implement struct{i int}

Leaving for others to decide if we can just close this, or if the error message should be tuned.

@seankhliao
Copy link
Member

it's similar to the error without constraints: https://go.dev/play/p/E8rF0Rzw26_A

@mdempsky
Copy link
Member

I think the type checker is correct to reject this code. struct{i int} denotes "different" types when appearing in different packages because of the non-exported field name.

That said, I agree the error message is confusing and could be made clearer like we do in some other cases.

@ianlancetaylor ianlancetaylor modified the milestones: Go1.19, Backlog Mar 25, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
@ALTree
Copy link
Member Author

ALTree commented Aug 16, 2022

For some reason I had this mental model that for type parameters it didn't matter if the field was exported or not, so I expected to be able to resolve the correct instantiation of a.F by writing a.F[struct{ i int }]. Anyway clearly is not the case and of course a normal variable would not be assignable so it makes sense that it doesn't work for type parameters.

Retitling this to make it about a less confusing error.

@ALTree ALTree changed the title cmd/compile: cannot satisfy other-package struct typeset cmd/compile: confusing error when trying to instantiate type parameter on other-package constraint with unexported fields Aug 16, 2022
@ALTree ALTree added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
Status: Triage Backlog
Development

No branches or pull requests

5 participants