|
| 1 | +// Copyright 2021 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 usesgenerics defines an Analyzer that checks for usage of generic |
| 6 | +// features added in Go 1.18. |
| 7 | +package usesgenerics |
| 8 | + |
| 9 | +import ( |
| 10 | + "go/ast" |
| 11 | + "go/types" |
| 12 | + "reflect" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "golang.org/x/tools/go/analysis" |
| 16 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 17 | + "golang.org/x/tools/go/ast/inspector" |
| 18 | + "golang.org/x/tools/internal/typeparams" |
| 19 | +) |
| 20 | + |
| 21 | +var Analyzer = &analysis.Analyzer{ |
| 22 | + Name: "usesgenerics", |
| 23 | + Doc: Doc, |
| 24 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 25 | + Run: run, |
| 26 | + ResultType: reflect.TypeOf((*Result)(nil)), |
| 27 | + FactTypes: []analysis.Fact{new(featuresFact)}, |
| 28 | +} |
| 29 | + |
| 30 | +const Doc = `detect whether a package uses generics features |
| 31 | +
|
| 32 | +The usesgenerics analysis reports whether a package directly or transitively |
| 33 | +uses certain features associated with generic programming in Go.` |
| 34 | + |
| 35 | +// Result is the usesgenerics analyzer result type. The Direct field records |
| 36 | +// features used directly by the package being analyzed (i.e. contained in the |
| 37 | +// package source code). The Transitive field records any features used by the |
| 38 | +// package or any of its transitive imports. |
| 39 | +type Result struct { |
| 40 | + Direct, Transitive Features |
| 41 | +} |
| 42 | + |
| 43 | +// Features is a set of flags reporting which features of generic Go code a |
| 44 | +// package uses, or 0. |
| 45 | +type Features int |
| 46 | + |
| 47 | +const ( |
| 48 | + // GenericTypeDecls indicates whether the package declares types with type |
| 49 | + // parameters. |
| 50 | + GenericTypeDecls Features = 1 << iota |
| 51 | + |
| 52 | + // GenericFuncDecls indicates whether the package declares functions with |
| 53 | + // type parameters. |
| 54 | + GenericFuncDecls |
| 55 | + |
| 56 | + // EmbeddedTypeSets indicates whether the package declares interfaces that |
| 57 | + // contain structural type restrictions, i.e. are not fully described by |
| 58 | + // their method sets. |
| 59 | + EmbeddedTypeSets |
| 60 | + |
| 61 | + // TypeInstantiation indicates whether the package instantiates any generic |
| 62 | + // types. |
| 63 | + TypeInstantiation |
| 64 | + |
| 65 | + // FuncInstantiation indicates whether the package instantiates any generic |
| 66 | + // functions. |
| 67 | + FuncInstantiation |
| 68 | +) |
| 69 | + |
| 70 | +func (f Features) String() string { |
| 71 | + var feats []string |
| 72 | + if f&GenericTypeDecls != 0 { |
| 73 | + feats = append(feats, "typeDecl") |
| 74 | + } |
| 75 | + if f&GenericFuncDecls != 0 { |
| 76 | + feats = append(feats, "funcDecl") |
| 77 | + } |
| 78 | + if f&EmbeddedTypeSets != 0 { |
| 79 | + feats = append(feats, "typeSet") |
| 80 | + } |
| 81 | + if f&TypeInstantiation != 0 { |
| 82 | + feats = append(feats, "typeInstance") |
| 83 | + } |
| 84 | + if f&FuncInstantiation != 0 { |
| 85 | + feats = append(feats, "funcInstance") |
| 86 | + } |
| 87 | + return "features{" + strings.Join(feats, ",") + "}" |
| 88 | +} |
| 89 | + |
| 90 | +type featuresFact struct { |
| 91 | + Features Features |
| 92 | +} |
| 93 | + |
| 94 | +func (f *featuresFact) AFact() {} |
| 95 | +func (f *featuresFact) String() string { return f.Features.String() } |
| 96 | + |
| 97 | +func run(pass *analysis.Pass) (interface{}, error) { |
| 98 | + direct := directFeatures(pass) |
| 99 | + |
| 100 | + transitive := direct | importedTransitiveFeatures(pass) |
| 101 | + if transitive != 0 { |
| 102 | + pass.ExportPackageFact(&featuresFact{transitive}) |
| 103 | + } |
| 104 | + |
| 105 | + return &Result{ |
| 106 | + Direct: direct, |
| 107 | + Transitive: transitive, |
| 108 | + }, nil |
| 109 | +} |
| 110 | + |
| 111 | +// directFeatures computes which generic features are used directly by the |
| 112 | +// package being analyzed. |
| 113 | +func directFeatures(pass *analysis.Pass) Features { |
| 114 | + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 115 | + |
| 116 | + nodeFilter := []ast.Node{ |
| 117 | + (*ast.FuncType)(nil), |
| 118 | + (*ast.InterfaceType)(nil), |
| 119 | + (*ast.ImportSpec)(nil), |
| 120 | + (*ast.TypeSpec)(nil), |
| 121 | + } |
| 122 | + |
| 123 | + var direct Features |
| 124 | + |
| 125 | + inspect.Preorder(nodeFilter, func(node ast.Node) { |
| 126 | + switch n := node.(type) { |
| 127 | + case *ast.FuncType: |
| 128 | + if tparams := typeparams.ForFuncType(n); tparams != nil { |
| 129 | + direct |= GenericFuncDecls |
| 130 | + } |
| 131 | + case *ast.InterfaceType: |
| 132 | + tv := pass.TypesInfo.Types[n] |
| 133 | + if iface, _ := tv.Type.(*types.Interface); iface != nil && !typeparams.IsMethodSet(iface) { |
| 134 | + direct |= EmbeddedTypeSets |
| 135 | + } |
| 136 | + case *ast.TypeSpec: |
| 137 | + if tparams := typeparams.ForTypeSpec(n); tparams != nil { |
| 138 | + direct |= GenericTypeDecls |
| 139 | + } |
| 140 | + } |
| 141 | + }) |
| 142 | + |
| 143 | + instances := typeparams.GetInstances(pass.TypesInfo) |
| 144 | + for _, inst := range instances { |
| 145 | + switch inst.Type.(type) { |
| 146 | + case *types.Named: |
| 147 | + direct |= TypeInstantiation |
| 148 | + case *types.Signature: |
| 149 | + direct |= FuncInstantiation |
| 150 | + } |
| 151 | + } |
| 152 | + return direct |
| 153 | +} |
| 154 | + |
| 155 | +// importedTransitiveFeatures computes features that are used transitively via |
| 156 | +// imports. |
| 157 | +func importedTransitiveFeatures(pass *analysis.Pass) Features { |
| 158 | + var feats Features |
| 159 | + for _, imp := range pass.Pkg.Imports() { |
| 160 | + var importedFact featuresFact |
| 161 | + if pass.ImportPackageFact(imp, &importedFact) { |
| 162 | + feats |= importedFact.Features |
| 163 | + } |
| 164 | + } |
| 165 | + return feats |
| 166 | +} |
0 commit comments