Source file test/fixedbugs/issue74379c.go

     1  // run
     2  
     3  // Copyright 2025 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  	"errors"
    11  	"fmt"
    12  	"os"
    13  )
    14  
    15  type S struct{ a, b int }
    16  
    17  func crashOnErr1(err error) S {
    18  	if err != nil {
    19  		panic(err)
    20  	}
    21  	return S{} // zero value struct
    22  }
    23  
    24  func f1() {
    25  	defer func() {
    26  		if recover() == nil {
    27  			fmt.Println("failed to have expected panic")
    28  			os.Exit(1)
    29  		}
    30  	}()
    31  	fmt.Println(crashOnErr1(errors.New("test error")))
    32  }
    33  
    34  func crashOnErr2(err error) S {
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  	return S{1, 2} // not zero value struct
    39  }
    40  
    41  func f2() {
    42  	defer func() {
    43  		if recover() == nil {
    44  			fmt.Println("failed to have expected panic")
    45  			os.Exit(1)
    46  		}
    47  	}()
    48  	fmt.Println(crashOnErr2(errors.New("test error")))
    49  }
    50  
    51  func main() {
    52  	f1()
    53  	f2()
    54  }
    55  

View as plain text