Skip to content

Commit

Permalink
cl: toType return types.Invalid if error
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Jan 12, 2024
1 parent 4ff40dc commit 3e58499
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
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 @@ func compileExprLHS(ctx *blockCtx, expr ast.Expr) {
case *ast.StarExpr:
compileStarExprLHS(ctx, v)
default:
log.Panicln("compileExpr failed: unknown -", reflect.TypeOf(v))
ctx.handleErrorf(v.Pos(), "compileExprLHS unexpected: %T", v)
}
if rec := ctx.recorder(); rec != nil {
rec.recordExpr(ctx, expr, true)
Expand Down Expand Up @@ -275,12 +275,8 @@ func compileExpr(ctx *blockCtx, expr ast.Expr, inFlags ...int) {
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)
}
if rec := ctx.recorder(); rec != nil {
rec.recordExpr(ctx, expr, false)
Expand Down Expand Up @@ -1167,7 +1163,7 @@ func compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr, inFlags int) {
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 compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr, inFlags int) {
}
}

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)
}

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 @@ import (
"go/constant"
"log"
"path/filepath"
"reflect"

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

0 comments on commit 3e58499

Please sign in to comment.