Source file
src/go/types/mono_test.go
1
2
3
4
5 package types_test
6
7 import (
8 "errors"
9 "fmt"
10 "go/importer"
11 "go/types"
12 "strings"
13 "testing"
14 )
15
16 func checkMono(t *testing.T, body string) error {
17 src := "package x; import `unsafe`; var _ unsafe.Pointer;\n" + body
18
19 var buf strings.Builder
20 conf := types.Config{
21 Error: func(err error) { fmt.Fprintln(&buf, err) },
22 Importer: importer.Default(),
23 }
24 typecheck(src, &conf, nil)
25 if buf.Len() == 0 {
26 return nil
27 }
28 return errors.New(strings.TrimRight(buf.String(), "\n"))
29 }
30
31 func TestMonoGood(t *testing.T) {
32 for i, good := range goods {
33 if err := checkMono(t, good); err != nil {
34 t.Errorf("%d: unexpected failure: %v", i, err)
35 }
36 }
37 }
38
39 func TestMonoBad(t *testing.T) {
40 for i, bad := range bads {
41 if err := checkMono(t, bad); err == nil {
42 t.Errorf("%d: unexpected success", i)
43 } else {
44 t.Log(err)
45 }
46 }
47 }
48
49 var goods = []string{
50 "func F[T any](x T) { F(x) }",
51 "func F[T, U, V any]() { F[U, V, T](); F[V, T, U]() }",
52 "type Ring[A, B, C any] struct { L *Ring[B, C, A]; R *Ring[C, A, B] }",
53 "func F[T any]() { type U[T any] [unsafe.Sizeof(F[*T])]byte }",
54 "func F[T any]() { type U[T any] [unsafe.Sizeof(F[*T])]byte; var _ U[int] }",
55 "type U[T any] [unsafe.Sizeof(F[*T])]byte; func F[T any]() { var _ U[U[int]] }",
56 "func F[T any]() { type A = int; F[A]() }",
57 }
58
59
60
61 var bads = []string{
62 "func F[T any](x T) { F(&x) }",
63 "func F[T any]() { F[*T]() }",
64 "func F[T any]() { F[[]T]() }",
65 "func F[T any]() { F[[1]T]() }",
66 "func F[T any]() { F[chan T]() }",
67 "func F[T any]() { F[map[*T]int]() }",
68 "func F[T any]() { F[map[error]T]() }",
69 "func F[T any]() { F[func(T)]() }",
70 "func F[T any]() { F[func() T]() }",
71 "func F[T any]() { F[struct{ t T }]() }",
72 "func F[T any]() { F[interface{ t() T }]() }",
73 "type U[_ any] int; func F[T any]() { F[U[T]]() }",
74 "func F[T any]() { type U int; F[U]() }",
75 "func F[T any]() { type U int; F[*U]() }",
76 "type U[T any] int; func (U[T]) m() { var _ U[*T] }",
77 "type U[T any] int; func (*U[T]) m() { var _ U[*T] }",
78 "type U[T1 any] [unsafe.Sizeof(F[*T1])]byte; func F[T2 any]() { var _ U[T2] }",
79 "func F[A, B, C, D, E any]() { F[B, C, D, E, *A]() }",
80 "type U[_ any] int; const X = unsafe.Sizeof(func() { type A[T any] U[A[*T]] })",
81 "func F[T any]() { type A = *T; F[A]() }",
82 "type A[T any] struct { _ A[*T] }",
83 }
84
View as plain text