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: error message for misuse of < in generic refers confusingly to comparable #63524

Open
ianlancetaylor opened this issue Oct 12, 2023 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@ianlancetaylor
Copy link
Contributor

Extracted from #63523. Compiling this program

package p

func F[T comparable](a, b T) bool {
	return a < b
}

produces

foo.go:4:9: invalid operation: a < b (type parameter T is not comparable with <)

It is somewhat confusing that the error message says that "T is not comparable" when the constraint for T is comparable. Not a major issue, but we should use better wording here.

@ianlancetaylor ianlancetaylor added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Oct 12, 2023
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Oct 12, 2023
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Oct 12, 2023
@cuonglm
Copy link
Member

cuonglm commented Oct 13, 2023

I think we should mention about the invalid operator instead. Something like:

foo.go:4:9: invalid operation: a < b (operator < does not apply to comparable type T)

@kavinaravind
Copy link

kavinaravind commented Oct 13, 2023

looks like the error is defined in src/cmd/compile/internal/types2/expr.go and src/go/types/expr.go

From a quick look, it seems that the phrase "is not comparable" is a common phrase for go errors, although I completely agree that the error message can be a bit confusing

@meling
Copy link

meling commented Oct 13, 2023

I think we should mention about the invalid operator instead. Something like:

foo.go:4:9: invalid operation: a < b (operator < does not apply to comparable type T)

For the error message to be helpful, it has to make reference to cmp.Ordered. For example:

foo.go:4:9: invalid operation: a < b (type parameter T must satisfy cmp.Ordered interface to be used with <)
foo.go:4:9: invalid operation: a < b (type parameter T must be cmp.Ordered with <)
foo.go:4:9: invalid operation: a < b (type parameter T is missing cmp.Ordered required for <)
foo.go:4:9: invalid operation: a < b (operator < with type parameter T must satisfy cmp.Ordered)
foo.go:4:9: invalid operation: a < b (operator < with type T must be cmp.Ordered)

@cuonglm
Copy link
Member

cuonglm commented Oct 14, 2023

For the error message to be helpful, it has to make reference to cmp.Ordered.

There could be any kind of operators here, so not sure it's feasible (or worth) to report it.

@blackgreen100
Copy link

blackgreen100 commented Oct 18, 2023

IMO the key distinction that users need to be aware of is the one mentioned by the spec about comparable vs. ordered.

Therefore it could be enough to parametrize the comparable bit of the error message (seen at line 580) based on the operator:

cause = check.sprintf("type parameter %s is not %s with %s", errOp.typ, op, verb)

where verb could be "comparable" when op is syntax.Eql, syntax.Neq: and "orderable" if op is syntax.Lss, syntax.Leq, syntax.Gtr, syntax.Geq.

The term "orderable" would fit in that sentence structure but has the disadvantage of being informal (probably not even grammatical English, not sure about that). The spec use the term "ordered". So an alternative would be to allow the error messages to diverge. For example, a logic like this:

switch op {
case syntax.Eql, syntax.Neq:
    cause = check.sprintf("type parameter %s is not comparable with %s", errOp.typ, op)

case syntax.Lss, syntax.Leq, syntax.Gtr, syntax.Geq:
    cause = check.sprintf("type parameter %s can not be ordered with %s", errOp.typ, op)
}

@mknyszek mknyszek changed the title cmd/compile: error message for misuse of < in generic refers confusing to comparable cmd/compile: error message for misuse of < in generic refers confusingly to comparable Nov 1, 2023
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. help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

6 participants