Skip to content

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

@ahrtr

Description

@ahrtr

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}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions