]> git.feebdaed.xyz Git - 0xmirror/go.git/commitdiff
go/types, types2: remove setDefType and most def plumbing
authorMark Freeman <mark@golang.org>
Mon, 24 Nov 2025 19:34:39 +0000 (14:34 -0500)
committerGopher Robot <gobot@golang.org>
Thu, 27 Nov 2025 00:17:55 +0000 (16:17 -0800)
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>
14 files changed:
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/lookup.go
src/cmd/compile/internal/types2/named.go
src/cmd/compile/internal/types2/resolver.go
src/cmd/compile/internal/types2/typexpr.go
src/go/types/call.go
src/go/types/decl.go
src/go/types/expr.go
src/go/types/lookup.go
src/go/types/named.go
src/go/types/resolver.go
src/go/types/typexpr.go

index 52dc33b8cd5bfef32c369a5a3ae07b25ca51b6da..3461c890a8b7ddb84cce52c46612943bad9edd9a 100644 (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 {
index 4f9f7c25e747ed4c9df93f7d5176c25be35b0649..25152545ece07232a607bde7e69405f954ae8351 100644 (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:
index 9d7580cb0168c9b54ee8bf3d0a32409d9eadeac6..637cbaee5d3ee63a5f926c9d868932199e90ca19 100644 (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) {
index 3e18db09f5c4982f772e03a0148d82033d5ba542..81a126b9c7df71fd44e77702eb7f97ba2bc03405 100644 (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 {
index b5c8ed142aa5bad27afc195a320dc73e829ae632..edd6357248ec792a48be782ba1764805549a9647 100644 (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)
index 4c9eeb329c8d585b61970c6a0c0fd10ec6eda502..2418de526864b22d4b076dd51fb8a5dcfcbfc7f9 100644 (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)
                }
        }
 
index c3e40184f5a1d55ba5af8b5edd28e19e4472ca47..a79b54eaccf8a86fb657448e707d66e28a901808 100644 (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 != "" {
index 3bc1a39ddcfa64bf1ca403045b4d94608fde283a..50aa7caba6da57c6cf58a7aab753f0396f96cb8c 100644 (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 {
index 1b0aa77ff3b850f85c36d3233d53552aad4d6d9a..80b8c20655110eea874f1297db7b7a607ef45ec8 100644 (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())
index 790e0111c3de5d9174e9366e9a8a978e753f9eea..09f7cdda802bad365d68f3556ce2b67ec8e9c68e 100644 (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)
index 97debb7395e129feb246221b0f63a3086d6660f7..7283db43f1219ed97da7a227495c92a98c5f5362 100644 (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 {
index b106d7a8eb703f38a565c8cd365dc087a7e0c9b1..be6a0f54267c0268a6aa2d1132541109f0f813f4 100644 (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)
index 2017b9c881379bf3e546d4d68d6b440a19c7c888..52576db2bd116b63d2c83f46bcda263f3c137252 100644 (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)
                }
        }
 
index c1381ddf4afaf97e8ea87de46c7820f85877fbc5..346ff18e9ad7de195ab20761b15366f03c970971 100644 (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 != "" {