1
0
mirror of https://github.com/golang/go synced 2024-11-11 17:11:36 -07:00

cmd/compile: improve single blank variable handling in walkrange

Refactor walkrange to treat "for _ = range a" as "for range a".

This avoids generating some later discarded nodes in the compiler.

Passes toolstash -cmp.

Change-Id: Ifb2e1ca3b8519cbb67e8ad5aad514af9d18f1ec4
Reviewed-on: https://go-review.googlesource.com/61017
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Martin Möhrmann 2017-09-03 08:20:57 +02:00
parent 78c4dc3709
commit 137e4a6c63
2 changed files with 65 additions and 9 deletions

View File

@ -155,27 +155,36 @@ func walkrange(n *Node) *Node {
lno := setlineno(a)
n.Right = nil
var v1 *Node
if n.List.Len() != 0 {
var v1, v2 *Node
l := n.List.Len()
if l > 0 {
v1 = n.List.First()
}
var v2 *Node
if n.List.Len() > 1 && !isblank(n.List.Second()) {
if l > 1 {
v2 = n.List.Second()
}
if isblank(v2) {
v2 = nil
}
if isblank(v1) && v2 == nil {
v1 = nil
}
if v1 == nil && v2 != nil {
Fatalf("walkrange: v2 != nil while v1 == nil")
}
var ifGuard *Node
translatedLoopOp := OFOR
// n.List has no meaning anymore, clear it
// to avoid erroneous processing by racewalk.
n.List.Set(nil)
var ifGuard *Node
translatedLoopOp := OFOR
var body []*Node
var init []*Node
switch t.Etype {

View File

@ -23,12 +23,57 @@ func seq(lo, hi int) chan int {
return c
}
const alphabet = "abcdefghijklmnopqrstuvwxyz"
func testblankvars() {
n := 0
for range alphabet {
n++
}
if n != 26 {
println("for range: wrong count", n, "want 26")
panic("fail")
}
n = 0
for _ = range alphabet {
n++
}
if n != 26 {
println("for _ = range: wrong count", n, "want 26")
panic("fail")
}
n = 0
for _, _ = range alphabet {
n++
}
if n != 26 {
println("for _, _ = range: wrong count", n, "want 26")
panic("fail")
}
s := 0
for i, _ := range alphabet {
s += i
}
if s != 325 {
println("for i, _ := range: wrong sum", s, "want 325")
panic("fail")
}
r := rune(0)
for _, v := range alphabet {
r += v
}
if r != 2847 {
println("for _, v := range: wrong sum", r, "want 2847")
panic("fail")
}
}
func testchan() {
s := ""
for i := range seq('a', 'z') {
s += string(i)
}
if s != "abcdefghijklmnopqrstuvwxyz" {
if s != alphabet {
println("Wanted lowercase alphabet; got", s)
panic("fail")
}
@ -38,6 +83,7 @@ func testchan() {
}
if n != 26 {
println("testchan wrong count", n, "want 26")
panic("fail")
}
}
@ -426,6 +472,7 @@ func testcalls() {
}
func main() {
testblankvars()
testchan()
testarray()
testarray1()