From 041f56d6bdfa4a4deeb830f7344aa9ba6ec9598e Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Thu, 13 Feb 2025 19:03:19 +0200 Subject: [PATCH] fix(gnolang): make Go2Gno return a prespective error instead of sudden/elusive runtime panic with a bad receiver (#3733) The pattern: ```go func() A() ``` confuses Go into expecting a receiver and it returns a compile time error "missing receiver", but previously Gno panicked with a runtime error due to a deference yet the Recv.List was empty. This change fixes that by detecting that condition and prescriptively panicking which can then be relayed reasonably as expecting to the calling user. Fixes #3727 --------- Co-authored-by: Morgan Bazalgette --- gnovm/pkg/gnolang/go2gno.go | 5 ++++- gnovm/tests/files/parse_err0.gno | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 gnovm/tests/files/parse_err0.gno diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 34686dc4cc1..1ef23a0079e 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -433,7 +433,10 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { recv := FieldTypeExpr{} if isMethod { if len(gon.Recv.List) > 1 { - panic("*ast.FuncDecl cannot have multiple receivers") + panic("method has multiple receivers") + } + if len(gon.Recv.List) == 0 { + panic("method has no receiver") } recv = *Go2Gno(fs, gon.Recv.List[0]).(*FieldTypeExpr) } diff --git a/gnovm/tests/files/parse_err0.gno b/gnovm/tests/files/parse_err0.gno new file mode 100644 index 00000000000..88a8e0df132 --- /dev/null +++ b/gnovm/tests/files/parse_err0.gno @@ -0,0 +1,10 @@ +// https://github.com/gnolang/gno/issues/3727 + +package main + +func () A() + +func main() {} + +// Error: +// method has no receiver