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

The method set of S shouldn't include promoted methods with receiver *T when S contains an embedded field T #39255

Closed
ahrtr opened this issue May 26, 2020 · 1 comment

Comments

@ahrtr
Copy link

@ahrtr ahrtr commented May 26, 2020

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

go version go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

YES

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

bogon:~ benjamin$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
......

What did you do?

I wrote a simple golang program as follow,

package main

import (
	"fmt"
)

type people struct {
	age  int
	name string
}

func (p *people) Speak() {
	fmt.Println("I am people.")
}

func (p people) Eat() {
	fmt.Println("People like cake.")
}

type student struct {
	people
	score int
}

func main() {
	p := people{30, "ben"}
	s := student{p, 80}
	s.Speak()
	s.Eat()

	fmt.Println(s)
}

What did you expect to see?

I expected to see a compiling error or a runtime panic. Based on the golang spec as below, The method set of S shouldn't include promoted methods with receiver *T. In my example, the student shouldn't include prometed method Speak() with receiver *people.

If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.

What did you see instead?

I did not see any error, and the output of my example program was as below,

I am people.
People like cake.
{{30 ben} 80}
@randall77
Copy link
Contributor

@randall77 randall77 commented May 26, 2020

From the spec:

If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m():

So your call s.Speak() is actually (&s).Speak() and then that falls in the second sentence of the part of the spec you quoted.

So this is working as intended. Closing.

@randall77 randall77 closed this May 26, 2020
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
2 participants
You can’t perform that action at this time.