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 fix ast.BadExpr #1622

Closed
wants to merge 2 commits into from
Closed
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
`)
}
15 changes: 5 additions & 10 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
case *ast.StarExpr:
compileStarExprLHS(ctx, v)
default:
log.Panicln("compileExpr failed: unknown -", reflect.TypeOf(v))
ctx.handleErrorf(v.Pos(), "compileExprLHS unexpected: %T", v)

Check warning on line 205 in cl/expr.go

View check run for this annotation

Codecov / codecov/patch

cl/expr.go#L205

Added line #L205 was not covered by tests
}
if rec := ctx.recorder(); rec != nil {
rec.recordExpr(ctx, expr, true)
Expand Down Expand Up @@ -275,12 +275,8 @@
compileErrWrapExpr(ctx, v, 0)
case *ast.FuncType:
ctx.cb.Typ(toFuncType(ctx, v, nil, nil), v)
case *ast.Ellipsis:
panic("compileEllipsis: ast.Ellipsis unexpected")
case *ast.KeyValueExpr:
panic("compileExpr: ast.KeyValueExpr unexpected")
default:
log.Panicln("compileExpr failed: unknown -", reflect.TypeOf(v))
ctx.handleErrorf(v.Pos(), "compileExpr unexpected: %T", v)

Check warning on line 279 in cl/expr.go

View check run for this annotation

Codecov / codecov/patch

cl/expr.go#L279

Added line #L279 was not covered by tests
}
if rec := ctx.recorder(); rec != nil {
rec.recordExpr(ctx, expr, false)
Expand Down Expand Up @@ -1167,7 +1163,7 @@
VarRef(err).
Val(pkg.Import(errorPkgPath).Ref("NewFrame")).
Val(err).
Val(sprintAst(pkg.Fset, v.X)).
Val(sprintAst(ctx, pkg.Fset, v.X)).
Val(relFile(ctx.relBaseDir, pos.Filename)).
Val(pos.Line).
Val(currentFuncName).
Expand All @@ -1189,13 +1185,12 @@
}
}

func sprintAst(fset *token.FileSet, x ast.Node) string {
func sprintAst(ctx *blockCtx, fset *token.FileSet, x ast.Node) string {
var buf bytes.Buffer
err := printer.Fprint(&buf, fset, x)
if err != nil {
panic("Unexpected error: " + err.Error())
ctx.handleErrorf(x.Pos(), "unexpected error: %v", err)

Check warning on line 1192 in cl/expr.go

View check run for this annotation

Codecov / codecov/patch

cl/expr.go#L1192

Added line #L1192 was not covered by tests
}

return buf.String()
}

Expand Down
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
3 changes: 1 addition & 2 deletions cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"go/constant"
"log"
"path/filepath"
"reflect"

goast "go/ast"
gotoken "go/token"
Expand Down Expand Up @@ -158,7 +157,7 @@
case *ast.EmptyStmt:
// do nothing
default:
log.Panicln("TODO - compileStmt failed: unknown -", reflect.TypeOf(v))
ctx.handleErrorf(v.Pos(), "compileStmt unexpected: %T", v)

Check warning on line 160 in cl/stmt.go

View check run for this annotation

Codecov / codecov/patch

cl/stmt.go#L160

Added line #L160 was not covered by tests
}
ctx.cb.EndStmt()
}
Expand Down