Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cl: toType error return types.Invalid #1628

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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