Source file test/defernil.go

     1  // run
     2  
     3  // Copyright 2019 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  // Check that deferring a nil function causes a proper
     8  // panic when the deferred function is invoked (not
     9  // when the function is deferred).
    10  // See Issue #8047 and #34926.
    11  
    12  package main
    13  
    14  var x = 0
    15  
    16  func main() {
    17  	defer func() {
    18  		err := recover()
    19  		if err == nil {
    20  			panic("did not panic")
    21  		}
    22  		if x != 1 {
    23  			panic("FAIL")
    24  		}
    25  	}()
    26  	f()
    27  }
    28  
    29  func f() {
    30  	var nilf func()
    31  	defer nilf()
    32  	x = 1
    33  }
    34  

View as plain text