From c048a9a11f21e879a76e328b7a92f3a47f298b37 Mon Sep 17 00:00:00 2001 From: Mark Freeman Date: Tue, 25 Nov 2025 13:56:18 -0500 Subject: [PATCH] go/types, types2: remove InvalidTypeCycle from literals.go 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 Reviewed-by: Robert Griesemer Auto-Submit: Mark Freeman --- src/cmd/compile/internal/types2/literals.go | 29 --------------------- src/go/types/literals.go | 29 --------------------- 2 files changed, 58 deletions(-) diff --git a/src/cmd/compile/internal/types2/literals.go b/src/cmd/compile/internal/types2/literals.go index 5b2dae9b13a..ed1c3f695c8 100644 --- a/src/cmd/compile/internal/types2/literals.go +++ b/src/cmd/compile/internal/types2/literals.go @@ -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. diff --git a/src/go/types/literals.go b/src/go/types/literals.go index df02b770364..7ca351e60b1 100644 --- a/src/go/types/literals.go +++ b/src/go/types/literals.go @@ -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.