Skip to content

Commit

Permalink
Merge pull request #1832 from visualfc/cl_error
Browse files Browse the repository at this point in the history
cl: compileExpr/compileExprLHS panic code error
  • Loading branch information
xushiwei authored Apr 1, 2024
2 parents 326b271 + d1e3230 commit 9875bae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
9 changes: 6 additions & 3 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ func TestCompileLambda2(t *testing.T) {

func TestCompileExpr(t *testing.T) {
defer func() {
if e := recover(); e != "compileExpr failed: unknown - *ast.Ellipsis\n" {
t.Fatal("compileExpr:", e)
if e := recover(); e != nil {
if ce := e.(*gogen.CodeError); ce.Msg != "compileExpr failed: unknown - *ast.Ellipsis" {
t.Fatal("compileExpr:", ce.Msg)
}
}
}()
compileExpr(nil, &ast.Ellipsis{})
ctx := &blockCtx{pkgCtx: &pkgCtx{}}
compileExpr(ctx, &ast.Ellipsis{})
}

func TestCompileStmt(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions cl/error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,3 +1049,17 @@ type
a := 1
`)
}

func TestCompileExprError(t *testing.T) {
codeErrorTestAst(t, "main", "bar.go", `bar.go:5:1: compileExpr failed: unknown - *ast.BadExpr`, `
func Foo(){}
func _() {
Foo(
}
`)
codeErrorTestAst(t, "main", "bar.go", `bar.go:3:2: compileExprLHS failed: unknown - *ast.StructType`, `
func _() {
struct() = nil
}
`)
}
5 changes: 2 additions & 3 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"go/types"
"log"
"math/big"
"reflect"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -249,7 +248,7 @@ func compileExprLHS(ctx *blockCtx, expr ast.Expr) {
case *ast.StarExpr:
compileStarExprLHS(ctx, v)
default:
log.Panicln("compileExpr failed: unknown -", reflect.TypeOf(v))
panic(ctx.newCodeErrorf(v.Pos(), "compileExprLHS failed: unknown - %T", expr))
}
if rec := ctx.recorder(); rec != nil {
rec.recordExpr(ctx, expr, true)
Expand Down Expand Up @@ -365,7 +364,7 @@ func compileExpr(ctx *blockCtx, expr ast.Expr, inFlags ...int) {
case *ast.EnvExpr:
compileEnvExpr(ctx, v)
default:
log.Panicf("compileExpr failed: unknown - %T\n", v)
panic(ctx.newCodeErrorf(v.Pos(), "compileExpr failed: unknown - %T", v))
}
if rec := ctx.recorder(); rec != nil {
rec.recordExpr(ctx, expr, false)
Expand Down

0 comments on commit 9875bae

Please sign in to comment.