Source file test/fixedbugs/issue33724.go

     1  // run
     2  package main
     3  
     4  import (
     5  	"fmt"
     6  	"runtime/debug"
     7  	"strings"
     8  )
     9  
    10  type Inner struct {
    11  	Err int
    12  }
    13  
    14  func (i *Inner) NotExpectedInStackTrace() int {
    15  	if i == nil {
    16  		return 86
    17  	}
    18  	return 17 + i.Err
    19  }
    20  
    21  type Outer struct {
    22  	Inner
    23  }
    24  
    25  func ExpectedInStackTrace() {
    26  	var o *Outer
    27  	println(o.NotExpectedInStackTrace())
    28  }
    29  
    30  func main() {
    31      defer func() {
    32          if r := recover(); r != nil {
    33          	stacktrace := string(debug.Stack())
    34          	if strings.Contains(stacktrace, "NotExpectedInStackTrace") {
    35          		fmt.Println("FAIL, stacktrace contains NotExpectedInStackTrace")
    36          	}
    37          	if !strings.Contains(stacktrace, "ExpectedInStackTrace") {
    38          		fmt.Println("FAIL, stacktrace does not contain ExpectedInStackTrace")
    39          	}
    40          } else {
    41          	fmt.Println("FAIL, should have panicked but did not")
    42          }
    43      }()
    44      ExpectedInStackTrace()
    45  }
    46  

View as plain text