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