github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/test/fixedbugs/issue8613.go (about)

     1  // +build amd64
     2  // run
     3  
     4  // Copyright 2016 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  package main
     9  
    10  var out int
    11  var zero int
    12  
    13  func main() {
    14  	wantPanic("test1", func() {
    15  		out = 1 / zero
    16  	})
    17  	wantPanic("test2", func() {
    18  		_ = 1 / zero
    19  	})
    20  	wantPanic("test3", func() {
    21  		v := 0
    22  		_ = 1 / v
    23  	})
    24  	wantPanic("test4", func() { divby(0) })
    25  }
    26  
    27  func wantPanic(test string, fn func()) {
    28  	defer func() {
    29  		if e := recover(); e == nil {
    30  			panic(test + ": expected panic")
    31  		}
    32  	}()
    33  	fn()
    34  }
    35  
    36  //go:noinline
    37  func divby(v int) {
    38  	_ = 1 / v
    39  }