Debian Codegraph @

go/ssa/methods_test.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package ssa_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/
6
7 import (
8 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
9 "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/"
10 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
11 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
12 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
13
14 "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/"
15 "golang.org/x/tools/go/ssa/ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/"
16 )
17
18 // Tests that MethodValue returns the expected method.
19 func TestMethodValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestMethodValue().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
20 input := `
21 package p
22
23 type I interface{ M() }
24
25 type S int
26 func (S) M() {}
27 type R[T any] struct{ S }
28
29 var i I
30 var s S
31 var r R[string]
32
33 func selections[T any]() {
34 _ = i.M
35 _ = s.M
36 _ = r.M
37
38 var v R[T]
39 _ = v.M
40 }
41 `
42
43 // Parse the file.
44 fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().()
45 f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "input.go", input, 0)
46 if err != nil {
47 t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err)
48 return
49 }
50
51 // Build an SSA program from the parsed file.
52 p, info, err := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.BuildPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/BuildPackage().(&typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#{}, fset,
53 typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPackagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPackage().("p", ""), []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.)
54 if err != nil {
55 t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().(err)
56 return
57 }
58
59 // Collect all of the *types.Selection in the function "selections".
60 var selections []*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Selectionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#
61 for _, decl := range f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. {
62 if fn, ok := decl.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#); ok && fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == "selections" {
63 for _, stmt := range fn.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List. {
64 if assign, ok := stmt.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#); ok {
65 sel := assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.[0].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#)
66 selections = append(selections, info.Selectionsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Selections.[sel])
67 }
68 }
69 }
70 }
71
72 wants := map[string]string{
73 "method (p.S) M()": "(p.S).M",
74 "method (p.R[string]) M()": "(p.R[string]).M",
75 "method (p.I) M()": "nil", // interface
76 "method (p.R[T]) M()": "nil", // parameterized
77 }
78 if len(wants) != len(selections) {
79 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Wanted %d selections. got %d", len(wants), len(selections))
80 }
81 for _, selection := range selections {
82 var got string
83 if m := p.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog..MethodValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodValue().(selection); m != nil {
84 got = m.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#String().()
85 } else {
86 got = "nil"
87 }
88 if want := wants[selection.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#String().()]; want != got {
89 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("p.Prog.MethodValue(%s) expected %q. got %q", selection, want, got)
90 }
91 }
92 }