Skip to content

static conversion of interface types fails #809

@rogpeppe

Description

@rogpeppe
some quotations from the manual:

``Two interface types are identical if they have the same set of methods with the same
names and 
identical function types. The order of the methods is irrelevant.''

``Type compatibility is less stringent than type identity: a named and an unnamed type
are compatible if 
the respective type literals are compatible. In all other respects, the definition of
type compatibility is the 
same as for type identity listed above but with ``compatible'' substituted for
``identical''.''

``In general, a conversion succeeds if the value of x is assignment compatible with type
T, or if the value 
would be assignment compatible with type T if the value's type, or T, or any of their
component types 
were unnamed.''

i'm not quite sure what a "type literal" means for an interface type,
but perhaps it's not unreasonable to assume that it's the value
from which the interface is made.

this makes me believe that the following program should work
(and in fact it would be useful if it did, as it enables the inter-operation
of interface types from different packages, even if some of their
component types happen to be named differently).

1. What is a short input program that triggers the error?

package main
type Y int
type X1 interface {
    foo() int
}
type X2 interface {
    foo() Y
}
type T1 bool
func (t T1) foo() int {
    return 99
}

type T2 bool
func (t T2) foo() Y {
    return 99
}

func main() {
    t1 := T1(false)
    t2 := T2(t1)       // demonstrate conversion of type literals

    x1 := X1(t1)
    x2 := X2(t2)

    fail := X2(x1)

    t1.foo()
    t2.foo()
    x1.foo()
    x2.foo()
    fail.foo()
}


2. What is the full compiler output?

tst.go:26: need type assertion to use X1 as X2
    missing foo() Y



3. What version of the compiler are you using?  (Run it with the -V flag.)

8g version 5516+

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