Skip to content

Commit 1f566be

Browse files
committed
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.
1 parent f2f28a8 commit 1f566be

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

compiler/natives/src/runtime/runtime.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ type eface struct {
1717
_type *_type
1818
}
1919
type _type struct {
20+
str string
2021
}
2122

2223
func (t *_type) string() string {
23-
return ""
24+
return t.str
2425
}
2526
func (t *_type) pkgpath() string {
2627
return ""

compiler/prelude/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,11 @@ var $assertType = function(value, type, returnTuple) {
729729
if (returnTuple) {
730730
return [type.zero(), false];
731731
}
732-
$panic(new $packages["runtime"].TypeAssertionError.ptr("", (value === $ifaceNil ? "" : value.constructor.string), type.string, missingMethod));
732+
$panic(new $packages["runtime"].TypeAssertionError.ptr(
733+
$packages["runtime"]._type.ptr.nil,
734+
(value === $ifaceNil ? $packages["runtime"]._type.ptr.nil : new $packages["runtime"]._type.ptr(value.constructor.string)),
735+
new $packages["runtime"]._type.ptr(type.string),
736+
missingMethod));
733737
}
734738
735739
if (!isInterface) {

tests/misc_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,26 @@ func TestSlicingNilSlice(t *testing.T) {
672672
}
673673
})
674674
}
675+
676+
// Ensure that doing an interface conversion that fails
677+
// produces an expected error type with the right error text.
678+
func TestInterfaceConversionRuntimeError(t *testing.T) {
679+
defer func() {
680+
r := recover()
681+
if r == nil {
682+
t.Fatal("got no panic, want panic")
683+
}
684+
re, ok := r.(runtime.Error)
685+
if !ok {
686+
t.Fatalf("got %T, want runtime.Error", r)
687+
}
688+
if got, want := re.Error(), "interface conversion: int is not tests.I: missing method Get"; got != want {
689+
t.Fatalf("got %q, want %q", got, want)
690+
}
691+
}()
692+
type I interface {
693+
Get() int
694+
}
695+
e := (interface{})(0)
696+
_ = e.(I)
697+
}

0 commit comments

Comments
 (0)