1
0
mirror of https://github.com/golang/go synced 2024-11-05 18:46:11 -07:00
go/test/fixedbugs/issue16130.go
Ian Lance Taylor 09834d1c08 runtime: panic with the right error on iface conversion
A straight conversion from a type T to an interface type I, where T does
not implement I, should always panic with an interface conversion error
that shows the missing method.  This was not happening if the conversion
was done once using the comma-ok form (the result would not be OK) and
then again in a straight conversion.  Due to an error in the runtime
package the second conversion was failing with a nil pointer
dereference.

Fixes #16130.

Change-Id: I8b9fca0f1bb635a6181b8b76de8c2385bb7ac2d2
Reviewed-on: https://go-review.googlesource.com/24284
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Michel Lespinasse <walken@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2016-06-21 01:43:42 +00:00

44 lines
947 B
Go

// run
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test that an interface conversion error panics with an "interface
// conversion" run-time error. It was (incorrectly) panicing with a
// "nil pointer dereference."
package main
import (
"fmt"
"runtime"
"strings"
)
type I interface {
Get() int
}
func main() {
defer func() {
r := recover()
if r == nil {
panic("expected panic")
}
re, ok := r.(runtime.Error)
if !ok {
panic(fmt.Sprintf("got %T, expected runtime.Error", r))
}
if !strings.Contains(re.Error(), "interface conversion") {
panic(fmt.Sprintf("got %q, expected interface conversion error", re.Error()))
}
}()
e := (interface{})(0)
if _, ok := e.(I); ok {
panic("unexpected interface conversion success")
}
fmt.Println(e.(I))
panic("unexpected interface conversion success")
}