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/go2go: improve error messages for missing or superfluous type constraints #43527

Open
candlerb opened this issue Jan 5, 2021 · 5 comments
Open
Assignees
Milestone

Comments

@candlerb
Copy link

@candlerb candlerb commented Jan 5, 2021

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

go2go playground

Does this issue reproduce with the latest release?

N/A

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

N/A

What did you do?

  1. https://go2goplay.golang.org/p/m9YUDBTWwpA

    func (m *MyMap[KT,VT comparable]) set(k KT, v VT) { ...
    
  2. https://go2goplay.golang.org/p/GDxiXiIIti7

    func blah[T1, T2](x T1) T2 { ...
    

What did you expect to see?

  1. error to the effect that a constraint was used in a place where it's not allowed
  2. error to the effect that a type constraint is missing

What did you see instead?

  1. "missing ',' in type argument list"
  2. "all type parameters must be named" (even though they were named!)
@ianlancetaylor ianlancetaylor changed the title go2: improve error messages for missing or superfluous type constraints cmd/go2go: improve error messages for missing or superfluous type constraints Jan 5, 2021
@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Jan 5, 2021
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jan 5, 2021

@griesemer griesemer self-assigned this Jan 6, 2021
@griesemer griesemer removed this from the Unreleased milestone Jan 6, 2021
@griesemer griesemer added this to the Go1.17 milestone Jan 6, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Jan 6, 2021

The first error is fixed in the latest (internal, not deployed version). The 2nd error is correct but misleading: Since the type parameter list is [T1, T2] and since type parameter lists are syntactically like regular parameter lists (but for the []'s), it looks like a parameter list with two constraints T1 and T2 and without explicitly named type parameters. For comparison, note that func f(int, int){} is a valid function declaration with unnamed value parameters.

Anyway, agreed that the error message should be better.

@candlerb
Copy link
Author

@candlerb candlerb commented Feb 8, 2021

A similar case occurs when defining generic types if you omit the constraint on the type parameter, e.g.
https://go2goplay.golang.org/p/GqCc1gE36NL

That is, if you write

type slot[T] ...

instead of

type slot[T any] ...

In the go2go playground, the error currently says "undeclared name: T", rather than "type parameter T is missing type constraint"

For comparison, note that func f(int, int){} is a valid function declaration with unnamed value parameters.

Ugh, I didn't realise that was allowed. If you want to discard arguments, you can also write func f(_, _ int){}, and to me that makes the intention clearer.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Feb 9, 2021

"type slot[T] some_type" defines slot as an array of length T of element type some_type. If T is not defined anywhere, then undeclared name: T is a correct error message.

That said, we might want to tweak the compiler error message here to make clear that the type is being interpreted as an array type.

1 similar comment
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Feb 9, 2021

"type slot[T] some_type" defines slot as an array of length T of element type some_type. If T is not defined anywhere, then undeclared name: T is a correct error message.

That said, we might want to tweak the compiler error message here to make clear that the type is being interpreted as an array type.

@griesemer griesemer removed this from the Go1.17 milestone Apr 14, 2021
@griesemer griesemer added this to the Go1.18 milestone Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants