1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:04:46 -07:00

go.tools/go/ssa: permit "for range x"

+ test

Also: don't generate init() functions for packages loaded from export data.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/111200043
This commit is contained in:
Alan Donovan 2014-07-15 17:34:50 +01:00
parent e078800d1f
commit b2ea2e8560
2 changed files with 15 additions and 1 deletions

View File

@ -1590,6 +1590,7 @@ func (b *builder) rangeIndexed(fn *Function, x Value, tv types.Type) (k, v Value
// data dependence upon x, permitting later dead-code
// elimination if x is pure, static unrolling, etc.
// Ranging over a nil *array may have >0 iterations.
// We still generate code for x, in case it has effects.
length = intConst(arr.Len())
} else {
// length = len(x).
@ -1764,7 +1765,7 @@ func (b *builder) rangeChan(fn *Function, x Value, tk types.Type, pos token.Pos)
//
func (b *builder) rangeStmt(fn *Function, s *ast.RangeStmt, label *lblock) {
var tk, tv types.Type
if !isBlankIdent(s.Key) {
if s.Key != nil && !isBlankIdent(s.Key) {
tk = fn.Pkg.typeOf(s.Key)
}
if s.Value != nil && !isBlankIdent(s.Value) {
@ -2156,6 +2157,11 @@ func (p *Package) Build() {
if p.info == nil {
return // synthetic package, e.g. "testmain"
}
if len(p.info.Files) == 0 {
p.info = nil
return // package loaded from export data
}
// Ensure we have runtime type info for all exported members.
// TODO(adonovan): ideally belongs in memberFromObject, but
// that would require package creation in topological order.

View File

@ -150,6 +150,14 @@ func init() {
if s != "Hello, 世界" {
panic(s)
}
var x int
for range "Hello, 世界" {
x++
}
if x != len(indices) {
panic(x)
}
}
func main() {