Skip to content

Commit

Permalink
cmd/compile: prevent double-walk of switch for OPRINT/OPRINTN
Browse files Browse the repository at this point in the history
When a println arg contains a call to an inlineable function
that itself contains a switch, that switch statement will be
walked twice, once by the walkexprlist formerly in the
OPRINT/OPRINTN case, then by walkexprlistcheap in walkprint.

Remove the first walkexprlist, it is not necessary.
walkexprlist =
		s[i] = walkexpr(s[i], init)
walkexprlistcheap = {
		s[i] = cheapexpr(n, init)
		s[i] = walkexpr(s[i], init)
}

Seems like this might be possible in other places, i.e.,
calls to inlineable switch-containing functions.

See also #25776.
Fixes #29220.

Change-Id: I3781e86aad6688711597b8bee9bc7ebd3af93601
Reviewed-on: https://go-review.googlesource.com/c/154497
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
  • Loading branch information
dr2chase committed Dec 17, 2018
1 parent 213845f commit d924c33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/cmd/compile/internal/gc/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ opswitch:
n = walkinrange(n, init)

case OPRINT, OPRINTN:
walkexprlist(n.List.Slice(), init)
n = walkprint(n, init)

case OPANIC:
Expand Down
26 changes: 26 additions & 0 deletions test/fixedbugs/issue29220.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// compile

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

func ascii(r rune) rune {
switch {
case 97 <= r && r <= 122:
return r - 32
case 65 <= r && r <= 90:
return r + 32
default:
return r
}
}

func main() {
nomeObjeto := "ABE1FK21"
println(string(nomeObjeto[1:4]))
println(ascii(rune(nomeObjeto[4])) >= 48 && ascii(rune(nomeObjeto[4])) <= 57)
println(string(nomeObjeto[5]))
println(string(nomeObjeto[6:10]))
}

0 comments on commit d924c33

Please sign in to comment.