Skip to content

Commit 0ac84b9

Browse files
committed
checker_infer for go1.23
1 parent 3d8e76c commit 0ac84b9

File tree

3 files changed

+110
-33
lines changed

3 files changed

+110
-33
lines changed

typeparams.go

+2-33
Original file line numberDiff line numberDiff line change
@@ -212,51 +212,20 @@ func toNamedType(pkg *Package, t *types.Named) ast.Expr {
212212

213213
type operandMode byte
214214

215-
//type builtinId int
215+
type builtinId int
216216

217217
type operand struct {
218218
mode operandMode
219219
expr ast.Expr
220220
typ types.Type
221221
val constant.Value
222-
//id builtinId
222+
id builtinId
223223
}
224224

225-
const (
226-
invalid operandMode = iota // operand is invalid
227-
novalue // operand represents no value (result of a function call w/o result)
228-
builtin // operand is a built-in function
229-
typexpr // operand is a type
230-
constant_ // operand is a constant; the operand's typ is a Basic type
231-
variable // operand is an addressable variable
232-
mapindex // operand is a map index expression (acts like a variable on lhs, commaok on rhs of an assignment)
233-
value // operand is a computed value
234-
commaok // like value, but operand may be used in a comma,ok expression
235-
commaerr // like commaok, but second value is error, not boolean
236-
cgofunc // operand is a cgo function
237-
)
238-
239225
type positioner interface {
240226
Pos() token.Pos
241227
}
242228

243-
//go:linkname checker_infer go/types.(*Checker).infer
244-
func checker_infer(check *types.Checker, posn positioner, tparams []*types.TypeParam, targs []types.Type, params *types.Tuple, args []*operand) (result []types.Type)
245-
246-
func infer(pkg *Package, posn positioner, tparams []*types.TypeParam, targs []types.Type, params *types.Tuple, args []*operand) (result []types.Type, err error) {
247-
conf := &types.Config{
248-
Error: func(e error) {
249-
err = e
250-
if terr, ok := e.(types.Error); ok {
251-
err = fmt.Errorf("%s", terr.Msg)
252-
}
253-
},
254-
}
255-
checker := types.NewChecker(conf, pkg.Fset, pkg.Types, nil)
256-
result = checker_infer(checker, posn, tparams, targs, params, args)
257-
return
258-
}
259-
260229
func getParamsTypes(tuple *types.Tuple, variadic bool) string {
261230
n := tuple.Len()
262231
if n == 0 {

typesinfer.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
commaok // like value, but operand may be used in a comma,ok expression
22+
commaerr // like commaok, but second value is error, not boolean
23+
cgofunc // operand is a cgo function
24+
)
25+
26+
//go:linkname checker_infer go/types.(*Checker).infer
27+
func checker_infer(check *types.Checker, posn positioner, tparams []*types.TypeParam, targs []types.Type, params *types.Tuple, args []*operand) (result []types.Type)
28+
29+
func infer(pkg *Package, posn positioner, tparams []*types.TypeParam, targs []types.Type, params *types.Tuple, args []*operand) (result []types.Type, err error) {
30+
conf := &types.Config{
31+
Error: func(e error) {
32+
err = e
33+
if terr, ok := e.(types.Error); ok {
34+
err = fmt.Errorf("%s", terr.Msg)
35+
}
36+
},
37+
}
38+
checker := types.NewChecker(conf, pkg.Fset, pkg.Types, nil)
39+
result = checker_infer(checker, posn, tparams, targs, params, args)
40+
return
41+
}

typesinfer_go123.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)