Debian Codegraph @

go/ssa/const_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/constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/"
10 "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/"
11 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
12 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
13 "math/bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/"
14 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
15 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
16
17 "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/"
18 )
19
20 func TestConstStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa_test`/TestConstString().(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#) {
21 const source = `
22 package P
23
24 type Named string
25
26 func fn() (int, bool, string)
27 func gen[T int]() {}
28 `
29 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().()
30 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, "p.go", source, 0)
31 if err != nil {
32 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
33 }
34
35 var conf 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#
36 pkg, err := conf.Checkscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Check().("P", fset, []*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}, nil)
37 if err != nil {
38 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
39 }
40
41 for _, test := range []struct {
42 expr string // type expression
43 constant any // constant value
44 want string // expected String() value
45 }{
46 {"int", int64(0), "0:int"},
47 {"int64", int64(0), "0:int64"},
48 {"float32", int64(0), "0:float32"},
49 {"float32", bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/.NewFloatscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/NewFloat().(1.5), "1.5:float32"},
50 {"bool", false, "false:bool"},
51 {"string", "", `"":string`},
52 {"Named", "", `"":P.Named`},
53 {"struct{x string}", nil, "struct{x string}{}:struct{x string}"},
54 {"[]int", nil, "nil:[]int"},
55 {"[3]int", nil, "[3]int{}:[3]int"},
56 {"*int", nil, "nil:*int"},
57 {"interface{}", nil, "nil:interface{}"},
58 {"interface{string}", nil, `"":interface{string}`},
59 {"interface{int|int64}", nil, "0:interface{int|int64}"},
60 {"interface{bool}", nil, "false:interface{bool}"},
61 {"interface{bool|int}", nil, "invalid:interface{bool|int}"},
62 {"interface{int|string}", nil, "invalid:interface{int|string}"},
63 {"interface{bool|string}", nil, "invalid:interface{bool|string}"},
64 {"interface{struct{x string}}", nil, "invalid:interface{struct{x string}}"},
65 {"interface{int|int64}", int64(1), "1:interface{int|int64}"},
66 {"interface{~bool}", true, "true:interface{~bool}"},
67 {"interface{Named}", "lorem ipsum", `"lorem ipsum":interface{P.Named}`},
68 {"func() (int, bool, string)", nil, "nil:func() (int, bool, string)"},
69 } {
70 // Eval() expr for its type.
71 tv, err := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Evalscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Eval().(fset, pkg, 0, test.expr)
72 if err != nil {
73 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Eval(%s) failed: %v", test.expr, err)
74 }
75 var val constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/Value#
76 if test.constant != nil {
77 val = constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/.Makescip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/Make().(test.constant)
78 }
79 c := ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.NewConstscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/NewConst().(val, tv.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type.)
80 got := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(c.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Const#String().(), " | ", "|") // Accept both interface{a | b} and interface{a|b}.
81 if got != test.want {
82 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ssa.NewConst(%v, %s).String() = %v, want %v", val, tv.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type., got, test.want)
83 }
84 }
85
86 // Test tuples
87 fn := pkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().("fn")
88 tup := fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#).Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Results().()
89 if got, want := ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.NewConstscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/NewConst().(nil, tup).Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Const#String().(), `(0, false, ""):(int, bool, string)`; got != want {
90 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ssa.NewConst(%v, %s).String() = %v, want %v", nil, tup, got, want)
91 }
92
93 // Test type-param
94 gen := pkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().("gen")
95 tp := gen.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#).TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#TypeParams().().Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParamList#At().(0)
96 if got, want := ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.NewConstscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/NewConst().(nil, tp).Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Const#String().(), "0:T"; got != want {
97 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ssa.NewConst(%v, %s).String() = %v, want %v", nil, tup, got, want)
98 }
99 }