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

go/types, types2: confusing error message when using comparable in a union #49602

Open
Code-Hex opened this issue Nov 16, 2021 · 4 comments
Open

go/types, types2: confusing error message when using comparable in a union #49602

Code-Hex opened this issue Nov 16, 2021 · 4 comments
Assignees
Labels
Milestone

Comments

@Code-Hex
Copy link

@Code-Hex Code-Hex commented Nov 16, 2021

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

$ go version
go version devel go1.18-c2397905e0 Sat Nov 13 03:33:55 2021 +0000 darwin/arm64

Does this issue reproduce with the latest release?

No, because it includes generics

What operating system and processor architecture are you using (go env)?

Whatever the go playground runs on

What did you do?

https://gotipplay.golang.org/p/FSMOjCXAtSL

What did you expect to see?

can compile

What did you see instead?

compile error

./prog.go:6:66: cannot use comparable in union (interface contains methods)

@syumai
Copy link

@syumai syumai commented Nov 16, 2021

I got the same error in a similar case.
https://gotipplay.golang.org/p/y_eVVr0rp7H

comparable interface should be treated as a union of enumerated comparable types, so expect to be able to use it in a union. (I think comparable interface does not contain method as a constraint.)
If it is not allowed to use comparable within a union, then I want a different error message like below.

cannot use comparable in union (comparable can not be a union element)

Loading

@findleyr
Copy link
Contributor

@findleyr findleyr commented Nov 16, 2021

CC @griesemer

This is working as intended, at least for Go 1.18. See #45346 (comment) for more context. With respect to computing type sets, comparable behaves like an interface with methods.

I agree that we should improve the error message, however.

Loading

@findleyr findleyr added this to the Go1.18 milestone Nov 16, 2021
@findleyr findleyr changed the title comparable union raises compile error go/types, types2: confusing error message when using comparable in a union Nov 16, 2021
@Code-Hex
Copy link
Author

@Code-Hex Code-Hex commented Nov 16, 2021

Thank you for your comment.

To be honest, I didn't understand why comparable types are not allowed to be used in unions. (As spec)
https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#comparable-types-in-constraints

This is just curious. Are there any resources or links that can help me?

Loading

@griesemer griesemer self-assigned this Nov 16, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Nov 16, 2021

Eventually there will be better documentation. We're in the middle of writing it. Suffice to say that for 1.18 at least, comparable cannot be part of a multi-element union.

Will keep this open to improve the error message.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants