aboutsummaryrefslogtreecommitdiff
path: root/src/go/types/instantiate_test.go
blob: 0c66acb8750c4b4c325e03c248ae6fb46b7d2219 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package types_test

import (
	. "go/types"
	"strings"
	"testing"
)

func TestInstantiateEquality(t *testing.T) {
	const src = genericPkg + "p; type T[P any] int"

	pkg, err := pkgFor(".", src, nil)
	if err != nil {
		t.Fatal(err)
	}

	T := pkg.Scope().Lookup("T").Type().(*Named)

	// Instantiating the same type twice should result in pointer-equivalent
	// instances.
	env := NewEnvironment()
	res1, err := Instantiate(env, T, []Type{Typ[Int]}, false)
	if err != nil {
		t.Fatal(err)
	}
	res2, err := Instantiate(env, T, []Type{Typ[Int]}, false)
	if err != nil {
		t.Fatal(err)
	}

	if res1 != res2 {
		t.Errorf("first instance (%s) not pointer-equivalent to second instance (%s)", res1, res2)
	}
}

func TestInstantiateNonEquality(t *testing.T) {
	const src = genericPkg + "p; type T[P any] int"

	pkg1, err := pkgFor(".", src, nil)
	if err != nil {
		t.Fatal(err)
	}
	pkg2, err := pkgFor(".", src, nil)
	if err != nil {
		t.Fatal(err)
	}

	// We consider T1 and T2 to be distinct types, so their instances should not
	// be deduplicated by the environment.
	T1 := pkg1.Scope().Lookup("T").Type().(*Named)
	T2 := pkg2.Scope().Lookup("T").Type().(*Named)

	env := NewEnvironment()
	res1, err := Instantiate(env, T1, []Type{Typ[Int]}, false)
	if err != nil {
		t.Fatal(err)
	}
	res2, err := Instantiate(env, T2, []Type{Typ[Int]}, false)
	if err != nil {
		t.Fatal(err)
	}

	if res1 == res2 {
		t.Errorf("instance from pkg1 (%s) is pointer-equivalent to instance from pkg2 (%s)", res1, res2)
	}
	if Identical(res1, res2) {
		t.Errorf("instance from pkg1 (%s) is identical to instance from pkg2 (%s)", res1, res2)
	}
}

func TestMethodInstantiation(t *testing.T) {
	const prefix = genericPkg + `p

type T[P any] struct{}

var X T[int]

`
	tests := []struct {
		decl string
		want string
	}{
		{"func (r T[P]) m() P", "func (T[int]).m() int"},
		{"func (r T[P]) m(P)", "func (T[int]).m(int)"},
		{"func (r T[P]) m() func() P", "func (T[int]).m() func() int"},
		{"func (r T[P]) m() T[P]", "func (T[int]).m() T[int]"},
		{"func (r T[P]) m(T[P])", "func (T[int]).m(T[int])"},
		{"func (r T[P]) m(T[P], P, string)", "func (T[int]).m(T[int], int, string)"},
		{"func (r T[P]) m(T[P], T[string], T[int])", "func (T[int]).m(T[int], T[string], T[int])"},
	}

	for _, test := range tests {
		src := prefix + test.decl
		pkg, err := pkgFor(".", src, nil)
		if err != nil {
			t.Fatal(err)
		}
		typ := pkg.Scope().Lookup("X").Type().(*Named)
		obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m")
		m, _ := obj.(*Func)
		if m == nil {
			t.Fatalf(`LookupFieldOrMethod(%s, "m") = %v, want func m`, typ, obj)
		}
		if got := ObjectString(m, RelativeTo(pkg)); got != test.want {
			t.Errorf("instantiated %q, want %q", got, test.want)
		}
	}
}

func TestImmutableSignatures(t *testing.T) {
	const src = genericPkg + `p

type T[P any] struct{}

func (T[P]) m() {}

var _ T[int]
`
	pkg, err := pkgFor(".", src, nil)
	if err != nil {
		t.Fatal(err)
	}
	typ := pkg.Scope().Lookup("T").Type().(*Named)
	obj, _, _ := LookupFieldOrMethod(typ, false, pkg, "m")
	if obj == nil {
		t.Fatalf(`LookupFieldOrMethod(%s, "m") = %v, want func m`, typ, obj)
	}

	// Verify that the original method is not mutated by instantiating T (this
	// bug manifested when subst did not return a new signature).
	want := "func (T[P]).m()"
	if got := stripAnnotations(ObjectString(obj, RelativeTo(pkg))); got != want {
		t.Errorf("instantiated %q, want %q", got, want)
	}
}

// Copied from errors.go.
func stripAnnotations(s string) string {
	var b strings.Builder
	for _, r := range s {
		// strip #'s and subscript digits
		if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
			b.WriteRune(r)
		}
	}
	if b.Len() < len(s) {
		return b.String()
	}
	return s
}