Welcome to mirror list, hosted at ThFree Co, Russian Federation.

concrete.go « functions « tools « go « honnef.co « vendor - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 932acd03edaec94a38053948d9e67ec801d095b6 (plain)
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
package functions

import (
	"go/token"
	"go/types"

	"honnef.co/go/tools/ssa"
)

func concreteReturnTypes(fn *ssa.Function) []*types.Tuple {
	res := fn.Signature.Results()
	if res == nil {
		return nil
	}
	ifaces := make([]bool, res.Len())
	any := false
	for i := 0; i < res.Len(); i++ {
		_, ifaces[i] = res.At(i).Type().Underlying().(*types.Interface)
		any = any || ifaces[i]
	}
	if !any {
		return []*types.Tuple{res}
	}
	var out []*types.Tuple
	for _, block := range fn.Blocks {
		if len(block.Instrs) == 0 {
			continue
		}
		ret, ok := block.Instrs[len(block.Instrs)-1].(*ssa.Return)
		if !ok {
			continue
		}
		vars := make([]*types.Var, res.Len())
		for i, v := range ret.Results {
			var typ types.Type
			if !ifaces[i] {
				typ = res.At(i).Type()
			} else if mi, ok := v.(*ssa.MakeInterface); ok {
				// TODO(dh): if mi.X is a function call that returns
				// an interface, call concreteReturnTypes on that
				// function (or, really, go through Descriptions,
				// avoid infinite recursion etc, just like nil error
				// detection)

				// TODO(dh): support Phi nodes
				typ = mi.X.Type()
			} else {
				typ = res.At(i).Type()
			}
			vars[i] = types.NewParam(token.NoPos, nil, "", typ)
		}
		out = append(out, types.NewTuple(vars...))
	}
	// TODO(dh): deduplicate out
	return out
}