modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/fixedbugs/issue4562.go (about)

     1  // run
     2  
     3  // Copyright 2012 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  	"runtime"
    12  	"strings"
    13  )
    14  
    15  type T struct {
    16  	val int
    17  }
    18  
    19  func main() {
    20  	defer expectError(22)
    21  	var pT *T
    22  	switch pT.val { // error should be here - line 22
    23  	case 0:
    24  		fmt.Println("0")
    25  	case 1: // used to show up here instead
    26  		fmt.Println("1")
    27  	case 2:
    28  		fmt.Println("2")
    29  	}
    30  	fmt.Println("finished")
    31  }
    32  
    33  func expectError(expectLine int) {
    34  	if recover() == nil {
    35  		panic("did not crash")
    36  	}
    37  	for i := 1;; i++ {
    38  		_, file, line, ok := runtime.Caller(i)
    39  		if !ok {
    40  			panic("cannot find issue4562.go on stack")
    41  		}
    42  		if strings.HasSuffix(file, "issue4562.go") {
    43  			if line != expectLine {
    44  				panic(fmt.Sprintf("crashed at line %d, wanted line %d", line, expectLine))
    45  			}
    46  			break
    47  		}
    48  	}
    49  }