mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00: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:
parent
78c4dc3709
commit
137e4a6c63
@ -155,27 +155,36 @@ func walkrange(n *Node) *Node {
|
|||||||
lno := setlineno(a)
|
lno := setlineno(a)
|
||||||
n.Right = nil
|
n.Right = nil
|
||||||
|
|
||||||
var v1 *Node
|
var v1, v2 *Node
|
||||||
if n.List.Len() != 0 {
|
l := n.List.Len()
|
||||||
|
if l > 0 {
|
||||||
v1 = n.List.First()
|
v1 = n.List.First()
|
||||||
}
|
}
|
||||||
var v2 *Node
|
|
||||||
if n.List.Len() > 1 && !isblank(n.List.Second()) {
|
if l > 1 {
|
||||||
v2 = n.List.Second()
|
v2 = n.List.Second()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isblank(v2) {
|
||||||
|
v2 = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if isblank(v1) && v2 == nil {
|
||||||
|
v1 = nil
|
||||||
|
}
|
||||||
|
|
||||||
if v1 == nil && v2 != nil {
|
if v1 == nil && v2 != nil {
|
||||||
Fatalf("walkrange: v2 != nil while v1 == nil")
|
Fatalf("walkrange: v2 != nil while v1 == nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
var ifGuard *Node
|
|
||||||
|
|
||||||
translatedLoopOp := OFOR
|
|
||||||
|
|
||||||
// n.List has no meaning anymore, clear it
|
// n.List has no meaning anymore, clear it
|
||||||
// to avoid erroneous processing by racewalk.
|
// to avoid erroneous processing by racewalk.
|
||||||
n.List.Set(nil)
|
n.List.Set(nil)
|
||||||
|
|
||||||
|
var ifGuard *Node
|
||||||
|
|
||||||
|
translatedLoopOp := OFOR
|
||||||
|
|
||||||
var body []*Node
|
var body []*Node
|
||||||
var init []*Node
|
var init []*Node
|
||||||
switch t.Etype {
|
switch t.Etype {
|
||||||
|
@ -23,12 +23,57 @@ func seq(lo, hi int) chan int {
|
|||||||
return c
|
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() {
|
func testchan() {
|
||||||
s := ""
|
s := ""
|
||||||
for i := range seq('a', 'z') {
|
for i := range seq('a', 'z') {
|
||||||
s += string(i)
|
s += string(i)
|
||||||
}
|
}
|
||||||
if s != "abcdefghijklmnopqrstuvwxyz" {
|
if s != alphabet {
|
||||||
println("Wanted lowercase alphabet; got", s)
|
println("Wanted lowercase alphabet; got", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
@ -38,6 +83,7 @@ func testchan() {
|
|||||||
}
|
}
|
||||||
if n != 26 {
|
if n != 26 {
|
||||||
println("testchan wrong count", n, "want 26")
|
println("testchan wrong count", n, "want 26")
|
||||||
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -426,6 +472,7 @@ func testcalls() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
testblankvars()
|
||||||
testchan()
|
testchan()
|
||||||
testarray()
|
testarray()
|
||||||
testarray1()
|
testarray1()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user