go/types, types2: remove setDefType and most def plumbing

CL 722161 replaced the setDefType mechanism with boundaries on composite
literals, removing the need to pass the def argument in all but 1 case.

The exception is interface types, which use def to populate the receiver
type for better error messages.

Change-Id: Ic78c91238588015153f0d22790be5872a01c5f63
Reviewed-on: https://go-review.googlesource.com/c/go/+/723920
Auto-Submit: Mark Freeman <markfreeman@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Mark Freeman 2025-11-24 14:34:39 -05:00 committed by Gopher Robot
parent 3531ac23d4
commit ff2fd6327e
14 changed files with 48 additions and 110 deletions

View file

@ -669,7 +669,7 @@ var cgoPrefixes = [...]string{
"_Cmacro_", // function to evaluate the expanded expression
}
func (check *Checker) selector(x *operand, e *syntax.SelectorExpr, def *TypeName, wantType bool) {
func (check *Checker) selector(x *operand, e *syntax.SelectorExpr, wantType bool) {
// these must be declared before the "goto Error" statements
var (
obj Object
@ -715,7 +715,7 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr, def *TypeName
}
goto Error
}
check.objDecl(exp, nil)
check.objDecl(exp)
} else {
exp = pkg.scope.Lookup(sel)
if exp == nil {
@ -777,12 +777,6 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr, def *TypeName
check.exprOrType(x, e.X, false)
switch x.mode {
case typexpr:
// don't crash for "type T T.x" (was go.dev/issue/51509)
if def != nil && def.typ == x.typ {
check.cycleError([]Object{def}, 0)
goto Error
}
case builtin:
check.errorf(e.Pos(), UncalledBuiltin, "invalid use of %s in selector expression", x)
goto Error
@ -844,7 +838,7 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr, def *TypeName
// methods may not have a fully set up signature yet
if m, _ := obj.(*Func); m != nil {
check.objDecl(m, nil)
check.objDecl(m)
}
if x.mode == typexpr {

View file

@ -45,8 +45,7 @@ func pathString(path []Object) string {
}
// objDecl type-checks the declaration of obj in its respective (file) environment.
// For the meaning of def, see Checker.definedType, in typexpr.go.
func (check *Checker) objDecl(obj Object, def *TypeName) {
func (check *Checker) objDecl(obj Object) {
if tracePos {
check.pushPos(obj.Pos())
defer func() {
@ -156,7 +155,7 @@ func (check *Checker) objDecl(obj Object, def *TypeName) {
check.varDecl(obj, d.lhs, d.vtyp, d.init)
case *TypeName:
// invalid recursive types are detected via path
check.typeDecl(obj, d.tdecl, def)
check.typeDecl(obj, d.tdecl)
check.collectMethods(obj) // methods can only be added to top-level types
case *Func:
// functions may be recursive - no need to track dependencies
@ -440,7 +439,7 @@ func (check *Checker) isImportedConstraint(typ Type) bool {
return u != nil && !u.IsMethodSet()
}
func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeName) {
func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl) {
assert(obj.typ == nil)
// Only report a version error if we have not reported one already.
@ -474,7 +473,6 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
if check.conf.EnableAlias {
alias := check.newAlias(obj, nil)
setDefType(def, alias)
// If we could not type the RHS, set it to invalid. This should
// only ever happen if we panic before setting.
@ -521,7 +519,6 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
}
named := check.newNamed(obj, nil, nil)
setDefType(def, named)
// The RHS of a named N can be nil if, for example, N is defined as a cycle of aliases with
// gotypesalias=0. Consider:
@ -878,7 +875,7 @@ func (check *Checker) declStmt(list []syntax.Decl) {
scopePos := s.Name.Pos()
check.declare(check.scope, s.Name, obj, scopePos)
check.push(obj) // mark as grey
check.typeDecl(obj, s, nil)
check.typeDecl(obj, s)
check.pop()
default:

View file

@ -1067,7 +1067,7 @@ func (check *Checker) exprInternal(T *target, x *operand, e syntax.Expr, hint Ty
goto Error // error was reported before
case *syntax.Name:
check.ident(x, e, nil, false)
check.ident(x, e, false)
case *syntax.DotsType:
// dots are handled explicitly where they are valid
@ -1102,7 +1102,7 @@ func (check *Checker) exprInternal(T *target, x *operand, e syntax.Expr, hint Ty
return kind
case *syntax.SelectorExpr:
check.selector(x, e, nil, false)
check.selector(x, e, false)
case *syntax.IndexExpr:
if check.indexExpr(x, e) {

View file

@ -447,7 +447,7 @@ func (check *Checker) missingMethod(V, T Type, static bool, equivalent func(x, y
// methods may not have a fully set up signature yet
if check != nil {
check.objDecl(f, nil)
check.objDecl(f)
}
if !equivalent(f.typ, m.typ) {
@ -466,7 +466,7 @@ func (check *Checker) missingMethod(V, T Type, static bool, equivalent func(x, y
// This method may be formatted in funcString below, so must have a fully
// set up signature.
if check != nil {
check.objDecl(f, nil)
check.objDecl(f)
}
}
switch state {

View file

@ -456,7 +456,7 @@ func (t *Named) expandMethod(i int) *Func {
check := t.check
// Ensure that the original method is type-checked.
if check != nil {
check.objDecl(origm, nil)
check.objDecl(origm)
}
origSig := origm.typ.(*Signature)

View file

@ -664,7 +664,7 @@ func (check *Checker) packageObjects() {
//
// Investigate and reenable this branch.
for _, obj := range check.objList {
check.objDecl(obj, nil)
check.objDecl(obj)
}
} else {
// Without Alias nodes, we process non-alias type declarations first, followed by
@ -680,7 +680,7 @@ func (check *Checker) packageObjects() {
if check.objMap[tname].tdecl.Alias {
aliasList = append(aliasList, tname)
} else {
check.objDecl(obj, nil)
check.objDecl(obj)
}
} else {
othersList = append(othersList, obj)
@ -688,11 +688,11 @@ func (check *Checker) packageObjects() {
}
// phase 2: alias type declarations
for _, obj := range aliasList {
check.objDecl(obj, nil)
check.objDecl(obj)
}
// phase 3: all other declarations
for _, obj := range othersList {
check.objDecl(obj, nil)
check.objDecl(obj)
}
}

View file

@ -16,9 +16,8 @@ import (
// ident type-checks identifier e and initializes x with the value or type of e.
// If an error occurred, x.mode is set to invalid.
// For the meaning of def, see Checker.declaredType, below.
// If wantType is set, the identifier e is expected to denote a type.
func (check *Checker) ident(x *operand, e *syntax.Name, def *TypeName, wantType bool) {
func (check *Checker) ident(x *operand, e *syntax.Name, wantType bool) {
x.mode = invalid
x.expr = e
@ -73,7 +72,7 @@ func (check *Checker) ident(x *operand, e *syntax.Name, def *TypeName, wantType
// packages, to avoid races: see issue #69912.
typ := obj.Type()
if typ == nil || (gotType && wantType && obj.Pkg() == check.pkg) {
check.objDecl(obj, def)
check.objDecl(obj)
typ = obj.Type() // type must have been assigned by Checker.objDecl
}
assert(typ != nil)
@ -257,13 +256,11 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.Name:
var x operand
check.ident(&x, e, def, true)
check.ident(&x, e, true)
switch x.mode {
case typexpr:
typ := x.typ
setDefType(def, typ)
return typ
return x.typ
case invalid:
// ignore - error reported before
case novalue:
@ -274,13 +271,11 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.SelectorExpr:
var x operand
check.selector(&x, e, def, true)
check.selector(&x, e, true)
switch x.mode {
case typexpr:
typ := x.typ
setDefType(def, typ)
return typ
return x.typ
case invalid:
// ignore - error reported before
case novalue:
@ -291,7 +286,7 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.IndexExpr:
check.verifyVersionf(e, go1_18, "type instantiation")
return check.instantiatedType(e.X, syntax.UnpackListExpr(e.Index), def)
return check.instantiatedType(e.X, syntax.UnpackListExpr(e.Index))
case *syntax.ParenExpr:
// Generic types must be instantiated before they can be used in any form.
@ -300,7 +295,6 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.ArrayType:
typ := new(Array)
setDefType(def, typ)
if e.Len != nil {
typ.len = check.arrayLength(e.Len)
} else {
@ -316,7 +310,6 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.SliceType:
typ := new(Slice)
setDefType(def, typ)
typ.elem = check.varType(e.Elem)
return typ
@ -326,7 +319,6 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.StructType:
typ := new(Struct)
setDefType(def, typ)
check.structType(typ, e)
return typ
@ -334,7 +326,6 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
if e.Op == syntax.Mul && e.Y == nil {
typ := new(Pointer)
typ.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration
setDefType(def, typ)
typ.base = check.varType(e.X)
// If typ.base is invalid, it's unlikely that *base is particularly
// useful - even a valid dereferenciation will lead to an invalid
@ -351,20 +342,16 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.FuncType:
typ := new(Signature)
setDefType(def, typ)
check.funcType(typ, nil, nil, e)
return typ
case *syntax.InterfaceType:
typ := check.newInterface()
setDefType(def, typ)
check.interfaceType(typ, e, def)
return typ
case *syntax.MapType:
typ := new(Map)
setDefType(def, typ)
typ.key = check.varType(e.Key)
typ.elem = check.varType(e.Value)
@ -388,7 +375,6 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.ChanType:
typ := new(Chan)
setDefType(def, typ)
dir := SendRecv
switch e.Dir {
@ -413,14 +399,10 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
}
typ := Typ[Invalid]
setDefType(def, typ)
return typ
}
// TODO(markfreeman): Remove this function.
func setDefType(def *TypeName, typ Type) {}
func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *TypeName) (res Type) {
func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr) (res Type) {
if check.conf.Trace {
check.trace(x.Pos(), "-- instantiating type %s with %s", x, xlist)
check.indent++
@ -431,10 +413,6 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
}()
}
defer func() {
setDefType(def, res)
}()
var cause string
typ := check.genericType(x, &cause)
if cause != "" {

View file

@ -671,7 +671,7 @@ var cgoPrefixes = [...]string{
"_Cmacro_", // function to evaluate the expanded expression
}
func (check *Checker) selector(x *operand, e *ast.SelectorExpr, def *TypeName, wantType bool) {
func (check *Checker) selector(x *operand, e *ast.SelectorExpr, wantType bool) {
// these must be declared before the "goto Error" statements
var (
obj Object
@ -717,7 +717,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr, def *TypeName, w
}
goto Error
}
check.objDecl(exp, nil)
check.objDecl(exp)
} else {
exp = pkg.scope.Lookup(sel)
if exp == nil {
@ -779,12 +779,6 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr, def *TypeName, w
check.exprOrType(x, e.X, false)
switch x.mode {
case typexpr:
// don't crash for "type T T.x" (was go.dev/issue/51509)
if def != nil && def.typ == x.typ {
check.cycleError([]Object{def}, 0)
goto Error
}
case builtin:
// types2 uses the position of '.' for the error
check.errorf(e.Sel, UncalledBuiltin, "invalid use of %s in selector expression", x)
@ -847,7 +841,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr, def *TypeName, w
// methods may not have a fully set up signature yet
if m, _ := obj.(*Func); m != nil {
check.objDecl(m, nil)
check.objDecl(m)
}
if x.mode == typexpr {

View file

@ -46,8 +46,7 @@ func pathString(path []Object) string {
}
// objDecl type-checks the declaration of obj in its respective (file) environment.
// For the meaning of def, see Checker.definedType, in typexpr.go.
func (check *Checker) objDecl(obj Object, def *TypeName) {
func (check *Checker) objDecl(obj Object) {
if tracePos {
check.pushPos(atPos(obj.Pos()))
defer func() {
@ -157,7 +156,7 @@ func (check *Checker) objDecl(obj Object, def *TypeName) {
check.varDecl(obj, d.lhs, d.vtyp, d.init)
case *TypeName:
// invalid recursive types are detected via path
check.typeDecl(obj, d.tdecl, def)
check.typeDecl(obj, d.tdecl)
check.collectMethods(obj) // methods can only be added to top-level types
case *Func:
// functions may be recursive - no need to track dependencies
@ -515,7 +514,7 @@ func (check *Checker) isImportedConstraint(typ Type) bool {
return u != nil && !u.IsMethodSet()
}
func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName) {
func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec) {
assert(obj.typ == nil)
// Only report a version error if we have not reported one already.
@ -549,7 +548,6 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName
if check.conf._EnableAlias {
alias := check.newAlias(obj, nil)
setDefType(def, alias)
// If we could not type the RHS, set it to invalid. This should
// only ever happen if we panic before setting.
@ -603,7 +601,6 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName
}
named := check.newNamed(obj, nil, nil)
setDefType(def, named)
// The RHS of a named N can be nil if, for example, N is defined as a cycle of aliases with
// gotypesalias=0. Consider:
@ -937,7 +934,7 @@ func (check *Checker) declStmt(d ast.Decl) {
scopePos := d.spec.Name.Pos()
check.declare(check.scope, d.spec.Name, obj, scopePos)
check.push(obj) // mark as grey
check.typeDecl(obj, d.spec, nil)
check.typeDecl(obj, d.spec)
check.pop()
default:
check.errorf(d.node(), InvalidSyntaxTree, "unknown ast.Decl node %T", d.node())

View file

@ -1056,7 +1056,7 @@ func (check *Checker) exprInternal(T *target, x *operand, e ast.Expr, hint Type)
goto Error // error was reported before
case *ast.Ident:
check.ident(x, e, nil, false)
check.ident(x, e, false)
case *ast.Ellipsis:
// ellipses are handled explicitly where they are valid
@ -1088,7 +1088,7 @@ func (check *Checker) exprInternal(T *target, x *operand, e ast.Expr, hint Type)
return kind
case *ast.SelectorExpr:
check.selector(x, e, nil, false)
check.selector(x, e, false)
case *ast.IndexExpr, *ast.IndexListExpr:
ix := unpackIndexedExpr(e)

View file

@ -450,7 +450,7 @@ func (check *Checker) missingMethod(V, T Type, static bool, equivalent func(x, y
// methods may not have a fully set up signature yet
if check != nil {
check.objDecl(f, nil)
check.objDecl(f)
}
if !equivalent(f.typ, m.typ) {
@ -469,7 +469,7 @@ func (check *Checker) missingMethod(V, T Type, static bool, equivalent func(x, y
// This method may be formatted in funcString below, so must have a fully
// set up signature.
if check != nil {
check.objDecl(f, nil)
check.objDecl(f)
}
}
switch state {

View file

@ -459,7 +459,7 @@ func (t *Named) expandMethod(i int) *Func {
check := t.check
// Ensure that the original method is type-checked.
if check != nil {
check.objDecl(origm, nil)
check.objDecl(origm)
}
origSig := origm.typ.(*Signature)

View file

@ -659,7 +659,7 @@ func (check *Checker) packageObjects() {
//
// Investigate and reenable this branch.
for _, obj := range check.objList {
check.objDecl(obj, nil)
check.objDecl(obj)
}
} else {
// Without Alias nodes, we process non-alias type declarations first, followed by
@ -675,7 +675,7 @@ func (check *Checker) packageObjects() {
if check.objMap[tname].tdecl.Assign.IsValid() {
aliasList = append(aliasList, tname)
} else {
check.objDecl(obj, nil)
check.objDecl(obj)
}
} else {
othersList = append(othersList, obj)
@ -683,11 +683,11 @@ func (check *Checker) packageObjects() {
}
// phase 2: alias type declarations
for _, obj := range aliasList {
check.objDecl(obj, nil)
check.objDecl(obj)
}
// phase 3: all other declarations
for _, obj := range othersList {
check.objDecl(obj, nil)
check.objDecl(obj)
}
}

View file

@ -16,9 +16,8 @@ import (
// ident type-checks identifier e and initializes x with the value or type of e.
// If an error occurred, x.mode is set to invalid.
// For the meaning of def, see Checker.declaredType, below.
// If wantType is set, the identifier e is expected to denote a type.
func (check *Checker) ident(x *operand, e *ast.Ident, def *TypeName, wantType bool) {
func (check *Checker) ident(x *operand, e *ast.Ident, wantType bool) {
x.mode = invalid
x.expr = e
@ -72,7 +71,7 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *TypeName, wantType bo
// packages, to avoid races: see issue #69912.
typ := obj.Type()
if typ == nil || (gotType && wantType && obj.Pkg() == check.pkg) {
check.objDecl(obj, def)
check.objDecl(obj)
typ = obj.Type() // type must have been assigned by Checker.objDecl
}
assert(typ != nil)
@ -255,13 +254,11 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.Ident:
var x operand
check.ident(&x, e, def, true)
check.ident(&x, e, true)
switch x.mode {
case typexpr:
typ := x.typ
setDefType(def, typ)
return typ
return x.typ
case invalid:
// ignore - error reported before
case novalue:
@ -272,13 +269,11 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.SelectorExpr:
var x operand
check.selector(&x, e, def, true)
check.selector(&x, e, true)
switch x.mode {
case typexpr:
typ := x.typ
setDefType(def, typ)
return typ
return x.typ
case invalid:
// ignore - error reported before
case novalue:
@ -290,7 +285,7 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.IndexExpr, *ast.IndexListExpr:
ix := unpackIndexedExpr(e)
check.verifyVersionf(inNode(e, ix.lbrack), go1_18, "type instantiation")
return check.instantiatedType(ix, def)
return check.instantiatedType(ix)
case *ast.ParenExpr:
// Generic types must be instantiated before they can be used in any form.
@ -300,13 +295,11 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.ArrayType:
if e.Len == nil {
typ := new(Slice)
setDefType(def, typ)
typ.elem = check.varType(e.Elt)
return typ
}
typ := new(Array)
setDefType(def, typ)
// Provide a more specific error when encountering a [...] array
// rather than leaving it to the handling of the ... expression.
if _, ok := e.Len.(*ast.Ellipsis); ok {
@ -327,14 +320,12 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.StructType:
typ := new(Struct)
setDefType(def, typ)
check.structType(typ, e)
return typ
case *ast.StarExpr:
typ := new(Pointer)
typ.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration
setDefType(def, typ)
typ.base = check.varType(e.X)
// If typ.base is invalid, it's unlikely that *base is particularly
// useful - even a valid dereferenciation will lead to an invalid
@ -347,20 +338,16 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.FuncType:
typ := new(Signature)
setDefType(def, typ)
check.funcType(typ, nil, e)
return typ
case *ast.InterfaceType:
typ := check.newInterface()
setDefType(def, typ)
check.interfaceType(typ, e, def)
return typ
case *ast.MapType:
typ := new(Map)
setDefType(def, typ)
typ.key = check.varType(e.Key)
typ.elem = check.varType(e.Value)
@ -384,7 +371,6 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.ChanType:
typ := new(Chan)
setDefType(def, typ)
dir := SendRecv
switch e.Dir {
@ -409,14 +395,10 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
}
typ := Typ[Invalid]
setDefType(def, typ)
return typ
}
// TODO(markfreeman): Remove this function.
func setDefType(def *TypeName, typ Type) {}
func (check *Checker) instantiatedType(ix *indexedExpr, def *TypeName) (res Type) {
func (check *Checker) instantiatedType(ix *indexedExpr) (res Type) {
if check.conf._Trace {
check.trace(ix.Pos(), "-- instantiating type %s with %s", ix.x, ix.indices)
check.indent++
@ -427,10 +409,6 @@ func (check *Checker) instantiatedType(ix *indexedExpr, def *TypeName) (res Type
}()
}
defer func() {
setDefType(def, res)
}()
var cause string
typ := check.genericType(ix.x, &cause)
if cause != "" {