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: Type parameter is not validated in receiver #50316

Closed
ilya-zz opened this issue Dec 22, 2021 · 3 comments
Closed

cmd/compile: Type parameter is not validated in receiver #50316

ilya-zz opened this issue Dec 22, 2021 · 3 comments

Comments

@ilya-zz
Copy link

@ilya-zz ilya-zz commented Dec 22, 2021

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

$ go version
devel go1.18-90fb5a4f97 Wed Dec 22 00:11:21 2021 +0000

Does this issue reproduce with the latest release?

No, generics required

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

any

go env Output
$ go env

What did you do?

Please refer to https://gotipplay.golang.org/p/pyAU-Se4Iqj

package main

import (
	"fmt"
)

type Foo[T any] struct {
	t T
}

func (f Foo[string]) Do() {
	fmt.Println(f.t)
}

func main() {
	x := Foo[int]{}
	x.Do()
}

What did you expect to see?

A compiler error at line func (f Foo[string]) Do() explaining that string is an invalid type parameter value for Foo

What did you see instead?

The program successfully compiled and can be executed. Output:

0
Program exited.
@ilya-zz ilya-zz changed the title affected/package: Go compiler. Type parameter is ignored in receiver Go compiler. Type parameter is ignored in receiver Dec 22, 2021
@ilya-zz ilya-zz changed the title Go compiler. Type parameter is ignored in receiver Go compiler. Type parameter is not validated in receiver Dec 22, 2021
@ilya-zz ilya-zz changed the title Go compiler. Type parameter is not validated in receiver cmd/compile: Type parameter is not validated in receiver Dec 22, 2021
@randall77
Copy link
Contributor

@randall77 randall77 commented Dec 22, 2021

string is not a keyword, just a predclared identifier. You can override it, e.g.

var string int = 3

Declares a variable of type int, value 3 and name string.

Similarly in your case, you're declaring the method Do on the generic type Foo, using the name string as the type parameter for the body of Do.

Dup of #48813

@ilya-zz
Copy link
Author

@ilya-zz ilya-zz commented Dec 22, 2021

@randall77 Thanks for explanation.

But if we slightly modify the code:

package main

import (
	"fmt"
)

type Foo[T any] struct {
	t T
}

func Do(f Foo[string])  {
	fmt.Println(f.t)
}

func main() {
	x := Foo[int]{}
	Do(x)
}

we get:
./prog.go:17:5: cannot use x (variable of type Foo[int]) as type Foo[string] in argument to Do

Looks a little inconsistent.

@randall77
Copy link
Contributor

@randall77 randall77 commented Dec 22, 2021

I agree it does look a bit strange. But in the latter example you're not defining a new name string, you're using the actual predeclared string type and instantiating Foo with it. Type expressions in the receiver position and in argument position are very different w.r.t. generics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants