github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/test/fixedbugs/issue4388.go (about)

     1  // run
     2  
     3  // Copyright 2014 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  package main
     8  
     9  import (
    10  	"fmt"
    11  	"io"
    12  	"runtime"
    13  )
    14  
    15  type T struct {
    16  	io.Closer
    17  }
    18  
    19  func f1() {
    20  	// The 5 here and below depends on the number of internal runtime frames
    21  	// that sit between a deferred function called during panic and
    22  	// the original frame. If that changes, this test will start failing and
    23  	// the number here will need to be updated.
    24  	defer checkLine(5)
    25  	var t *T
    26  	var c io.Closer = t
    27  	c.Close()
    28  }
    29  
    30  func f2() {
    31  	defer checkLine(5)
    32  	var t T
    33  	var c io.Closer = t
    34  	c.Close()
    35  }
    36  
    37  func main() {
    38  	f1()
    39  	f2()
    40  }
    41  
    42  func checkLine(n int) {
    43  	if err := recover(); err == nil {
    44  		panic("did not panic")
    45  	}
    46  	var file string
    47  	var line int
    48  	for i := 1; i <= n; i++ {
    49  		_, file, line, _ = runtime.Caller(i)
    50  		if file != "<autogenerated>" || line != 1 {
    51  			continue
    52  		}
    53  		return
    54  	}
    55  	panic(fmt.Sprintf("expected <autogenerated>:1 have %s:%d", file, line))
    56  }