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 "_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 // these must be declared before the "goto Error" statements
var ( var (
obj Object obj Object
@ -715,7 +715,7 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr, def *TypeName
} }
goto Error goto Error
} }
check.objDecl(exp, nil) check.objDecl(exp)
} else { } else {
exp = pkg.scope.Lookup(sel) exp = pkg.scope.Lookup(sel)
if exp == nil { if exp == nil {
@ -777,12 +777,6 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr, def *TypeName
check.exprOrType(x, e.X, false) check.exprOrType(x, e.X, false)
switch x.mode { 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: case builtin:
check.errorf(e.Pos(), UncalledBuiltin, "invalid use of %s in selector expression", x) check.errorf(e.Pos(), UncalledBuiltin, "invalid use of %s in selector expression", x)
goto Error 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 // methods may not have a fully set up signature yet
if m, _ := obj.(*Func); m != nil { if m, _ := obj.(*Func); m != nil {
check.objDecl(m, nil) check.objDecl(m)
} }
if x.mode == typexpr { 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. // 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) {
func (check *Checker) objDecl(obj Object, def *TypeName) {
if tracePos { if tracePos {
check.pushPos(obj.Pos()) check.pushPos(obj.Pos())
defer func() { defer func() {
@ -156,7 +155,7 @@ func (check *Checker) objDecl(obj Object, def *TypeName) {
check.varDecl(obj, d.lhs, d.vtyp, d.init) check.varDecl(obj, d.lhs, d.vtyp, d.init)
case *TypeName: case *TypeName:
// invalid recursive types are detected via path // 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 check.collectMethods(obj) // methods can only be added to top-level types
case *Func: case *Func:
// functions may be recursive - no need to track dependencies // 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() 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) assert(obj.typ == nil)
// Only report a version error if we have not reported one already. // 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 { if check.conf.EnableAlias {
alias := check.newAlias(obj, nil) alias := check.newAlias(obj, nil)
setDefType(def, alias)
// If we could not type the RHS, set it to invalid. This should // If we could not type the RHS, set it to invalid. This should
// only ever happen if we panic before setting. // 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) 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 // The RHS of a named N can be nil if, for example, N is defined as a cycle of aliases with
// gotypesalias=0. Consider: // gotypesalias=0. Consider:
@ -878,7 +875,7 @@ func (check *Checker) declStmt(list []syntax.Decl) {
scopePos := s.Name.Pos() scopePos := s.Name.Pos()
check.declare(check.scope, s.Name, obj, scopePos) check.declare(check.scope, s.Name, obj, scopePos)
check.push(obj) // mark as grey check.push(obj) // mark as grey
check.typeDecl(obj, s, nil) check.typeDecl(obj, s)
check.pop() check.pop()
default: 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 goto Error // error was reported before
case *syntax.Name: case *syntax.Name:
check.ident(x, e, nil, false) check.ident(x, e, false)
case *syntax.DotsType: case *syntax.DotsType:
// dots are handled explicitly where they are valid // 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 return kind
case *syntax.SelectorExpr: case *syntax.SelectorExpr:
check.selector(x, e, nil, false) check.selector(x, e, false)
case *syntax.IndexExpr: case *syntax.IndexExpr:
if check.indexExpr(x, e) { 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 // methods may not have a fully set up signature yet
if check != nil { if check != nil {
check.objDecl(f, nil) check.objDecl(f)
} }
if !equivalent(f.typ, m.typ) { 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 // This method may be formatted in funcString below, so must have a fully
// set up signature. // set up signature.
if check != nil { if check != nil {
check.objDecl(f, nil) check.objDecl(f)
} }
} }
switch state { switch state {

View file

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

View file

@ -664,7 +664,7 @@ func (check *Checker) packageObjects() {
// //
// Investigate and reenable this branch. // Investigate and reenable this branch.
for _, obj := range check.objList { for _, obj := range check.objList {
check.objDecl(obj, nil) check.objDecl(obj)
} }
} else { } else {
// Without Alias nodes, we process non-alias type declarations first, followed by // 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 { if check.objMap[tname].tdecl.Alias {
aliasList = append(aliasList, tname) aliasList = append(aliasList, tname)
} else { } else {
check.objDecl(obj, nil) check.objDecl(obj)
} }
} else { } else {
othersList = append(othersList, obj) othersList = append(othersList, obj)
@ -688,11 +688,11 @@ func (check *Checker) packageObjects() {
} }
// phase 2: alias type declarations // phase 2: alias type declarations
for _, obj := range aliasList { for _, obj := range aliasList {
check.objDecl(obj, nil) check.objDecl(obj)
} }
// phase 3: all other declarations // phase 3: all other declarations
for _, obj := range othersList { 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. // 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. // 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. // 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.mode = invalid
x.expr = e 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. // packages, to avoid races: see issue #69912.
typ := obj.Type() typ := obj.Type()
if typ == nil || (gotType && wantType && obj.Pkg() == check.pkg) { 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 typ = obj.Type() // type must have been assigned by Checker.objDecl
} }
assert(typ != nil) assert(typ != nil)
@ -257,13 +256,11 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.Name: case *syntax.Name:
var x operand var x operand
check.ident(&x, e, def, true) check.ident(&x, e, true)
switch x.mode { switch x.mode {
case typexpr: case typexpr:
typ := x.typ return x.typ
setDefType(def, typ)
return typ
case invalid: case invalid:
// ignore - error reported before // ignore - error reported before
case novalue: case novalue:
@ -274,13 +271,11 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.SelectorExpr: case *syntax.SelectorExpr:
var x operand var x operand
check.selector(&x, e, def, true) check.selector(&x, e, true)
switch x.mode { switch x.mode {
case typexpr: case typexpr:
typ := x.typ return x.typ
setDefType(def, typ)
return typ
case invalid: case invalid:
// ignore - error reported before // ignore - error reported before
case novalue: case novalue:
@ -291,7 +286,7 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.IndexExpr: case *syntax.IndexExpr:
check.verifyVersionf(e, go1_18, "type instantiation") 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: case *syntax.ParenExpr:
// Generic types must be instantiated before they can be used in any form. // 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: case *syntax.ArrayType:
typ := new(Array) typ := new(Array)
setDefType(def, typ)
if e.Len != nil { if e.Len != nil {
typ.len = check.arrayLength(e.Len) typ.len = check.arrayLength(e.Len)
} else { } else {
@ -316,7 +310,6 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.SliceType: case *syntax.SliceType:
typ := new(Slice) typ := new(Slice)
setDefType(def, typ)
typ.elem = check.varType(e.Elem) typ.elem = check.varType(e.Elem)
return typ return typ
@ -326,7 +319,6 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.StructType: case *syntax.StructType:
typ := new(Struct) typ := new(Struct)
setDefType(def, typ)
check.structType(typ, e) check.structType(typ, e)
return typ 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 { if e.Op == syntax.Mul && e.Y == nil {
typ := new(Pointer) typ := new(Pointer)
typ.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration typ.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration
setDefType(def, typ)
typ.base = check.varType(e.X) typ.base = check.varType(e.X)
// If typ.base is invalid, it's unlikely that *base is particularly // If typ.base is invalid, it's unlikely that *base is particularly
// useful - even a valid dereferenciation will lead to an invalid // 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: case *syntax.FuncType:
typ := new(Signature) typ := new(Signature)
setDefType(def, typ)
check.funcType(typ, nil, nil, e) check.funcType(typ, nil, nil, e)
return typ return typ
case *syntax.InterfaceType: case *syntax.InterfaceType:
typ := check.newInterface() typ := check.newInterface()
setDefType(def, typ)
check.interfaceType(typ, e, def) check.interfaceType(typ, e, def)
return typ return typ
case *syntax.MapType: case *syntax.MapType:
typ := new(Map) typ := new(Map)
setDefType(def, typ)
typ.key = check.varType(e.Key) typ.key = check.varType(e.Key)
typ.elem = check.varType(e.Value) typ.elem = check.varType(e.Value)
@ -388,7 +375,6 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
case *syntax.ChanType: case *syntax.ChanType:
typ := new(Chan) typ := new(Chan)
setDefType(def, typ)
dir := SendRecv dir := SendRecv
switch e.Dir { switch e.Dir {
@ -413,14 +399,10 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *TypeName) (T Type) {
} }
typ := Typ[Invalid] typ := Typ[Invalid]
setDefType(def, typ)
return typ return typ
} }
// TODO(markfreeman): Remove this function. func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr) (res Type) {
func setDefType(def *TypeName, typ Type) {}
func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *TypeName) (res Type) {
if check.conf.Trace { if check.conf.Trace {
check.trace(x.Pos(), "-- instantiating type %s with %s", x, xlist) check.trace(x.Pos(), "-- instantiating type %s with %s", x, xlist)
check.indent++ 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 var cause string
typ := check.genericType(x, &cause) typ := check.genericType(x, &cause)
if cause != "" { if cause != "" {

View file

@ -671,7 +671,7 @@ var cgoPrefixes = [...]string{
"_Cmacro_", // function to evaluate the expanded expression "_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 // these must be declared before the "goto Error" statements
var ( var (
obj Object obj Object
@ -717,7 +717,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr, def *TypeName, w
} }
goto Error goto Error
} }
check.objDecl(exp, nil) check.objDecl(exp)
} else { } else {
exp = pkg.scope.Lookup(sel) exp = pkg.scope.Lookup(sel)
if exp == nil { 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) check.exprOrType(x, e.X, false)
switch x.mode { 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: case builtin:
// types2 uses the position of '.' for the error // types2 uses the position of '.' for the error
check.errorf(e.Sel, UncalledBuiltin, "invalid use of %s in selector expression", x) 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 // methods may not have a fully set up signature yet
if m, _ := obj.(*Func); m != nil { if m, _ := obj.(*Func); m != nil {
check.objDecl(m, nil) check.objDecl(m)
} }
if x.mode == typexpr { 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. // 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) {
func (check *Checker) objDecl(obj Object, def *TypeName) {
if tracePos { if tracePos {
check.pushPos(atPos(obj.Pos())) check.pushPos(atPos(obj.Pos()))
defer func() { defer func() {
@ -157,7 +156,7 @@ func (check *Checker) objDecl(obj Object, def *TypeName) {
check.varDecl(obj, d.lhs, d.vtyp, d.init) check.varDecl(obj, d.lhs, d.vtyp, d.init)
case *TypeName: case *TypeName:
// invalid recursive types are detected via path // 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 check.collectMethods(obj) // methods can only be added to top-level types
case *Func: case *Func:
// functions may be recursive - no need to track dependencies // 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() 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) assert(obj.typ == nil)
// Only report a version error if we have not reported one already. // 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 { if check.conf._EnableAlias {
alias := check.newAlias(obj, nil) alias := check.newAlias(obj, nil)
setDefType(def, alias)
// If we could not type the RHS, set it to invalid. This should // If we could not type the RHS, set it to invalid. This should
// only ever happen if we panic before setting. // 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) 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 // The RHS of a named N can be nil if, for example, N is defined as a cycle of aliases with
// gotypesalias=0. Consider: // gotypesalias=0. Consider:
@ -937,7 +934,7 @@ func (check *Checker) declStmt(d ast.Decl) {
scopePos := d.spec.Name.Pos() scopePos := d.spec.Name.Pos()
check.declare(check.scope, d.spec.Name, obj, scopePos) check.declare(check.scope, d.spec.Name, obj, scopePos)
check.push(obj) // mark as grey check.push(obj) // mark as grey
check.typeDecl(obj, d.spec, nil) check.typeDecl(obj, d.spec)
check.pop() check.pop()
default: default:
check.errorf(d.node(), InvalidSyntaxTree, "unknown ast.Decl node %T", d.node()) 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 goto Error // error was reported before
case *ast.Ident: case *ast.Ident:
check.ident(x, e, nil, false) check.ident(x, e, false)
case *ast.Ellipsis: case *ast.Ellipsis:
// ellipses are handled explicitly where they are valid // 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 return kind
case *ast.SelectorExpr: case *ast.SelectorExpr:
check.selector(x, e, nil, false) check.selector(x, e, false)
case *ast.IndexExpr, *ast.IndexListExpr: case *ast.IndexExpr, *ast.IndexListExpr:
ix := unpackIndexedExpr(e) 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 // methods may not have a fully set up signature yet
if check != nil { if check != nil {
check.objDecl(f, nil) check.objDecl(f)
} }
if !equivalent(f.typ, m.typ) { 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 // This method may be formatted in funcString below, so must have a fully
// set up signature. // set up signature.
if check != nil { if check != nil {
check.objDecl(f, nil) check.objDecl(f)
} }
} }
switch state { switch state {

View file

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

View file

@ -659,7 +659,7 @@ func (check *Checker) packageObjects() {
// //
// Investigate and reenable this branch. // Investigate and reenable this branch.
for _, obj := range check.objList { for _, obj := range check.objList {
check.objDecl(obj, nil) check.objDecl(obj)
} }
} else { } else {
// Without Alias nodes, we process non-alias type declarations first, followed by // 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() { if check.objMap[tname].tdecl.Assign.IsValid() {
aliasList = append(aliasList, tname) aliasList = append(aliasList, tname)
} else { } else {
check.objDecl(obj, nil) check.objDecl(obj)
} }
} else { } else {
othersList = append(othersList, obj) othersList = append(othersList, obj)
@ -683,11 +683,11 @@ func (check *Checker) packageObjects() {
} }
// phase 2: alias type declarations // phase 2: alias type declarations
for _, obj := range aliasList { for _, obj := range aliasList {
check.objDecl(obj, nil) check.objDecl(obj)
} }
// phase 3: all other declarations // phase 3: all other declarations
for _, obj := range othersList { 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. // 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. // 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. // 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.mode = invalid
x.expr = e 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. // packages, to avoid races: see issue #69912.
typ := obj.Type() typ := obj.Type()
if typ == nil || (gotType && wantType && obj.Pkg() == check.pkg) { 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 typ = obj.Type() // type must have been assigned by Checker.objDecl
} }
assert(typ != nil) assert(typ != nil)
@ -255,13 +254,11 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.Ident: case *ast.Ident:
var x operand var x operand
check.ident(&x, e, def, true) check.ident(&x, e, true)
switch x.mode { switch x.mode {
case typexpr: case typexpr:
typ := x.typ return x.typ
setDefType(def, typ)
return typ
case invalid: case invalid:
// ignore - error reported before // ignore - error reported before
case novalue: case novalue:
@ -272,13 +269,11 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.SelectorExpr: case *ast.SelectorExpr:
var x operand var x operand
check.selector(&x, e, def, true) check.selector(&x, e, true)
switch x.mode { switch x.mode {
case typexpr: case typexpr:
typ := x.typ return x.typ
setDefType(def, typ)
return typ
case invalid: case invalid:
// ignore - error reported before // ignore - error reported before
case novalue: case novalue:
@ -290,7 +285,7 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.IndexExpr, *ast.IndexListExpr: case *ast.IndexExpr, *ast.IndexListExpr:
ix := unpackIndexedExpr(e) ix := unpackIndexedExpr(e)
check.verifyVersionf(inNode(e, ix.lbrack), go1_18, "type instantiation") check.verifyVersionf(inNode(e, ix.lbrack), go1_18, "type instantiation")
return check.instantiatedType(ix, def) return check.instantiatedType(ix)
case *ast.ParenExpr: case *ast.ParenExpr:
// Generic types must be instantiated before they can be used in any form. // 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: case *ast.ArrayType:
if e.Len == nil { if e.Len == nil {
typ := new(Slice) typ := new(Slice)
setDefType(def, typ)
typ.elem = check.varType(e.Elt) typ.elem = check.varType(e.Elt)
return typ return typ
} }
typ := new(Array) typ := new(Array)
setDefType(def, typ)
// Provide a more specific error when encountering a [...] array // Provide a more specific error when encountering a [...] array
// rather than leaving it to the handling of the ... expression. // rather than leaving it to the handling of the ... expression.
if _, ok := e.Len.(*ast.Ellipsis); ok { 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: case *ast.StructType:
typ := new(Struct) typ := new(Struct)
setDefType(def, typ)
check.structType(typ, e) check.structType(typ, e)
return typ return typ
case *ast.StarExpr: case *ast.StarExpr:
typ := new(Pointer) typ := new(Pointer)
typ.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration typ.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration
setDefType(def, typ)
typ.base = check.varType(e.X) typ.base = check.varType(e.X)
// If typ.base is invalid, it's unlikely that *base is particularly // If typ.base is invalid, it's unlikely that *base is particularly
// useful - even a valid dereferenciation will lead to an invalid // 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: case *ast.FuncType:
typ := new(Signature) typ := new(Signature)
setDefType(def, typ)
check.funcType(typ, nil, e) check.funcType(typ, nil, e)
return typ return typ
case *ast.InterfaceType: case *ast.InterfaceType:
typ := check.newInterface() typ := check.newInterface()
setDefType(def, typ)
check.interfaceType(typ, e, def) check.interfaceType(typ, e, def)
return typ return typ
case *ast.MapType: case *ast.MapType:
typ := new(Map) typ := new(Map)
setDefType(def, typ)
typ.key = check.varType(e.Key) typ.key = check.varType(e.Key)
typ.elem = check.varType(e.Value) typ.elem = check.varType(e.Value)
@ -384,7 +371,6 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
case *ast.ChanType: case *ast.ChanType:
typ := new(Chan) typ := new(Chan)
setDefType(def, typ)
dir := SendRecv dir := SendRecv
switch e.Dir { switch e.Dir {
@ -409,14 +395,10 @@ func (check *Checker) typInternal(e0 ast.Expr, def *TypeName) (T Type) {
} }
typ := Typ[Invalid] typ := Typ[Invalid]
setDefType(def, typ)
return typ return typ
} }
// TODO(markfreeman): Remove this function. func (check *Checker) instantiatedType(ix *indexedExpr) (res Type) {
func setDefType(def *TypeName, typ Type) {}
func (check *Checker) instantiatedType(ix *indexedExpr, def *TypeName) (res Type) {
if check.conf._Trace { if check.conf._Trace {
check.trace(ix.Pos(), "-- instantiating type %s with %s", ix.x, ix.indices) check.trace(ix.Pos(), "-- instantiating type %s with %s", ix.x, ix.indices)
check.indent++ check.indent++
@ -427,10 +409,6 @@ func (check *Checker) instantiatedType(ix *indexedExpr, def *TypeName) (res Type
}() }()
} }
defer func() {
setDefType(def, res)
}()
var cause string var cause string
typ := check.genericType(ix.x, &cause) typ := check.genericType(ix.x, &cause)
if cause != "" { if cause != "" {