| 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 aliases_test |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | "go/parser" |
| 10 | "go/token" |
| 11 | "go/types" |
| 12 | "testing" |
| 13 | |
| 14 | "golang.org/x/tools/internal/aliases" |
| 15 | "golang.org/x/tools/internal/testenv" |
| 16 | ) |
| 17 | |
| 18 | // TestNew tests that [aliases.New] creates a *types.Alias of a type |
| 19 | // whose underlying and Unaliased type is *Named. |
| 20 | func TestNew(t *testing.T) { |
| 21 | const source = ` |
| 22 | package p |
| 23 | |
| 24 | type Named int |
| 25 | ` |
| 26 | fset := token.NewFileSet() |
| 27 | f, err := parser.ParseFile(fset, "hello.go", source, 0) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | |
| 32 | var conf types.Config |
| 33 | pkg, err := conf.Check("p", fset, []*ast.File{f}, nil) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | |
| 38 | expr := `*Named` |
| 39 | tv, err := types.Eval(fset, pkg, 0, expr) |
| 40 | if err != nil { |
| 41 | t.Fatalf("Eval(%s) failed: %v", expr, err) |
| 42 | } |
| 43 | |
| 44 | A := aliases.New(token.NoPos, pkg, "A", tv.Type, nil) |
| 45 | if got, want := A.Name(), "A"; got != want { |
| 46 | t.Errorf("Expected A.Name()==%q. got %q", want, got) |
| 47 | } |
| 48 | |
| 49 | if got, want := A.Type().Underlying(), tv.Type; got != want { |
| 50 | t.Errorf("Expected A.Type().Underlying()==%q. got %q", want, got) |
| 51 | } |
| 52 | if got, want := types.Unalias(A.Type()), tv.Type; got != want { |
| 53 | t.Errorf("Expected Unalias(A)==%q. got %q", want, got) |
| 54 | } |
| 55 | if _, ok := A.Type().(*types.Alias); !ok { |
| 56 | t.Errorf("Expected A.Type() to be a *types.Alias; got %q", A.Type()) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // TestNewParameterizedAlias tests that [aliases.New] can create a parameterized alias |
| 61 | // A[T] of a type whose underlying and Unaliased type is *T. The test then |
| 62 | // instantiates A[Named] and checks that the underlying and Unaliased type |
| 63 | // of A[Named] is *Named. |
| 64 | // |
| 65 | // Requires aliastypeparams GOEXPERIMENT. |
| 66 | func TestNewParameterizedAlias(t *testing.T) { |
| 67 | testenv.NeedsGo1Point(t, 23) |
| 68 | if testenv.Go1Point() == 23 { |
| 69 | testenv.NeedsGoExperiment(t, "aliastypeparams") |
| 70 | } |
| 71 | |
| 72 | const source = ` |
| 73 | package p |
| 74 | |
| 75 | type Named int |
| 76 | ` |
| 77 | fset := token.NewFileSet() |
| 78 | f, err := parser.ParseFile(fset, "hello.go", source, 0) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | |
| 83 | var conf types.Config |
| 84 | pkg, err := conf.Check("p", fset, []*ast.File{f}, nil) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | |
| 89 | // type A[T ~int] = *T |
| 90 | tparam := types.NewTypeParam( |
| 91 | types.NewTypeName(token.NoPos, pkg, "T", nil), |
| 92 | types.NewUnion([]*types.Term{types.NewTerm(true, types.Typ[types.Int])}), |
| 93 | ) |
| 94 | ptrT := types.NewPointer(tparam) |
| 95 | A := aliases.New(token.NoPos, pkg, "A", ptrT, []*types.TypeParam{tparam}) |
| 96 | if got, want := A.Name(), "A"; got != want { |
| 97 | t.Errorf("New: got %q, want %q", got, want) |
| 98 | } |
| 99 | |
| 100 | if got, want := A.Type().Underlying(), ptrT; !types.Identical(got, want) { |
| 101 | t.Errorf("A.Type().Underlying (%q) is not identical to %q", got, want) |
| 102 | } |
| 103 | if got, want := types.Unalias(A.Type()), ptrT; !types.Identical(got, want) { |
| 104 | t.Errorf("Unalias(A)==%q is not identical to %q", got, want) |
| 105 | } |
| 106 | |
| 107 | if _, ok := A.Type().(*types.Alias); !ok { |
| 108 | t.Errorf("Expected A.Type() to be a types.Alias(). got %q", A.Type()) |
| 109 | } |
| 110 | |
| 111 | pkg.Scope().Insert(A) // Add A to pkg so it is available to types.Eval. |
| 112 | |
| 113 | named, ok := pkg.Scope().Lookup("Named").(*types.TypeName) |
| 114 | if !ok { |
| 115 | t.Fatalf("Failed to Lookup(%q) in package %s", "Named", pkg) |
| 116 | } |
| 117 | ptrNamed := types.NewPointer(named.Type()) |
| 118 | |
| 119 | const expr = `A[Named]` |
| 120 | tv, err := types.Eval(fset, pkg, 0, expr) |
| 121 | if err != nil { |
| 122 | t.Fatalf("Eval(%s) failed: %v", expr, err) |
| 123 | } |
| 124 | |
| 125 | if got, want := tv.Type.Underlying(), ptrNamed; !types.Identical(got, want) { |
| 126 | t.Errorf("A[Named].Type().Underlying (%q) is not identical to %q", got, want) |
| 127 | } |
| 128 | if got, want := types.Unalias(tv.Type), ptrNamed; !types.Identical(got, want) { |
| 129 | t.Errorf("Unalias(A[Named])==%q is not identical to %q", got, want) |
| 130 | } |
| 131 | } |