| 1 | // Copyright 2024 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 typesinternal_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "go/ast" |
| 10 | "go/parser" |
| 11 | "go/printer" |
| 12 | "go/token" |
| 13 | "go/types" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "golang.org/x/tools/internal/testenv" |
| 18 | "golang.org/x/tools/internal/typesinternal" |
| 19 | ) |
| 20 | |
| 21 | func TestZeroValue(t *testing.T) { |
| 22 | if testenv.Go1Point() == 23 { |
| 23 | testenv.NeedsGoExperiment(t, "aliastypeparams") |
| 24 | } |
| 25 | |
| 26 | // This test only reference types/functions defined within the same package. |
| 27 | // We can safely drop the package name when encountered. |
| 28 | qual := types.Qualifier(func(p *types.Package) string { |
| 29 | return "" |
| 30 | }) |
| 31 | src := ` |
| 32 | package main |
| 33 | |
| 34 | type foo struct{ |
| 35 | bar string |
| 36 | } |
| 37 | |
| 38 | type aliasFoo = foo |
| 39 | |
| 40 | type namedInt int |
| 41 | type namedString string |
| 42 | type namedBool bool |
| 43 | type namedPointer *foo |
| 44 | type namedSlice []foo |
| 45 | type namedInterface interface{ Error() string } |
| 46 | type namedChan chan int |
| 47 | type namedMap map[string]foo |
| 48 | type namedSignature func(string) string |
| 49 | type namedStruct struct{ bar string } |
| 50 | type namedArray [3]foo |
| 51 | type namedAlias aliasFoo |
| 52 | |
| 53 | type aliasInt = int |
| 54 | type aliasString = string |
| 55 | type aliasBool = bool |
| 56 | type aliasPointer = *foo |
| 57 | type aliasSlice = []foo |
| 58 | type aliasInterface = interface{ Error() string } |
| 59 | type aliasChan = chan int |
| 60 | type aliasMap = map[string]foo |
| 61 | type aliasSignature = func(string) string |
| 62 | type aliasStruct = struct{ bar string } |
| 63 | type aliasArray = [3]foo |
| 64 | type aliasNamed = foo |
| 65 | |
| 66 | func _[T any]() { |
| 67 | type aliasTypeParam = T |
| 68 | |
| 69 | type aliasWithTypeParam[u any] = struct { |
| 70 | x u |
| 71 | y T |
| 72 | } |
| 73 | type aliasWithTypeParams[u, q any] = struct { |
| 74 | x u |
| 75 | y q |
| 76 | z T |
| 77 | } |
| 78 | |
| 79 | type namedWithTypeParam[u any] struct { |
| 80 | x u |
| 81 | y T |
| 82 | } |
| 83 | type namedWithTypeParams[u, q any] struct{ |
| 84 | x u |
| 85 | y q |
| 86 | z T |
| 87 | } |
| 88 | var ( |
| 89 | _ int // 0 |
| 90 | _ bool // false |
| 91 | _ string // "" |
| 92 | |
| 93 | _ *foo // nil |
| 94 | _ []string // nil |
| 95 | _ []foo // nil |
| 96 | _ interface{ Error() string } // nil |
| 97 | _ chan foo // nil |
| 98 | _ map[string]foo // nil |
| 99 | _ func(string) string // nil |
| 100 | |
| 101 | _ namedInt // 0 |
| 102 | _ namedString // "" |
| 103 | _ namedBool // false |
| 104 | _ namedSlice // nil |
| 105 | _ namedInterface // nil |
| 106 | _ namedChan // nil |
| 107 | _ namedMap// nil |
| 108 | _ namedSignature // nil |
| 109 | _ namedStruct // namedStruct{} |
| 110 | _ namedArray // namedArray{} |
| 111 | _ namedAlias // namedAlias{} |
| 112 | |
| 113 | _ aliasInt // 0 |
| 114 | _ aliasString // "" |
| 115 | _ aliasBool // false |
| 116 | _ aliasSlice // nil |
| 117 | _ aliasInterface // nil |
| 118 | _ aliasChan // nil |
| 119 | _ aliasMap// nil |
| 120 | _ aliasSignature // nil |
| 121 | _ aliasStruct // aliasStruct{} |
| 122 | _ aliasArray // aliasArray{} |
| 123 | _ aliasNamed // aliasNamed{} |
| 124 | |
| 125 | _ [4]string // [4]string{} |
| 126 | _ [5]foo // [5]foo{} |
| 127 | _ foo // foo{} |
| 128 | _ struct{f foo} // struct{f foo}{} |
| 129 | |
| 130 | _ T // *new(T) |
| 131 | _ *T // nil |
| 132 | |
| 133 | _ aliasTypeParam // *new(T) |
| 134 | _ *aliasTypeParam // nil |
| 135 | |
| 136 | _ aliasWithTypeParam[int] // aliasWithTypeParam[int]{} |
| 137 | _ aliasWithTypeParams[int, string] // aliasWithTypeParams[int, string]{} |
| 138 | |
| 139 | _ namedWithTypeParam[int] // namedWithTypeParam[int]{} |
| 140 | _ namedWithTypeParams[int, string] // namedWithTypeParams[int, string]{} |
| 141 | ) |
| 142 | } |
| 143 | ` |
| 144 | fset := token.NewFileSet() |
| 145 | f, err := parser.ParseFile(fset, "p.go", src, parser.ParseComments) |
| 146 | if err != nil { |
| 147 | t.Fatalf("parse file error %v on file source:\n%s\n", err, src) |
| 148 | } |
| 149 | info := &types.Info{ |
| 150 | Types: make(map[ast.Expr]types.TypeAndValue), |
| 151 | Defs: make(map[*ast.Ident]types.Object), |
| 152 | Uses: make(map[*ast.Ident]types.Object), |
| 153 | } |
| 154 | var conf types.Config |
| 155 | pkg, err := conf.Check("", fset, []*ast.File{f}, info) |
| 156 | if err != nil { |
| 157 | t.Fatalf("type check error %v on file source:\n%s\n", err, src) |
| 158 | } |
| 159 | |
| 160 | fun, ok := f.Decls[len(f.Decls)-1].(*ast.FuncDecl) |
| 161 | if !ok { |
| 162 | t.Fatalf("the last decl of the file is not FuncDecl") |
| 163 | } |
| 164 | |
| 165 | decl, ok := fun.Body.List[len(fun.Body.List)-1].(*ast.DeclStmt).Decl.(*ast.GenDecl) |
| 166 | if !ok { |
| 167 | t.Fatalf("the last statement of the function is not GenDecl") |
| 168 | } |
| 169 | |
| 170 | for _, spec := range decl.Specs { |
| 171 | s, ok := spec.(*ast.ValueSpec) |
| 172 | if !ok { |
| 173 | t.Fatalf("%s: got %T, want ValueSpec", fset.Position(spec.Pos()), spec) |
| 174 | } |
| 175 | want := strings.TrimSpace(s.Comment.Text()) |
| 176 | |
| 177 | typ := info.TypeOf(s.Type) |
| 178 | got, _ := typesinternal.ZeroString(typ, qual) |
| 179 | if got != want { |
| 180 | t.Errorf("%s: ZeroString() = %q, want zero value %q", fset.Position(spec.Pos()), got, want) |
| 181 | } |
| 182 | |
| 183 | zeroExpr, _ := typesinternal.ZeroExpr(typ, typesinternal.FileQualifier(f, pkg)) |
| 184 | var bytes bytes.Buffer |
| 185 | printer.Fprint(&bytes, fset, zeroExpr) |
| 186 | got = bytes.String() |
| 187 | if got != want { |
| 188 | t.Errorf("%s: ZeroExpr() = %q, want zero value %q", fset.Position(spec.Pos()), got, want) |
| 189 | } |
| 190 | } |
| 191 | } |