1
2
3
4
5
6
7
8 package aliases
9
10 import (
11 "go/ast"
12 "go/parser"
13 "go/token"
14 "go/types"
15 )
16
17
18 type Alias = types.Alias
19
20
21 func Rhs(alias *Alias) types.Type {
22 if alias, ok := any(alias).(interface{ Rhs() types.Type }); ok {
23 return alias.Rhs()
24 }
25
26
27
28 return Unalias(alias)
29 }
30
31
32 func TypeParams(alias *Alias) *types.TypeParamList {
33 if alias, ok := any(alias).(interface{ TypeParams() *types.TypeParamList }); ok {
34 return alias.TypeParams()
35 }
36 return nil
37 }
38
39
40 func SetTypeParams(alias *Alias, tparams []*types.TypeParam) {
41 if alias, ok := any(alias).(interface {
42 SetTypeParams(tparams []*types.TypeParam)
43 }); ok {
44 alias.SetTypeParams(tparams)
45 } else if len(tparams) > 0 {
46 panic("cannot set type parameters of an Alias type in go1.22")
47 }
48 }
49
50
51 func TypeArgs(alias *Alias) *types.TypeList {
52 if alias, ok := any(alias).(interface{ TypeArgs() *types.TypeList }); ok {
53 return alias.TypeArgs()
54 }
55 return nil
56 }
57
58
59
60 func Origin(alias *Alias) *Alias {
61 if alias, ok := any(alias).(interface{ Origin() *types.Alias }); ok {
62 return alias.Origin()
63 }
64 return alias
65 }
66
67
68 func Unalias(t types.Type) types.Type { return types.Unalias(t) }
69
70
71
72
73 func newAlias(tname *types.TypeName, rhs types.Type, tparams []*types.TypeParam) *Alias {
74 a := types.NewAlias(tname, rhs)
75 SetTypeParams(a, tparams)
76 return a
77 }
78
79
80
81
82 func Enabled() bool {
83
84
85
86
87
88
89
90
91
92
93 fset := token.NewFileSet()
94 f, _ := parser.ParseFile(fset, "a.go", "package p; type A = int", 0)
95 pkg, _ := new(types.Config).Check("p", fset, []*ast.File{f}, nil)
96 _, enabled := pkg.Scope().Lookup("A").Type().(*types.Alias)
97 return enabled
98 }
99
View as plain text