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: better error message for invalid use of constraint interface #42881

Closed
voldyman opened this issue Nov 30, 2020 · 6 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@voldyman
Copy link

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

fmt.Printf("%s\n", runtime.Version())
devel +9daca74ad2 Wed Nov 18 00:18:01 2020 +0000

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

go2 playground (https://go2goplay.golang.org/)

go env Output
fmt.Printf("%s/%s\n", runtime.GOOS, runtime.GOARCH)
linux/amd64

What did you do?

type SortableComparable interface {
   comparable
   GreaterOrEqual(other SortableComparable) bool
}

https://go2goplay.golang.org/p/LGpaCLkA31a

What did you expect to see?

Successful compilation, since it's the same as the following code which compiles


type sortable interface {
	GreaterOrEqual(other sortable) bool
}

type SortableComparable interface {
	comparable
	sortable
}

https://go2goplay.golang.org/p/f2Lpuv-3JKl

What did you see instead?

type checking failed for main
prog.go2:10:23: interface is (or embeds) comparable
@ianlancetaylor
Copy link
Contributor

This isn't permitted, because it would require the type to have a method GreaterOrEqual(other SortableComparable) bool. But since SortableComparable embeds comparable, it may only be used as a type constraint, not as an ordinary type. So no type can ever have such a method.

But the error message could be better. CC @griesemer.

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 30, 2020
@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Nov 30, 2020
@griesemer
Copy link
Contributor

As an aside, the two programs you (@voldyman) are comparing are not the same: The first one has a different interface type for the other parameter than the second one.

@griesemer griesemer changed the title cmd/go2go: interface with comparable constraint and self reference fails to compile cmd/go2go: better error message for invalid use of constraint interface Nov 30, 2020
@griesemer griesemer self-assigned this Nov 30, 2020
@voldyman
Copy link
Author

Got it, in that case the error message while trying to create a implementation is also not clear.

https://go2goplay.golang.org/p/-knY7KsfF67

type sortable interface {
	GreaterOrEqual(other sortable) bool
}

type SortableComparable interface {
	comparable
	sortable
}

type Num struct {
	num int
}

func (n Num) GreaterOrEqual(s sortable) bool {
	other, ok := s.(Num)
	if !ok {
		return false
	}
	return n.num >= other.num
}

var _ SortableComparable = Num{2}

type checking failed for main
prog.go2:29:7: interface is (or embeds) comparable

related question: Is there a way to express "the type should have methods a & b, and also be allowed in the key of a map " with the current generics proposal?
(https://go2goplay.golang.org/p/MbM5zhGwMS9)

@griesemer
Copy link
Contributor

@voldyman Interfaces that contain type lists or embed comparable cannot be used as type of a variable (incl. parameter, result, field, etc.); it can only be used as a constraint. But map keys can be interfaces even if they don't embed comparable (that's always been the case). Taking your example and modifying it a bit shows how a SortableComparable can be used as a map key; though there is no way to define your own equality function that then would be used for the map. The latter would require a custom (generic) map, but that will be possible with generics.

@voldyman
Copy link
Author

voldyman commented Dec 2, 2020

Thanks for the detailed answer!

(I'll leave this issue open for the compiler error message but feel free to close it if not required)

@griesemer griesemer changed the title cmd/go2go: better error message for invalid use of constraint interface go/types, types2: better error message for invalid use of constraint interface Mar 18, 2022
@griesemer griesemer modified the milestones: Unreleased, Go1.19 Mar 18, 2022
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/410954 mentions this issue: go/types, types2: better error message for inavlid use of constraint type

@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 NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants