]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: factor out isdddArray and inNode helper functions
authorRobert Griesemer <gri@golang.org>
Wed, 4 Sep 2024 23:42:56 +0000 (16:42 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 5 Sep 2024 22:09:22 +0000 (22:09 +0000)
Preparation for generation of go/types/literals.go from types2 sources.

Change-Id: I9af23fbe1e448976394ddd7b348188c2595d8afe
Reviewed-on: https://go-review.googlesource.com/c/go/+/610557
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Tim King <taking@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/literals.go
src/cmd/compile/internal/types2/util.go
src/go/types/literals.go
src/go/types/util.go

index 43149ec9b9b4c77b935aef9cb76e85388428a6a4..fbb139be8f04c7abc237433c9e722ff7621ca4ed 100644 (file)
@@ -20,7 +20,7 @@ func (check *Checker) compositeLit(T *target, x *operand, e *syntax.CompositeLit
                // composite literal type present - use it
                // [...]T array types may only appear with composite literals.
                // Check for them here so we don't have to handle ... in general.
-               if atyp, _ := e.Type.(*syntax.ArrayType); atyp != nil && atyp.Len == nil {
+               if atyp, _ := e.Type.(*syntax.ArrayType); atyp != nil && isdddArray(atyp) {
                        // We have an "open" [...]T array type.
                        // Create a new ArrayType with unknown length (-1)
                        // and finish setting it up after analyzing the literal.
@@ -124,7 +124,7 @@ func (check *Checker) compositeLit(T *target, x *operand, e *syntax.CompositeLit
                                check.assignment(x, etyp, "struct literal")
                        }
                        if len(e.ElemList) < len(fields) {
-                               check.errorf(e.Rbrace, InvalidStructLit, "too few values in struct literal of type %s", base)
+                               check.errorf(inNode(e, e.Rbrace), InvalidStructLit, "too few values in struct literal of type %s", base)
                                // ok to continue
                        }
                }
index db0a3e70badb43490d653317e7782d6187240a75..26c6d4eaea9452cbd3bcdac2426315bc115f22cb 100644 (file)
@@ -36,6 +36,9 @@ func dddErrPos(call *syntax.CallExpr) *syntax.CallExpr {
        return call
 }
 
+// isdddArray reports whether atyp is of the form [...]E.
+func isdddArray(atyp *syntax.ArrayType) bool { return atyp.Len == nil }
+
 // argErrPos returns the node (poser) for reporting an invalid argument count.
 func argErrPos(call *syntax.CallExpr) *syntax.CallExpr { return call }
 
@@ -48,6 +51,9 @@ func startPos(n syntax.Node) syntax.Pos { return syntax.StartPos(n) }
 // endPos returns the position of the first character immediately after node n.
 func endPos(n syntax.Node) syntax.Pos { return syntax.EndPos(n) }
 
+// inNode is a dummy function returning pos.
+func inNode(_ syntax.Node, pos syntax.Pos) syntax.Pos { return pos }
+
 // makeFromLiteral returns the constant value for the given literal string and kind.
 func makeFromLiteral(lit string, kind syntax.LitKind) constant.Value {
        return constant.MakeFromLiteral(lit, kind2tok[kind], 0)
index f35df424752611e49935da73d18c6d019051ec57..d2bd7b5d15c5a1391af443f47547e39c433a90a2 100644 (file)
@@ -20,15 +20,13 @@ func (check *Checker) compositeLit(T *target, x *operand, e *ast.CompositeLit, h
                // composite literal type present - use it
                // [...]T array types may only appear with composite literals.
                // Check for them here so we don't have to handle ... in general.
-               if atyp, _ := e.Type.(*ast.ArrayType); atyp != nil && atyp.Len != nil {
-                       if ellip, _ := atyp.Len.(*ast.Ellipsis); ellip != nil && ellip.Elt == nil {
-                               // We have an "open" [...]T array type.
-                               // Create a new ArrayType with unknown length (-1)
-                               // and finish setting it up after analyzing the literal.
-                               typ = &Array{len: -1, elem: check.varType(atyp.Elt)}
-                               base = typ
-                               break
-                       }
+               if atyp, _ := e.Type.(*ast.ArrayType); atyp != nil && isdddArray(atyp) {
+                       // We have an "open" [...]T array type.
+                       // Create a new ArrayType with unknown length (-1)
+                       // and finish setting it up after analyzing the literal.
+                       typ = &Array{len: -1, elem: check.varType(atyp.Elt)}
+                       base = typ
+                       break
                }
                typ = check.typ(e.Type)
                base = typ
index 5d4ccc6f1ff15e14c9bc19be79eb62305a43488c..c01a4da479cc63a412bf16c2344ea2cd0b605589 100644 (file)
@@ -33,6 +33,16 @@ func hasDots(call *ast.CallExpr) bool { return call.Ellipsis.IsValid() }
 // dddErrPos returns the positioner for reporting an invalid ... use in a call.
 func dddErrPos(call *ast.CallExpr) positioner { return atPos(call.Ellipsis) }
 
+// isdddArray reports whether atyp is of the form [...]E.
+func isdddArray(atyp *ast.ArrayType) bool {
+       if atyp.Len != nil {
+               if ddd, _ := atyp.Len.(*ast.Ellipsis); ddd != nil && ddd.Elt == nil {
+                       return true
+               }
+       }
+       return false
+}
+
 // argErrPos returns positioner for reporting an invalid argument count.
 func argErrPos(call *ast.CallExpr) positioner { return inNode(call, call.Rparen) }