Skip to content

Commit

Permalink
cl: toType error return types.Invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Jan 13, 2024
1 parent a085ab3 commit 1134377
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 26 additions & 0 deletions cl/error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)", `
Expand Down Expand Up @@ -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
`)
}
6 changes: 3 additions & 3 deletions cl/func_type_and_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"go/types"
"log"
"math/big"
"reflect"
"strconv"

"github.com/goplus/gop/ast"
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 1134377

Please sign in to comment.