mirror of
https://github.com/golang/go
synced 2024-11-13 18:30:26 -07:00
7395083136
Currently, order desugars map assignment operations like m[k] op= r into m[k] = m[k] op r which in turn is transformed during walk into: tmp := *mapaccess(m, k) tmp = tmp op r *mapassign(m, k) = tmp However, this is suboptimal, as we could instead produce just: *mapassign(m, k) op= r One complication though is if "r == 0", then "m[k] /= r" and "m[k] %= r" will panic, and they need to do so *before* calling mapassign, otherwise we may insert a new zero-value element into the map. It would be spec compliant to just emit the "r != 0" check before calling mapassign (see #23735), but currently these checks aren't generated until SSA construction. For now, it's simpler to continue desugaring /= and %= into two map indexing operations. Fixes #23661. Change-Id: I46e3739d9adef10e92b46fdd78b88d5aabe68952 Reviewed-on: https://go-review.googlesource.com/91557 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
78 lines
1.2 KiB
Go
78 lines
1.2 KiB
Go
// run
|
|
|
|
// Copyright 2017 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.
|
|
|
|
// Test to make sure RHS is evaluated before map insert is started.
|
|
// The RHS panics in all of these cases.
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
for i, f := range []func(map[int]int){
|
|
f0, f1, f2, f3, f4, f5, f6, f7, f8,
|
|
} {
|
|
m := map[int]int{}
|
|
func() { // wrapper to scope the defer.
|
|
defer func() {
|
|
recover()
|
|
}()
|
|
f(m) // Will panic. Shouldn't modify m.
|
|
fmt.Printf("RHS didn't panic, case f%d\n", i)
|
|
}()
|
|
if len(m) != 0 {
|
|
fmt.Printf("map insert happened, case f%d\n", i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func f0(m map[int]int) {
|
|
var p *int
|
|
m[0] = *p
|
|
}
|
|
|
|
func f1(m map[int]int) {
|
|
var p *int
|
|
m[0] += *p
|
|
}
|
|
|
|
func f2(m map[int]int) {
|
|
var p *int
|
|
sink, m[0] = sink, *p
|
|
}
|
|
|
|
func f3(m map[int]int) {
|
|
var p *chan int
|
|
m[0], sink = <-(*p)
|
|
}
|
|
|
|
func f4(m map[int]int) {
|
|
var p *interface{}
|
|
m[0], sink = (*p).(int)
|
|
}
|
|
|
|
func f5(m map[int]int) {
|
|
var p *map[int]int
|
|
m[0], sink = (*p)[0]
|
|
}
|
|
|
|
func f6(m map[int]int) {
|
|
var z int
|
|
m[0] /= z
|
|
}
|
|
|
|
func f7(m map[int]int) {
|
|
var a []int
|
|
m[0] = a[0]
|
|
}
|
|
|
|
func f8(m map[int]int) {
|
|
var z int
|
|
m[0] %= z
|
|
}
|
|
|
|
var sink bool
|