Skip to content

cmd/compile: anonymous interface conversion not working #33602

Closed

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

Activity

  1. added this to the Go1.13 milestone on Aug 12, 2019
  2. self-assigned this
    on Aug 12, 2019
  3. cuonglm commented on Aug 12, 2019

    @cuonglm
    Member

    Seems strange, in https://go-review.googlesource.com/c/go/+/170157, I did use package path qualified.

  4. cuonglm commented on Aug 12, 2019

    @cuonglm
    Member

    @randall77 Seems it was fixed on tip, broken with go1.12.7.

  5. randall77 commented on Aug 12, 2019

    @randall77
    ContributorAuthor

    Right, thanks.
    Dup of issue 29612.

  6. locked and limited conversation to collaborators on Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions