File: usesgenerics.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (83 lines) | stat: -rw-r--r-- 2,447 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package usesgenerics

import (
	_ "embed"
	"reflect"

	"golang.org/x/tools/go/analysis"
	"golang.org/x/tools/go/analysis/passes/inspect"
	"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
	"golang.org/x/tools/go/ast/inspector"
	"golang.org/x/tools/internal/typeparams/genericfeatures"
)

//go:embed doc.go
var doc string

var Analyzer = &analysis.Analyzer{
	Name:       "usesgenerics",
	Doc:        analysisutil.MustExtractDoc(doc, "usesgenerics"),
	URL:        "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/usesgenerics",
	Requires:   []*analysis.Analyzer{inspect.Analyzer},
	Run:        run,
	ResultType: reflect.TypeOf((*Result)(nil)),
	FactTypes:  []analysis.Fact{new(featuresFact)},
}

type Features = genericfeatures.Features

const (
	GenericTypeDecls  = genericfeatures.GenericTypeDecls
	GenericFuncDecls  = genericfeatures.GenericFuncDecls
	EmbeddedTypeSets  = genericfeatures.EmbeddedTypeSets
	TypeInstantiation = genericfeatures.TypeInstantiation
	FuncInstantiation = genericfeatures.FuncInstantiation
)

// Result is the usesgenerics analyzer result type. The Direct field records
// features used directly by the package being analyzed (i.e. contained in the
// package source code). The Transitive field records any features used by the
// package or any of its transitive imports.
type Result struct {
	Direct, Transitive Features
}

type featuresFact struct {
	Features Features
}

func (f *featuresFact) AFact()         {}
func (f *featuresFact) String() string { return f.Features.String() }

func run(pass *analysis.Pass) (interface{}, error) {
	inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

	direct := genericfeatures.ForPackage(inspect, pass.TypesInfo)

	transitive := direct | importedTransitiveFeatures(pass)
	if transitive != 0 {
		pass.ExportPackageFact(&featuresFact{transitive})
	}

	return &Result{
		Direct:     direct,
		Transitive: transitive,
	}, nil
}

// importedTransitiveFeatures computes features that are used transitively via
// imports.
func importedTransitiveFeatures(pass *analysis.Pass) Features {
	var feats Features
	for _, imp := range pass.Pkg.Imports() {
		var importedFact featuresFact
		if pass.ImportPackageFact(imp, &importedFact) {
			feats |= importedFact.Features
		}
	}
	return feats
}