Skip to content

Commit

Permalink
go/types, types2: factor out isdddArray and inNode helper functions
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
griesemer authored and gopherbot committed Sep 5, 2024
1 parent 0fb35ef commit aca0e2b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/cmd/compile/internal/types2/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/compile/internal/types2/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions src/go/types/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/go/types/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down

0 comments on commit aca0e2b

Please sign in to comment.