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

Closed
Code-Hex opened this issue Nov 16, 2021 · 7 comments
Closed
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@Code-Hex
Copy link

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
Contributor

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)

@findleyr
Copy link
Contributor

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.

@findleyr findleyr added this to the Go1.18 milestone Nov 16, 2021
@findleyr findleyr added the NeedsFix The path to resolution is known, but the work has not been done. label 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 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?

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

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.

@gopherbot
Copy link

Change https://golang.org/cl/372674 mentions this issue: cmd/compile/internal/types2: better error message when using comparable in union

@gopherbot
Copy link

Change https://golang.org/cl/372694 mentions this issue: spec: clarify that comparable cannot be a union element

@gopherbot
Copy link

Change https://golang.org/cl/372954 mentions this issue: go/types: better error message when using comparable in union

gopherbot pushed a commit that referenced this issue Dec 17, 2021
For #49602.

Change-Id: I0d3ff8f087dffb3409918494147fd1dceff7514d
Reviewed-on: https://go-review.googlesource.com/c/go/+/372694
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
gopherbot pushed a commit that referenced this issue Dec 20, 2021
This is a port of CL 372674 from types2 to go/types with
minor adjustments for error handling.

For #49602.

Change-Id: I726081325a2ff2d5690d11ddc8a830bbcbd8ab33
Reviewed-on: https://go-review.googlesource.com/c/go/+/372954
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@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