Skip to content

type assertion resolving to wrong type #5180

@dgryski

Description

@dgryski
~/go/src/github.com/dgryski/bug/aws $ tinygo run main.go
*main.Foo
0
*main.Foo
panic: runtime error at 0x000000010b195139: type assert failed
failed to run compiled binary /var/folders/5b/_hr1d1fd3qn4t9vtfx5p61wm0000gp/T/tinygo3370967634/main: signal: abort trap
~/go/src/github.com/dgryski/bug/aws $ cat main.go
package main

import (
	"reflect"
)

func main() {
	TestCopy1()
	TestCopyIgnoreNilMembers()
}

func TestCopy1() {
	type Foo struct {
		A int
	}
	f1 := &Foo{}
	fcopy := CopyOf(f1)
	f2 := fcopy.(*Foo)
	println(f2.A)
}

func TestCopyIgnoreNilMembers() {
	type Foo struct {
		A *int
	}
	f1 := &Foo{}
	fcopy := CopyOf(f1)
	f2 := fcopy.(*Foo) // BOOM -- checking that it's TestCopy1.Foo instead
	println(f2.A)
}

func CopyOf(src interface{}) (dst interface{}) {
	t := reflect.TypeOf(src)
	println(t.String())
	dsti := reflect.New(t.Elem())
	dst = dsti.Interface()
	return dst
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions