Home | History | Annotate | Download | only in fixedbugs
      1 // run
      2 
      3 // Copyright 2017 The Go Authors. All rights reserved.
      4 // Use of this source code is governed by a BSD-style
      5 // license that can be found in the LICENSE file.
      6 
      7 // Test to make sure RHS is evaluated before map insert is started.
      8 // The RHS panics in all of these cases.
      9 
     10 package main
     11 
     12 import "fmt"
     13 
     14 func main() {
     15 	for i, f := range []func(map[int]int){
     16 		f0, f1, f2, f3, f4, f5, f6, f7,
     17 	} {
     18 		m := map[int]int{}
     19 		func() { // wrapper to scope the defer.
     20 			defer func() {
     21 				recover()
     22 			}()
     23 			f(m) // Will panic. Shouldn't modify m.
     24 			fmt.Printf("RHS didn't panic, case f%d\n", i)
     25 		}()
     26 		if len(m) != 0 {
     27 			fmt.Printf("map insert happened, case f%d\n", i)
     28 		}
     29 	}
     30 }
     31 
     32 func f0(m map[int]int) {
     33 	var p *int
     34 	m[0] = *p
     35 }
     36 
     37 func f1(m map[int]int) {
     38 	var p *int
     39 	m[0] += *p
     40 }
     41 
     42 func f2(m map[int]int) {
     43 	var p *int
     44 	sink, m[0] = sink, *p
     45 }
     46 
     47 func f3(m map[int]int) {
     48 	var p *chan int
     49 	m[0], sink = <-(*p)
     50 }
     51 
     52 func f4(m map[int]int) {
     53 	var p *interface{}
     54 	m[0], sink = (*p).(int)
     55 }
     56 
     57 func f5(m map[int]int) {
     58 	var p *map[int]int
     59 	m[0], sink = (*p)[0]
     60 }
     61 
     62 func f6(m map[int]int) {
     63 	var z int
     64 	m[0] /= z
     65 }
     66 
     67 func f7(m map[int]int) {
     68 	var a []int
     69 	m[0] = a[0]
     70 }
     71 
     72 var sink bool
     73