| 1 |
Variants of embed-crosspkg.txt: cross-package method promotion via |
| 2 |
pointer embedding, generic embedding, and interface embedding. |
| 3 |
All follow the same resolution path (methodsets.Index.LocationOf) |
| 4 |
as plain struct embedding; this test guards against regressions in |
| 5 |
those forms. |
| 6 |
|
| 7 |
-- go.mod -- |
| 8 |
module example.com |
| 9 |
go 1.18 |
| 10 |
|
| 11 |
-- iface/iface.go -- |
| 12 |
package iface |
| 13 |
|
| 14 |
type I interface { //@loc(I, "I"),implementation("I", SP, SG, J) |
| 15 |
// Implementations of M include J.M (same method set as I); |
| 16 |
// J.M's position is resolved via LocationOf to iface.I.M (IM), |
| 17 |
// since interface embedding shares the original *types.Func. |
| 18 |
M() //@loc(IM, "M"),implementation("M", TM, GM, IM) |
| 19 |
N() //@loc(IN, "N") |
| 20 |
} |
| 21 |
|
| 22 |
-- iface2/iface2.go -- |
| 23 |
package iface2 |
| 24 |
|
| 25 |
import "example.com/iface" |
| 26 |
|
| 27 |
// J embeds iface.I from another package: J.M and J.N are declared |
| 28 |
// in iface, so iface2's index records them with zero Posn. Querying |
| 29 |
// implementations of a concrete N must resolve J.N via iface's index; |
| 30 |
// the result location coincides with iface.I.N (same types.Func). |
| 31 |
type J interface { //@loc(J, "J"),implementation("J", I, SP, SG) |
| 32 |
iface.I |
| 33 |
} |
| 34 |
|
| 35 |
-- impl/impl.go -- |
| 36 |
package impl |
| 37 |
|
| 38 |
type T struct{} |
| 39 |
|
| 40 |
func (*T) M() {} //@loc(TM, "M") |
| 41 |
|
| 42 |
type G[X any] struct{} |
| 43 |
|
| 44 |
func (G[X]) M() {} //@loc(GM, "M") |
| 45 |
|
| 46 |
-- embed/embed.go -- |
| 47 |
package embed |
| 48 |
|
| 49 |
import "example.com/impl" |
| 50 |
|
| 51 |
// SP embeds *impl.T: M is promoted from the pointer receiver. |
| 52 |
type SP struct { //@loc(SP, "SP") |
| 53 |
*impl.T |
| 54 |
} |
| 55 |
|
| 56 |
// Querying implementations of SP.N (a concrete method) finds J.N as |
| 57 |
// well as I.N; J.N's position resolves to IN via iface's index. |
| 58 |
func (SP) N() {} //@implementation("N", IN) |
| 59 |
|
| 60 |
// SG embeds an instantiation of a generic type. |
| 61 |
// M's origin is impl.G[X].M; resolution must match impl's index entry. |
| 62 |
type SG struct { //@loc(SG, "SG") |
| 63 |
impl.G[int] |
| 64 |
} |
| 65 |
|
| 66 |
func (SG) N() {} |