]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: remove InvalidTypeCycle from literals.go
authorMark Freeman <mark@golang.org>
Tue, 25 Nov 2025 18:56:18 +0000 (13:56 -0500)
committerGopher Robot <gobot@golang.org>
Thu, 27 Nov 2025 00:20:21 +0000 (16:20 -0800)
Both CL 722161 and CL 724140 implement a more general solution to
detecting cycles involving values of a type on the object path.

The logic in literals.go was intended to be a stop-gap solution and
is no longer necessary.

Change-Id: I328c0febf35444f07fc1894278dc76ab140710bf
Reviewed-on: https://go-review.googlesource.com/c/go/+/724380
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Mark Freeman <markfreeman@google.com>

src/cmd/compile/internal/types2/literals.go
src/go/types/literals.go

index 5b2dae9b13a72635f33e91c0e83f3ddb3fd21945..ed1c3f695c8807d0fe5409b65a8d6b6d70caf33c 100644 (file)
@@ -145,13 +145,6 @@ func (check *Checker) compositeLit(x *operand, e *syntax.CompositeLit, hint Type
 
        switch u, _ := commonUnder(base, nil); utyp := u.(type) {
        case *Struct:
-               // Prevent crash if the struct referred to is not yet set up.
-               // See analogous comment for *Array.
-               if utyp.fields == nil {
-                       check.error(e, InvalidTypeCycle, "invalid recursive type")
-                       x.mode = invalid
-                       return
-               }
                if len(e.ElemList) == 0 {
                        break
                }
@@ -225,14 +218,6 @@ func (check *Checker) compositeLit(x *operand, e *syntax.CompositeLit, hint Type
                }
 
        case *Array:
-               // Prevent crash if the array referred to is not yet set up. Was go.dev/issue/18643.
-               // This is a stop-gap solution. Should use Checker.objPath to report entire
-               // path starting with earliest declaration in the source. TODO(gri) fix this.
-               if utyp.elem == nil {
-                       check.error(e, InvalidTypeCycle, "invalid recursive type")
-                       x.mode = invalid
-                       return
-               }
                n := check.indexedElts(e.ElemList, utyp.elem, utyp.len)
                // If we have an array of unknown length (usually [...]T arrays, but also
                // arrays [n]T where n is invalid) set the length now that we know it and
@@ -254,23 +239,9 @@ func (check *Checker) compositeLit(x *operand, e *syntax.CompositeLit, hint Type
                }
 
        case *Slice:
-               // Prevent crash if the slice referred to is not yet set up.
-               // See analogous comment for *Array.
-               if utyp.elem == nil {
-                       check.error(e, InvalidTypeCycle, "invalid recursive type")
-                       x.mode = invalid
-                       return
-               }
                check.indexedElts(e.ElemList, utyp.elem, -1)
 
        case *Map:
-               // Prevent crash if the map referred to is not yet set up.
-               // See analogous comment for *Array.
-               if utyp.key == nil || utyp.elem == nil {
-                       check.error(e, InvalidTypeCycle, "invalid recursive type")
-                       x.mode = invalid
-                       return
-               }
                // If the map key type is an interface (but not a type parameter),
                // the type of a constant key must be considered when checking for
                // duplicates.
index df02b7703642e6992e519f36c2cfaf6e9f2adcf3..7ca351e60b1087df69f0209408a69b36659e492f 100644 (file)
@@ -149,13 +149,6 @@ func (check *Checker) compositeLit(x *operand, e *ast.CompositeLit, hint Type) {
 
        switch u, _ := commonUnder(base, nil); utyp := u.(type) {
        case *Struct:
-               // Prevent crash if the struct referred to is not yet set up.
-               // See analogous comment for *Array.
-               if utyp.fields == nil {
-                       check.error(e, InvalidTypeCycle, "invalid recursive type")
-                       x.mode = invalid
-                       return
-               }
                if len(e.Elts) == 0 {
                        break
                }
@@ -229,14 +222,6 @@ func (check *Checker) compositeLit(x *operand, e *ast.CompositeLit, hint Type) {
                }
 
        case *Array:
-               // Prevent crash if the array referred to is not yet set up. Was go.dev/issue/18643.
-               // This is a stop-gap solution. Should use Checker.objPath to report entire
-               // path starting with earliest declaration in the source. TODO(gri) fix this.
-               if utyp.elem == nil {
-                       check.error(e, InvalidTypeCycle, "invalid recursive type")
-                       x.mode = invalid
-                       return
-               }
                n := check.indexedElts(e.Elts, utyp.elem, utyp.len)
                // If we have an array of unknown length (usually [...]T arrays, but also
                // arrays [n]T where n is invalid) set the length now that we know it and
@@ -258,23 +243,9 @@ func (check *Checker) compositeLit(x *operand, e *ast.CompositeLit, hint Type) {
                }
 
        case *Slice:
-               // Prevent crash if the slice referred to is not yet set up.
-               // See analogous comment for *Array.
-               if utyp.elem == nil {
-                       check.error(e, InvalidTypeCycle, "invalid recursive type")
-                       x.mode = invalid
-                       return
-               }
                check.indexedElts(e.Elts, utyp.elem, -1)
 
        case *Map:
-               // Prevent crash if the map referred to is not yet set up.
-               // See analogous comment for *Array.
-               if utyp.key == nil || utyp.elem == nil {
-                       check.error(e, InvalidTypeCycle, "invalid recursive type")
-                       x.mode = invalid
-                       return
-               }
                // If the map key type is an interface (but not a type parameter),
                // the type of a constant key must be considered when checking for
                // duplicates.