| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"go/ast" |
| 14 |
"go/parser" |
| 15 |
"go/token" |
| 16 |
"go/types" |
| 17 |
"log" |
| 18 |
) |
| 19 |
|
| 20 |
const src = ` |
| 21 |
//!+input |
| 22 |
package p |
| 23 |
|
| 24 |
type Pair[L, R comparable] struct { |
| 25 |
left L |
| 26 |
right R |
| 27 |
} |
| 28 |
|
| 29 |
func (p Pair[L, _]) Left() L { |
| 30 |
return p.left |
| 31 |
} |
| 32 |
|
| 33 |
func Equal[L, R comparable](x, y Pair[L, R]) bool { |
| 34 |
return x.left == y.left && x.right == y.right |
| 35 |
} |
| 36 |
|
| 37 |
var X Pair[int, string] |
| 38 |
var Y Pair[string, int] |
| 39 |
|
| 40 |
var E = Equal[int, string] |
| 41 |
//!-input |
| 42 |
` |
| 43 |
|
| 44 |
|
| 45 |
func CheckInstances(fset *token.FileSet, file *ast.File) (*types.Package, error) { |
| 46 |
conf := types.Config{} |
| 47 |
info := &types.Info{ |
| 48 |
Instances: make(map[*ast.Ident]types.Instance), |
| 49 |
} |
| 50 |
pkg, err := conf.Check("p", fset, []*ast.File{file}, info) |
| 51 |
for id, inst := range info.Instances { |
| 52 |
posn := fset.Position(id.Pos()) |
| 53 |
fmt.Printf("%s: %s instantiated with %s: %s\n", posn, id.Name, FormatTypeList(inst.TypeArgs), inst.Type) |
| 54 |
} |
| 55 |
return pkg, err |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
func FormatTypeList(list *types.TypeList) string { |
| 72 |
var buf bytes.Buffer |
| 73 |
buf.WriteRune('[') |
| 74 |
for i := 0; i < list.Len(); i++ { |
| 75 |
if i > 0 { |
| 76 |
buf.WriteString(", ") |
| 77 |
} |
| 78 |
buf.WriteString(list.At(i).String()) |
| 79 |
} |
| 80 |
buf.WriteRune(']') |
| 81 |
return buf.String() |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
func Instantiate(pkg *types.Package) error { |
| 86 |
Pair := pkg.Scope().Lookup("Pair").Type() |
| 87 |
X := pkg.Scope().Lookup("X").Type() |
| 88 |
Y := pkg.Scope().Lookup("Y").Type() |
| 89 |
|
| 90 |
|
| 91 |
Compare("X", "Y", X, Y) |
| 92 |
|
| 93 |
|
| 94 |
ctxt := types.NewContext() |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
Int, String := types.Typ[types.Int], types.Typ[types.String] |
| 99 |
inst1, _ := types.Instantiate(ctxt, Pair, []types.Type{Int, String}, true) |
| 100 |
Compare("X", "inst1", X, inst1) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
inst2, _ := types.Instantiate(ctxt, Pair, []types.Type{Int, String}, true) |
| 105 |
Compare("inst1", "inst2", inst1, inst2) |
| 106 |
|
| 107 |
|
| 108 |
Any := types.Universe.Lookup("any").Type() |
| 109 |
_, err := types.Instantiate(ctxt, Pair, []types.Type{Int, Any}, true) |
| 110 |
var argErr *types.ArgumentError |
| 111 |
if errors.As(err, &argErr) { |
| 112 |
fmt.Printf("Argument %d: %v\n", argErr.Index, argErr.Err) |
| 113 |
} |
| 114 |
|
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
func Compare(leftName, rightName string, left, right types.Type) { |
| 119 |
fmt.Printf("Identical(%s, %s) : %t\n", leftName, rightName, types.Identical(left, right)) |
| 120 |
fmt.Printf("%s == %s : %t\n\n", leftName, rightName, left == right) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
func main() { |
| 144 |
fset := token.NewFileSet() |
| 145 |
f, err := parser.ParseFile(fset, "hello.go", src, 0) |
| 146 |
if err != nil { |
| 147 |
log.Fatal(err) |
| 148 |
} |
| 149 |
fmt.Println("=== CheckInstances ===") |
| 150 |
pkg, err := CheckInstances(fset, f) |
| 151 |
if err != nil { |
| 152 |
log.Fatal(err) |
| 153 |
} |
| 154 |
fmt.Println("=== Instantiate ===") |
| 155 |
if err := Instantiate(pkg); err != nil { |
| 156 |
log.Fatal(err) |
| 157 |
} |
| 158 |
} |