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

feat(gnolang): make panics from Go2Gno produce lineno:column information #3748

Merged
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
30 changes: 19 additions & 11 deletions gnovm/pkg/gnolang/go2gno.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@
}
}()
}

panicWithPos := func(fmtStr string, args ...any) {
pos := fs.Position(gon.Pos())
loc := fmt.Sprintf("%s:%d:%d", pos.Filename, pos.Line, pos.Column)
panic(fmt.Errorf("%s: %v", loc, fmt.Sprintf(fmtStr, args...)))
}

switch gon := gon.(type) {
case *ast.ParenExpr:
return toExpr(fs, gon.X)
Expand Down Expand Up @@ -245,10 +252,10 @@
Tag: toExpr(fs, gon.Tag),
}
} else {
panic(fmt.Sprintf(
panicWithPos(

Check warning on line 255 in gnovm/pkg/gnolang/go2gno.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/go2gno.go#L255

Added line #L255 was not covered by tests
"expected a Go Field with 1 name but got %v.\n"+
"maybe call toFields",
gon.Names))
gon.Names)

Check warning on line 258 in gnovm/pkg/gnolang/go2gno.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/go2gno.go#L258

Added line #L258 was not covered by tests
}
case *ast.ArrayType:
if _, ok := gon.Len.(*ast.Ellipsis); ok {
Expand Down Expand Up @@ -330,7 +337,7 @@
if cx, ok := gon.X.(*ast.CallExpr); ok {
if ix, ok := cx.Fun.(*ast.Ident); ok && ix.Name == "panic" {
if len(cx.Args) != 1 {
panic("expected panic statement to have single exception value")
panicWithPos("expected panic statement to have single exception value")

Check warning on line 340 in gnovm/pkg/gnolang/go2gno.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/go2gno.go#L340

Added line #L340 was not covered by tests
}
return &PanicStmt{
Exception: toExpr(fs, cx.Args[0]),
Expand Down Expand Up @@ -412,9 +419,8 @@
VarName: "",
}
default:
panic(fmt.Sprintf(
"unexpected *ast.TypeSwitchStmt.Assign type %s",
reflect.TypeOf(gon.Assign).String()))
panicWithPos("unexpected *ast.TypeSwitchStmt.Assign type %s",
reflect.TypeOf(gon.Assign).String())

Check warning on line 423 in gnovm/pkg/gnolang/go2gno.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/go2gno.go#L422-L423

Added lines #L422 - L423 were not covered by tests
}
case *ast.SwitchStmt:
x := toExpr(fs, gon.Tag)
Expand All @@ -433,10 +439,10 @@
recv := FieldTypeExpr{}
if isMethod {
if len(gon.Recv.List) > 1 {
panic("method has multiple receivers")
panicWithPos("method has multiple receivers")

Check warning on line 442 in gnovm/pkg/gnolang/go2gno.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/go2gno.go#L442

Added line #L442 was not covered by tests
}
if len(gon.Recv.List) == 0 {
panic("method has no receiver")
panicWithPos("method has no receiver")
}
recv = *Go2Gno(fs, gon.Recv.List[0]).(*FieldTypeExpr)
}
Expand All @@ -454,7 +460,7 @@
Body: body,
}
case *ast.GenDecl:
panic("unexpected *ast.GenDecl; use toDecls(fs,) instead")
panicWithPos("unexpected *ast.GenDecl; use toDecls(fs,) instead")

Check warning on line 463 in gnovm/pkg/gnolang/go2gno.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/go2gno.go#L463

Added line #L463 was not covered by tests
case *ast.File:
pkgName := Name(gon.Name.Name)
decls := make([]Decl, 0, len(gon.Decls))
Expand All @@ -473,11 +479,13 @@
case *ast.EmptyStmt:
return &EmptyStmt{}
default:
panic(fmt.Sprintf("unknown Go type %v: %s\n",
panicWithPos("unknown Go type %v: %s\n",

Check warning on line 482 in gnovm/pkg/gnolang/go2gno.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/go2gno.go#L482

Added line #L482 was not covered by tests
reflect.TypeOf(gon),
spew.Sdump(gon),
))
)

Check warning on line 485 in gnovm/pkg/gnolang/go2gno.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/go2gno.go#L485

Added line #L485 was not covered by tests
}

return

Check warning on line 488 in gnovm/pkg/gnolang/go2gno.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/go2gno.go#L488

Added line #L488 was not covered by tests
}

//----------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/parse_err0.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ func () A()
func main() {}

// Error:
// method has no receiver
// files/parse_err0.gno:5:1: method has no receiver
Loading