Skip to content

cmd/compile: anonymous interface conversion not working #33602

@randall77

Description

@randall77

With a directory structure like:

./go.mod
./c/c.go
./a/ssa/a.go
./b/ssa/b.go

go.mod:

module modpath

go 1.12

a.go:

package ssa

type T struct {
}

func (t *T) setNum(n int) {
}

var X interface{} = &T{}

func Run() {
	X.(interface {
		setNum(int)
	}).setNum(0)
}

b.go:

package ssa

type T struct {
}

func (t *T) setNum(n int) {
}

var X interface{} = &T{}

func Run() {
	X.(interface {
		setNum(int)
	}).setNum(0)
}

c.go:

package main

import (
	a "modpath/a/ssa"
	b "modpath/b/ssa"
)

func main() {
	a.Run()
	b.Run()
}

Then do

cd c
go build
./c

It fails with:

panic: interface conversion: *ssa.T is not interface { ssa.setNum(int) }: missing method setNum

goroutine 1 [running]:
modpath/b/ssa.Run(...)
	/usr/local/google/home/khr/gowork/matloob/b/ssa/b.go:12
main.main()
	/usr/local/google/home/khr/gowork/matloob/c/c.go:10 +0x90

This bug requires that both interfaces in a and b be anonymous. Somehow they are being unified (by the linker?) and the test in package b is using the anonymous interface from package a, and failing because the unexported methods must match packages.

I suspect we qualify the unexported methods with only the package name, not the package path. The two anonymous interfaces here have the same linker name when using just the package names.

@matloob

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions