You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
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
changed the title
Go compiler. Type parameter is not validated in receiver
cmd/compile: Type parameter is not validated in receiver
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.
What version of Go are you using (
go version
)?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
OutputWhat did you do?
Please refer to https://gotipplay.golang.org/p/pyAU-Se4Iqj
What did you expect to see?
A compiler error at line
func (f Foo[string]) Do()
explaining thatstring
is an invalid type parameter value forFoo
What did you see instead?
The program successfully compiled and can be executed. Output:
The text was updated successfully, but these errors were encountered: