Skip to content

Commit b17a55d

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: move Checker.indexedElts into literals.go where it belongs
The function is only used by Checker.compositeLit. Also, now its go/types source can be gerated from the types2 source. No other code changes. Change-Id: I88b7ad371d809a5d9bf8e635d9e003ba0a71ab78 Reviewed-on: https://go-review.googlesource.com/c/go/+/615635 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Tim King <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent 681751a commit b17a55d

File tree

4 files changed

+94
-94
lines changed

4 files changed

+94
-94
lines changed

src/cmd/compile/internal/types2/index.go

-47
Original file line numberDiff line numberDiff line change
@@ -415,50 +415,3 @@ func (check *Checker) isValidIndex(x *operand, code Code, what string, allowNega
415415

416416
return true
417417
}
418-
419-
// indexedElts checks the elements (elts) of an array or slice composite literal
420-
// against the literal's element type (typ), and the element indices against
421-
// the literal length if known (length >= 0). It returns the length of the
422-
// literal (maximum index value + 1).
423-
func (check *Checker) indexedElts(elts []syntax.Expr, typ Type, length int64) int64 {
424-
visited := make(map[int64]bool, len(elts))
425-
var index, max int64
426-
for _, e := range elts {
427-
// determine and check index
428-
validIndex := false
429-
eval := e
430-
if kv, _ := e.(*syntax.KeyValueExpr); kv != nil {
431-
if typ, i := check.index(kv.Key, length); isValid(typ) {
432-
if i >= 0 {
433-
index = i
434-
validIndex = true
435-
} else {
436-
check.errorf(e, InvalidLitIndex, "index %s must be integer constant", kv.Key)
437-
}
438-
}
439-
eval = kv.Value
440-
} else if length >= 0 && index >= length {
441-
check.errorf(e, OversizeArrayLit, "index %d is out of bounds (>= %d)", index, length)
442-
} else {
443-
validIndex = true
444-
}
445-
446-
// if we have a valid index, check for duplicate entries
447-
if validIndex {
448-
if visited[index] {
449-
check.errorf(e, DuplicateLitKey, "duplicate index %d in array or slice literal", index)
450-
}
451-
visited[index] = true
452-
}
453-
index++
454-
if index > max {
455-
max = index
456-
}
457-
458-
// check element against composite literal element type
459-
var x operand
460-
check.exprWithHint(&x, eval, typ)
461-
check.assignment(&x, typ, "array or slice literal")
462-
}
463-
return max
464-
}

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

+47
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,50 @@ func (check *Checker) compositeLit(x *operand, e *syntax.CompositeLit, hint Type
340340
x.mode = value
341341
x.typ = typ
342342
}
343+
344+
// indexedElts checks the elements (elts) of an array or slice composite literal
345+
// against the literal's element type (typ), and the element indices against
346+
// the literal length if known (length >= 0). It returns the length of the
347+
// literal (maximum index value + 1).
348+
func (check *Checker) indexedElts(elts []syntax.Expr, typ Type, length int64) int64 {
349+
visited := make(map[int64]bool, len(elts))
350+
var index, max int64
351+
for _, e := range elts {
352+
// determine and check index
353+
validIndex := false
354+
eval := e
355+
if kv, _ := e.(*syntax.KeyValueExpr); kv != nil {
356+
if typ, i := check.index(kv.Key, length); isValid(typ) {
357+
if i >= 0 {
358+
index = i
359+
validIndex = true
360+
} else {
361+
check.errorf(e, InvalidLitIndex, "index %s must be integer constant", kv.Key)
362+
}
363+
}
364+
eval = kv.Value
365+
} else if length >= 0 && index >= length {
366+
check.errorf(e, OversizeArrayLit, "index %d is out of bounds (>= %d)", index, length)
367+
} else {
368+
validIndex = true
369+
}
370+
371+
// if we have a valid index, check for duplicate entries
372+
if validIndex {
373+
if visited[index] {
374+
check.errorf(e, DuplicateLitKey, "duplicate index %d in array or slice literal", index)
375+
}
376+
visited[index] = true
377+
}
378+
index++
379+
if index > max {
380+
max = index
381+
}
382+
383+
// check element against composite literal element type
384+
var x operand
385+
check.exprWithHint(&x, eval, typ)
386+
check.assignment(&x, typ, "array or slice literal")
387+
}
388+
return max
389+
}

src/go/types/index.go

-47
Original file line numberDiff line numberDiff line change
@@ -408,50 +408,3 @@ func (check *Checker) isValidIndex(x *operand, code Code, what string, allowNega
408408

409409
return true
410410
}
411-
412-
// indexedElts checks the elements (elts) of an array or slice composite literal
413-
// against the literal's element type (typ), and the element indices against
414-
// the literal length if known (length >= 0). It returns the length of the
415-
// literal (maximum index value + 1).
416-
func (check *Checker) indexedElts(elts []ast.Expr, typ Type, length int64) int64 {
417-
visited := make(map[int64]bool, len(elts))
418-
var index, max int64
419-
for _, e := range elts {
420-
// determine and check index
421-
validIndex := false
422-
eval := e
423-
if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
424-
if typ, i := check.index(kv.Key, length); isValid(typ) {
425-
if i >= 0 {
426-
index = i
427-
validIndex = true
428-
} else {
429-
check.errorf(e, InvalidLitIndex, "index %s must be integer constant", kv.Key)
430-
}
431-
}
432-
eval = kv.Value
433-
} else if length >= 0 && index >= length {
434-
check.errorf(e, OversizeArrayLit, "index %d is out of bounds (>= %d)", index, length)
435-
} else {
436-
validIndex = true
437-
}
438-
439-
// if we have a valid index, check for duplicate entries
440-
if validIndex {
441-
if visited[index] {
442-
check.errorf(e, DuplicateLitKey, "duplicate index %d in array or slice literal", index)
443-
}
444-
visited[index] = true
445-
}
446-
index++
447-
if index > max {
448-
max = index
449-
}
450-
451-
// check element against composite literal element type
452-
var x operand
453-
check.exprWithHint(&x, eval, typ)
454-
check.assignment(&x, typ, "array or slice literal")
455-
}
456-
return max
457-
}

src/go/types/literals.go

+47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)