|
| 1 | +//go:build go1.23 |
| 2 | +// +build go1.23 |
| 3 | + |
| 4 | +package gogen |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "go/types" |
| 9 | + _ "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + invalid operandMode = iota // operand is invalid |
| 14 | + novalue // operand represents no value (result of a function call w/o result) |
| 15 | + builtin // operand is a built-in function |
| 16 | + typexpr // operand is a type |
| 17 | + constant_ // operand is a constant; the operand's typ is a Basic type |
| 18 | + variable // operand is an addressable variable |
| 19 | + mapindex // operand is a map index expression (acts like a variable on lhs, commaok on rhs of an assignment) |
| 20 | + value // operand is a computed value |
| 21 | + nilvalue // operand is the nil value - only used by types2 |
| 22 | + commaok // like value, but operand may be used in a comma,ok expression |
| 23 | + commaerr // like commaok, but second value is error, not boolean |
| 24 | + cgofunc // operand is a cgo function |
| 25 | +) |
| 26 | + |
| 27 | +type errorDesc struct { |
| 28 | + posn positioner |
| 29 | + msg string |
| 30 | +} |
| 31 | + |
| 32 | +// An error_ represents a type-checking error. |
| 33 | +// A new error_ is created with Checker.newError. |
| 34 | +// To report an error_, call error_.report. |
| 35 | +type error_ struct { |
| 36 | + check *types.Checker |
| 37 | + desc []errorDesc |
| 38 | + code int |
| 39 | + soft bool |
| 40 | +} |
| 41 | + |
| 42 | +//go:linkname checker_infer123 go/types.(*Checker).infer |
| 43 | +func checker_infer123(check *types.Checker, posn positioner, tparams []*types.TypeParam, targs []types.Type, params *Tuple, args []*operand, reverse bool, err *error_) (inferred []types.Type) |
| 44 | + |
| 45 | +func checker_infer(check *types.Checker, posn positioner, tparams []*types.TypeParam, targs []types.Type, params *types.Tuple, args []*operand) (result []types.Type, err error) { |
| 46 | + const CannotInferTypeArgs = 138 |
| 47 | + _err := &error_{check: check, code: CannotInferTypeArgs} |
| 48 | + result = checker_infer123(check, posn, tparams, targs, params, args, true, _err) |
| 49 | + if len(_err.desc) > 0 { |
| 50 | + err = fmt.Errorf("%s", _err.desc[0].msg) |
| 51 | + } |
| 52 | + return |
| 53 | +} |
| 54 | + |
| 55 | +func infer(pkg *Package, posn positioner, tparams []*types.TypeParam, targs []types.Type, params *types.Tuple, args []*operand) (result []types.Type, err error) { |
| 56 | + conf := &types.Config{ |
| 57 | + Error: func(e error) { |
| 58 | + err = e |
| 59 | + if terr, ok := e.(types.Error); ok { |
| 60 | + err = fmt.Errorf("%s", terr.Msg) |
| 61 | + } |
| 62 | + }, |
| 63 | + } |
| 64 | + checker := types.NewChecker(conf, pkg.Fset, pkg.Types, nil) |
| 65 | + result, err = checker_infer(checker, posn, tparams, targs, params, args) |
| 66 | + return |
| 67 | +} |
0 commit comments