Skip to content

cmd/compile: method call on interface panics, but works on the contained struct #24547

@hubslave

Description

@hubslave

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

go version go1.10 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/a/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/a"
GORACE=""
GOROOT="/home/a/go"
GOTMPDIR=""
GOTOOLDIR="/home/a/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build961933609=/tmp/go-build"

What did you do?

https://play.golang.org/p/TjVzZGtL8zz

package main

import (
	"bytes"
	"fmt"
	"log"
)

type mystruct struct {
	f int
}

func (t mystruct) String() string {
	return "foo"
}

func main() {
	type deep struct {
		mystruct
	}
	s := struct {
		deep
		*bytes.Buffer
	}{
		deep{},
		bytes.NewBufferString("TEST"),
	}
	log.Print(s.String()) // ok
	var i fmt.Stringer = s
	log.Print(i.String)   // ok
	log.Print(i.String()) // panic
}

What did you expect to see?

2018/03/27 03:04:27 TEST
2018/03/27 03:04:27 0x48dc70
2018/03/27 03:04:27 TEST

What did you see instead?

2018/03/27 03:04:27 TEST
2018/03/27 03:04:27 0x48dc70
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x0]

goroutine 1 [running]:
main.main()
        /tmp/test.go:31 +0x25c

Are my expectations correct? I have reread the Struct types and the Selectors paragraphs of the specs but I can't find an explanation for the panic, I hope I haven't overlooked something obvious.
It seems related to the method collision (at different depths) and some specific combination of pointers and values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions