Permalink
Browse files

natives/runtime: Update TypeAssertionError instantiation.

In the relevant upstream change is golang/go@cda1947,
fields of TypeAssertionError are changed from string to *_type.
This change follows suit and updates the instantiation of the
TypeAssertionError type in $assertType of compiler/prelude to
use the new definition. This happens when a type assertion fails.

Add test coverage for the runtime error string, since it wasn't
covered before.

Fixes fixedbugs/issue16130.go and fixedbugs/issue26094.go tests.
  • Loading branch information...
dmitshur committed Aug 23, 2018
1 parent f2f28a8 commit 1f566bed91de2daf999967192b4066c28be028e1
Showing with 30 additions and 2 deletions.
  1. +2 −1 compiler/natives/src/runtime/runtime.go
  2. +5 −1 compiler/prelude/types.go
  3. +23 −0 tests/misc_test.go
@@ -17,10 +17,11 @@ type eface struct {
_type *_type
}
type _type struct {
str string
}
func (t *_type) string() string {
return ""
return t.str
}
func (t *_type) pkgpath() string {
return ""
@@ -729,7 +729,11 @@ var $assertType = function(value, type, returnTuple) {
if (returnTuple) {
return [type.zero(), false];
}
$panic(new $packages["runtime"].TypeAssertionError.ptr("", (value === $ifaceNil ? "" : value.constructor.string), type.string, missingMethod));
$panic(new $packages["runtime"].TypeAssertionError.ptr(
$packages["runtime"]._type.ptr.nil,
(value === $ifaceNil ? $packages["runtime"]._type.ptr.nil : new $packages["runtime"]._type.ptr(value.constructor.string)),
new $packages["runtime"]._type.ptr(type.string),
missingMethod));
}
if (!isInterface) {
View
@@ -672,3 +672,26 @@ func TestSlicingNilSlice(t *testing.T) {
}
})
}
// Ensure that doing an interface conversion that fails
// produces an expected error type with the right error text.
func TestInterfaceConversionRuntimeError(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("got no panic, want panic")
}
re, ok := r.(runtime.Error)
if !ok {
t.Fatalf("got %T, want runtime.Error", r)
}
if got, want := re.Error(), "interface conversion: int is not tests.I: missing method Get"; got != want {
t.Fatalf("got %q, want %q", got, want)
}
}()
type I interface {
Get() int
}
e := (interface{})(0)
_ = e.(I)
}

0 comments on commit 1f566be

Please sign in to comment.