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: cannot satisfy other-package struct typeset #51942

Open
ALTree opened this issue Mar 25, 2022 · 4 comments
Open

cmd/compile: cannot satisfy other-package struct typeset #51942

ALTree opened this issue Mar 25, 2022 · 4 comments
Labels
NeedsInvestigation
Milestone

Comments

@ALTree
Copy link
Member

@ALTree 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?

cc @findleyr @griesemer @mdempsky

@ALTree ALTree added the NeedsInvestigation label Mar 25, 2022
@ALTree ALTree added this to the Go1.19 milestone Mar 25, 2022
@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Mar 25, 2022

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

@ALTree
Copy link
Member Author

@ALTree 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
Contributor

@seankhliao seankhliao commented Mar 25, 2022

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

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Mar 25, 2022

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 removed this from the Go1.19 milestone Mar 25, 2022
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

4 participants