File: generics.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.5.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 16,592 kB
  • sloc: javascript: 2,011; asm: 1,635; sh: 192; yacc: 155; makefile: 52; ansic: 8
file content (79 lines) | stat: -rw-r--r-- 1,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//go:build ignore
// +build ignore

package main

// Test of generic function calls.

type I interface {
	Foo()
}

type A struct{}

func (a A) Foo() {}

type B struct{}

func (b B) Foo() {}

func instantiated[X I](x X) {
	x.Foo()
}

var a A
var b B

func main() {
	instantiated[A](a) // static call
	instantiated[B](b) // static call

	local[C]().Foo()

	lambda[A]()()()
}

func local[X I]() I {
	var x X
	return x
}

type C struct{}

func (c C) Foo() {}

func lambda[X I]() func() func() {
	return func() func() {
		var x X
		return x.Foo
	}
}

// WANT:
// All calls
//   (*C).Foo --> (C).Foo
//   (A).Foo$bound --> (A).Foo
//   instantiated[main.A] --> (A).Foo
//   instantiated[main.B] --> (B).Foo
//   main --> (*C).Foo
//   main --> (A).Foo$bound
//   main --> (C).Foo
//   main --> instantiated[main.A]
//   main --> instantiated[main.B]
//   main --> lambda[main.A]
//   main --> lambda[main.A]$1
//   main --> local[main.C]
// Reachable functions
//   (*C).Foo
//   (A).Foo
//   (A).Foo$bound
//   (B).Foo
//   (C).Foo
//   instantiated[main.A]
//   instantiated[main.B]
//   lambda[main.A]
//   lambda[main.A]$1
//   local[main.C]
// Reflect types
//   *C
//   C