Skip to content

Commit

Permalink
Merge pull request #1628 from visualfc/fix_totype
Browse files Browse the repository at this point in the history
cl: toType error return types.Invalid
  • Loading branch information
xushiwei committed Jan 13, 2024
2 parents a085ab3 + 1134377 commit d3f76ce
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 d3f76ce

Please sign in to comment.