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: misleading error message when using type parameter in typeset #50420

Closed
icholy opened this issue Jan 4, 2022 · 9 comments
Closed
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@icholy
Copy link

icholy commented Jan 4, 2022

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

devel go1.18-c8861432b8 Sun Jan 2 14:27:43 2022 +0000

What did you do?

type X[T any] interface {
	T | string
}

https://go.dev/play/p/v-8zaOs_d3r?v=gotip

What did you expect to see?

"cannot use type parameter in typeset"

What did you see instead?

"cannot embed a type parameter"

@go101
Copy link

go101 commented Jan 4, 2022

This is specified in https://tip.golang.org/ref/spec#Interface_types

In a union, a term cannot be a type parameter ...

[edit], Aha, you mean the message is not clear. Okay, it is some of.

@thanm thanm added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jan 4, 2022
@thanm
Copy link
Contributor

thanm commented Jan 4, 2022

@griesemer per owners.

Personally I think "cannot use type parameter in typeset" is perfectly clear as it is.

@griesemer griesemer added this to the Backlog milestone Jan 4, 2022
@griesemer griesemer self-assigned this Jan 4, 2022
@griesemer
Copy link
Contributor

griesemer commented Jan 4, 2022

Just to be clear (@thanm ), the current error message is: cannot embed a type parameter.
cc: @findleyr for suggestions.

@findleyr
Copy link
Contributor

findleyr commented Jan 4, 2022

I'm not sure if the word(s) 'type set' (or typeset) are appropriate here, since it refers to the entire type set of the interface type. Maybe "cannot use a type parameter in a union", which is the phrasing currently used in the spec.

@findleyr
Copy link
Contributor

findleyr commented Jan 4, 2022

...though it may also be confusing that *T is valid here, as in

type X[T any] interface {
	*T | string
}

Maybe union terms cannot be type parameters is sufficiently specific.

@go101
Copy link

go101 commented Jan 5, 2022

Some off-topic, should "union" be replaced with "intersection"? int | string means allowing the operations which are legal for both int and string, instead of for either int or string.

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Jan 5, 2022

int | string means that both int and string are in the type set of the interface. More generally T1 | T2 means that the union of the type sets of T1 and T2 are in the type set of the interface. So union is the correct term.

It's true that because an operation is supported if it is supported by all types in the type set, one can view operations as going by the intersection. But from a type set perspective it is definitely a union operator.

See #45346.

@griesemer griesemer modified the milestones: Backlog, Go1.19 Mar 18, 2022
@gopherbot
Copy link

gopherbot commented Mar 30, 2022

Change https://go.dev/cl/396874 mentions this issue: go/types, types2: better error message for invalid type parameter term

@gopherbot
Copy link

gopherbot commented Mar 30, 2022

Change https://go.dev/cl/396915 mentions this issue: spec: clarify type term restriction for type parameters

gopherbot pushed a commit that referenced this issue Mar 31, 2022
Be clear that the type of a term (not the term itself, which may
be of the form ~P) cannot be a type parameter.

For #50420.

Change-Id: I388d57be0618393d7ebe2c74ec04c1ebe3f33f7d
Reviewed-on: https://go-review.googlesource.com/c/go/+/396915
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

7 participants