Source file test/fixedbugs/issue79762.go

     1  // run
     2  
     3  // Copyright 2026 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  // Make sure we get the right line number for nil pointer panics.
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"runtime/debug"
    14  	"strings"
    15  )
    16  
    17  type T struct {
    18  	a int
    19  }
    20  
    21  func (t *T) foo() {
    22  	t.a = 3
    23  }
    24  
    25  func f1() {
    26  	var t *T
    27  	(*t).foo()
    28  }
    29  
    30  type U struct {
    31  	V
    32  }
    33  type V struct {
    34  	x int
    35  }
    36  
    37  func (v *V) setX() {
    38  	v.x = 7
    39  }
    40  
    41  func f2() {
    42  	var u *U
    43  	u.setX()
    44  }
    45  
    46  func check(stack string, good []string, bad []string) {
    47  	for _, g := range good {
    48  		if !strings.Contains(stack, g) {
    49  			fmt.Println(stack)
    50  			fmt.Printf("ERROR: stack doesn't contain %s\n", g)
    51  		}
    52  	}
    53  	for _, b := range bad {
    54  		if strings.Contains(stack, b) {
    55  			fmt.Println(stack)
    56  			fmt.Printf("ERROR: stack contains %s\n", b)
    57  		}
    58  	}
    59  }
    60  
    61  func test1() {
    62  	defer func() {
    63  		r := recover()
    64  		if r == nil {
    65  			fmt.Println("ERROR: f1 should have panicked")
    66  			return
    67  		}
    68  		check(string(debug.Stack()), []string{"f1", "issue79762.go:27"}, []string{"foo"})
    69  	}()
    70  	f1()
    71  }
    72  
    73  func test2() {
    74  	defer func() {
    75  		r := recover()
    76  		if r == nil {
    77  			fmt.Println("ERROR: f2 should have panicked")
    78  			return
    79  		}
    80  		check(string(debug.Stack()), []string{"f2", "issue79762.go:43"}, []string{"setX"})
    81  	}()
    82  	f2()
    83  }
    84  
    85  func main() {
    86  	test1()
    87  	test2()
    88  }
    89  

View as plain text