File: builder_go122_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (198 lines) | stat: -rw-r--r-- 4,682 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2023 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.

//go:build go1.22
// +build go1.22

package ssa_test

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"go/types"
	"testing"

	"golang.org/x/tools/go/expect"
	"golang.org/x/tools/go/ssa"
	"golang.org/x/tools/go/ssa/ssautil"
	"golang.org/x/tools/internal/testenv"
)

// TestMultipleGoversions tests that globals initialized to equivalent
// function literals are compiled based on the different GoVersion in each file.
func TestMultipleGoversions(t *testing.T) {
	var contents = map[string]string{
		"post.go": `
	//go:build go1.22
	package p

	var distinct = func(l []int) {
		for i := range l {
			print(&i)
		}
	}
	`,
		"pre.go": `
	package p

	var same = func(l []int) {
		for i := range l {
			print(&i)
		}
	}
	`,
	}

	fset := token.NewFileSet()
	var files []*ast.File
	for _, fname := range []string{"post.go", "pre.go"} {
		file, err := parser.ParseFile(fset, fname, contents[fname], 0)
		if err != nil {
			t.Fatal(err)
		}
		files = append(files, file)
	}

	pkg := types.NewPackage("p", "")
	conf := &types.Config{Importer: nil, GoVersion: "go1.21"}
	p, _, err := ssautil.BuildPackage(conf, fset, pkg, files, ssa.SanityCheckFunctions)
	if err != nil {
		t.Fatal(err)
	}

	// Test that global is initialized to a function literal that was
	// compiled to have the expected for loop range variable lifetime for i.
	for _, test := range []struct {
		global *ssa.Global
		want   string // basic block to []*ssa.Alloc.
	}{
		{p.Var("same"), "map[entry:[new int (i)]]"},               // i is allocated in the entry block.
		{p.Var("distinct"), "map[rangeindex.body:[new int (i)]]"}, // i is allocated in the body block.
	} {
		// Find the function the test.name global is initialized to.
		var fn *ssa.Function
		for _, b := range p.Func("init").Blocks {
			for _, instr := range b.Instrs {
				if s, ok := instr.(*ssa.Store); ok && s.Addr == test.global {
					fn, _ = s.Val.(*ssa.Function)
				}
			}
		}
		if fn == nil {
			t.Fatalf("Failed to find *ssa.Function for initial value of global %s", test.global)
		}

		allocs := make(map[string][]string) // block comments -> []Alloc
		for _, b := range fn.Blocks {
			for _, instr := range b.Instrs {
				if a, ok := instr.(*ssa.Alloc); ok {
					allocs[b.Comment] = append(allocs[b.Comment], a.String())
				}
			}
		}
		if got := fmt.Sprint(allocs); got != test.want {
			t.Errorf("[%s:=%s] expected the allocations to be in the basic blocks %q, got %q", test.global, fn, test.want, got)
		}
	}
}

const rangeOverIntSrc = `
package p

type I uint8

func noKey(x int) {
	for range x {
		// does not crash
	}
}

func untypedConstantOperand() {
	for i := range 10 {
		print(i) /*@ types("int")*/
	}
}

func unsignedOperand(x uint64) {
	for i := range x {
		print(i) /*@ types("uint64")*/
	}
}

func namedOperand(x I) {
	for i := range x {
		print(i)  /*@ types("p.I")*/
	}
}

func typeparamOperand[T int](x T) {
	for i := range x {
		print(i)  /*@ types("T")*/
	}
}

func assignment(x I) {
	var k I
	for k = range x {
		print(k) /*@ types("p.I")*/
	}
}
`

// TestRangeOverInt tests that, in a range-over-int (#61405),
// the type of each range var v (identified by print(v) calls)
// has the expected type.
func TestRangeOverInt(t *testing.T) {
	testenv.NeedsGoExperiment(t, "range")

	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "p.go", rangeOverIntSrc, parser.ParseComments)
	if err != nil {
		t.Fatal(err)
	}

	pkg := types.NewPackage("p", "")
	conf := &types.Config{}
	p, _, err := ssautil.BuildPackage(conf, fset, pkg, []*ast.File{f}, ssa.SanityCheckFunctions)
	if err != nil {
		t.Fatal(err)
	}

	// Collect all notes in f, i.e. comments starting with "//@ types".
	notes, err := expect.ExtractGo(fset, f)
	if err != nil {
		t.Fatal(err)
	}

	// Collect calls to the built-in print function.
	fns := make(map[*ssa.Function]bool)
	for _, mem := range p.Members {
		if fn, ok := mem.(*ssa.Function); ok {
			fns[fn] = true
		}
	}
	probes := callsTo(fns, "print")
	expectations := matchNotes(fset, notes, probes)

	for call := range probes {
		if expectations[call] == nil {
			t.Errorf("Unmatched call: %v @ %s", call, fset.Position(call.Pos()))
		}
	}

	// Check each expectation.
	for call, note := range expectations {
		var args []string
		for _, a := range call.Args {
			args = append(args, a.Type().String())
		}
		if got, want := fmt.Sprint(args), fmt.Sprint(note.Args); got != want {
			at := fset.Position(call.Pos())
			t.Errorf("%s: arguments to print had types %s, want %s", at, got, want)
			logFunction(t, probes[call])
		}
	}
}