From 1134377d85029563a48354a8397fa341b2d2e037 Mon Sep 17 00:00:00 2001 From: visualfc Date: Sat, 13 Jan 2024 15:52:34 +0800 Subject: [PATCH] cl: toType error return types.Invalid --- cl/error_msg_test.go | 26 ++++++++++++++++++++++++++ cl/func_type_and_var.go | 6 +++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/cl/error_msg_test.go b/cl/error_msg_test.go index fe2bf1df5..4a4e1a589 100644 --- a/cl/error_msg_test.go +++ b/cl/error_msg_test.go @@ -23,6 +23,7 @@ import ( "runtime" "testing" + "github.com/goplus/gop/ast" "github.com/goplus/gop/cl" "github.com/goplus/gop/parser" "github.com/goplus/gop/parser/fsx/memfs" @@ -53,6 +54,24 @@ func codeErrorTestEx(t *testing.T, pkgname, filename, msg, src string) { } } +func codeErrorTestAst(t *testing.T, pkgname, filename, msg, src string) { + f, _ := parser.ParseFile(gblFset, filename, src, parser.AllErrors) + pkg := &ast.Package{ + Name: pkgname, + Files: map[string]*ast.File{filename: f}, + } + conf := *gblConf + conf.NoFileLine = false + conf.RelativeBase = "/foo" + _, err := cl.NewPackage("", pkg, &conf) + if err == nil { + t.Fatal("no error?") + } + if ret := err.Error(); ret != msg { + t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg) + } +} + func TestErrLambdaExpr(t *testing.T) { codeErrorTest(t, "bar.gop:7:6: too few arguments in lambda expression\n\thave ()\n\twant (int, int)", ` @@ -969,3 +988,10 @@ func TestErrCompileFunc(t *testing.T) { printf("%+v\n", int32) `) } + +func TestToTypeError(t *testing.T) { + codeErrorTestAst(t, "main", "bar.gop", `bar.gop:3:3: toType unexpected: *ast.BadExpr`, ` +type +a := 1 +`) +} diff --git a/cl/func_type_and_var.go b/cl/func_type_and_var.go index ca8a37f1a..6d195d2a4 100644 --- a/cl/func_type_and_var.go +++ b/cl/func_type_and_var.go @@ -21,7 +21,6 @@ import ( "go/types" "log" "math/big" - "reflect" "strconv" "github.com/goplus/gop/ast" @@ -179,9 +178,10 @@ func toType(ctx *blockCtx, typ ast.Expr) (t types.Type) { return toIndexType(ctx, v) case *ast.IndexListExpr: return toIndexListType(ctx, v) + default: + ctx.handleErrorf(v.Pos(), "toType unexpected: %T", v) + return types.Typ[types.Invalid] } - log.Panicln("toType: unknown -", reflect.TypeOf(typ)) - return nil } var (