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

     1  // run
     2  
     3  // Copyright 2016 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  // Test that an interface conversion error panics with an "interface
     8  // conversion" run-time error. It was (incorrectly) panicking with a
     9  // "nil pointer dereference."
    10  
    11  package main
    12  
    13  import (
    14  	"fmt"
    15  	"runtime"
    16  	"strings"
    17  )
    18  
    19  type I interface {
    20  	Get() int
    21  }
    22  
    23  func main() {
    24  	defer func() {
    25  		r := recover()
    26  		if r == nil {
    27  			panic("expected panic")
    28  		}
    29  		re, ok := r.(runtime.Error)
    30  		if !ok {
    31  			panic(fmt.Sprintf("got %T, expected runtime.Error", r))
    32  		}
    33  		if !strings.Contains(re.Error(), "interface conversion") {
    34  			panic(fmt.Sprintf("got %q, expected interface conversion error", re.Error()))
    35  		}
    36  	}()
    37  	e := (interface{})(0)
    38  	if _, ok := e.(I); ok {
    39  		panic("unexpected interface conversion success")
    40  	}
    41  	fmt.Println(e.(I))
    42  	panic("unexpected interface conversion success")
    43  }