From d9414573ac9b4ec2a02653fe8d28c734e6b7f54e Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 23 Jul 2022 21:37:46 +0800 Subject: [PATCH] getVStruct: type not found --- cl/blockctx.go | 5 ++++- cl/compile_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cl/blockctx.go b/cl/blockctx.go index 7d30ccd..209a9ff 100644 --- a/cl/blockctx.go +++ b/cl/blockctx.go @@ -475,7 +475,10 @@ func (p *blockCtx) buildVStruct(struc *types.Struct, vfs gox.VFields) *types.Str } func (p *blockCtx) getVStruct(typ *types.Named) *types.Struct { - t := typ.Underlying().(*types.Struct) + t, ok := typ.Underlying().(*types.Struct) + if !ok { + log.Panicln(typ.Obj().Name(), "not found") + } if vfs, ok := p.pkg.VFields(typ); ok { t = p.buildVStruct(t, vfs) } diff --git a/cl/compile_test.go b/cl/compile_test.go index 7146cd4..ebbacc8 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -165,6 +165,23 @@ func testPanic(t *testing.T, panicMsg string, doPanic func()) { // ----------------------------------------------------------------------------- +func TestGetVStruct(t *testing.T) { + defer func() { + if e := recover(); e != nil { + if emsg, ok := e.(string); ok { + if emsg == "foo not found\n" { + return + } + } + t.Fatal("TestGetVStruct failed:", e) + } + t.Fatal("TestGetVStruct: no error?") + }() + var ctx blockCtx + typ := types.NewNamed(types.NewTypeName(0, nil, "foo", nil), nil, nil) + ctx.getVStruct(typ) +} + func TestFuncAndDecl(t *testing.T) { testFunc(t, "testKeyword", ` void test(int var) {