Skip to content

Commit

Permalink
go/types, types2: reduce differences between go/types and types2 buil…
Browse files Browse the repository at this point in the history
…tins.go

Change-Id: I2946e061c70d31df3ba2aa3582700c3785b647e0
Reviewed-on: https://go-review.googlesource.com/c/go/+/495615
Reviewed-by: Robert Findley <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
Auto-Submit: Robert Griesemer <[email protected]>
Run-TryBot: Robert Griesemer <[email protected]>
  • Loading branch information
griesemer authored and gopherbot committed May 18, 2023
1 parent 8e3dfe7 commit 44b8f39
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 46 deletions.
28 changes: 16 additions & 12 deletions src/cmd/compile/internal/types2/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ import (
// but x.expr is not set. If the call is invalid, the result is
// false, and *x is undefined.
func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (_ bool) {
argList := call.ArgList

// append is the only built-in that permits the use of ... for the last argument
bin := predeclaredFuncs[id]
if call.HasDots && id != _Append {
//check.errorf(call.Ellipsis, invalidOp + "invalid use of ... with built-in %s", bin.name)
check.errorf(call, InvalidDotDotDot, invalidOp+"invalid use of ... with built-in %s", bin.name)
check.use(call.ArgList...)
check.errorf(call,
InvalidDotDotDot,
invalidOp+"invalid use of ... with built-in %s", bin.name)
check.use(argList...)
return
}

Expand All @@ -47,7 +51,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
switch id {
default:
// check all arguments
args = check.exprList(call.ArgList)
args = check.exprList(argList)
nargs = len(args)
for _, a := range args {
if a.mode == invalid {
Expand All @@ -60,7 +64,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
}
case _Make, _New, _Offsetof, _Trace:
// arguments require special handling
nargs = len(call.ArgList)
nargs = len(argList)
}

// check argument count
Expand Down Expand Up @@ -486,7 +490,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
// make(T, n)
// make(T, n, m)
// (no argument evaluated yet)
arg0 := call.ArgList[0]
arg0 := argList[0]
T := check.varType(arg0)
if T == Typ[Invalid] {
return
Expand All @@ -512,15 +516,15 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (

types := []Type{T}
var sizes []int64 // constant integer arguments, if any
for _, arg := range call.ArgList[1:] {
for _, arg := range argList[1:] {
typ, size := check.index(arg, -1) // ok to continue with typ == Typ[Invalid]
types = append(types, typ)
if size >= 0 {
sizes = append(sizes, size)
}
}
if len(sizes) == 2 && sizes[0] > sizes[1] {
check.error(call.ArgList[1], SwappedMakeArgs, invalidArg+"length and capacity swapped")
check.error(argList[1], SwappedMakeArgs, invalidArg+"length and capacity swapped")
// safe to continue
}
x.mode = value
Expand All @@ -532,7 +536,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
case _New:
// new(T)
// (no argument evaluated yet)
T := check.varType(call.ArgList[0])
T := check.varType(argList[0])
if T == Typ[Invalid] {
return
}
Expand Down Expand Up @@ -641,7 +645,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
case _Offsetof:
// unsafe.Offsetof(x T) uintptr, where x must be a selector
// (no argument evaluated yet)
arg0 := call.ArgList[0]
arg0 := argList[0]
selx, _ := unparen(arg0).(*syntax.SelectorExpr)
if selx == nil {
check.errorf(arg0, BadOffsetofSyntax, invalidArg+"%s is not a selector expression", arg0)
Expand Down Expand Up @@ -842,7 +846,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
}
var t operand
x1 := x
for _, arg := range call.ArgList {
for _, arg := range argList {
check.rawExpr(nil, x1, arg, nil, false) // permit trace for types, e.g.: new(trace(T))
check.dump("%v: %s", atPos(x1), x1)
x1 = &t // use incoming x only for first argument
Expand Down Expand Up @@ -921,8 +925,8 @@ func (check *Checker) applyTypeFunc(f func(Type) Type, x *operand, id builtinId)

// We can type-check this fine but we're introducing a synthetic
// type parameter for the result. It's not clear what the API
// implications are here. Report an error for 1.18 but continue
// type-checking.
// implications are here. Report an error for 1.18 (see go.dev/issue/50912),
// but continue type-checking.
var code Code
switch id {
case _Real:
Expand Down
4 changes: 4 additions & 0 deletions src/go/types/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ type Info struct {
InitOrder []*Initializer
}

func (info *Info) recordTypes() bool {
return info.Types != nil
}

// TypeOf returns the type of expression e, or nil if not found.
// Precondition: the Types, Uses and Defs maps are populated.
func (info *Info) TypeOf(e ast.Expr) Type {
Expand Down
Loading

0 comments on commit 44b8f39

Please sign in to comment.