From f90d82ac8f1d0e487d4f2ba82e0ecd8c215978d5 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Wed, 12 Feb 2025 01:52:23 +0200 Subject: [PATCH 1/3] fix(gnolang): make Go2Gno return a prespective error instead of sudden/elusive runtime panic with a bad receiver 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 --- gnovm/pkg/gnolang/go2gno.go | 3 +++ gnovm/pkg/gnolang/go2gno_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 34686dc4cc1..43bf4065f09 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -435,6 +435,9 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { if len(gon.Recv.List) > 1 { panic("*ast.FuncDecl cannot have multiple receivers") } + if len(gon.Recv.List) == 0 { + panic("*ast.FuncDecl has missing receiver") + } recv = *Go2Gno(fs, gon.Recv.List[0]).(*FieldTypeExpr) } name := toName(gon.Name) diff --git a/gnovm/pkg/gnolang/go2gno_test.go b/gnovm/pkg/gnolang/go2gno_test.go index 920c5acd9c8..be98823af18 100644 --- a/gnovm/pkg/gnolang/go2gno_test.go +++ b/gnovm/pkg/gnolang/go2gno_test.go @@ -25,3 +25,15 @@ func main(){ fmt.Printf("AST:\n%#v\n\n", n) fmt.Printf("AST.String():\n%s\n", n.String()) } + +// Issue https://github.com/gnolang/gno/issues/3727 +func TestParseFile_wonkyFunctionDeclarationConfusesReceiver(t *testing.T) { + t.Parallel() + + gocode := `package main +func() A() +func main() {}` + _, err := ParseFile("main.go", gocode) + assert.Error(t, err) + assert.Contains(t, err.Error(), "missing receiver") +} From 973dc1ef83a7b912727cc91efd8b8e764f315159 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 13 Feb 2025 17:28:32 +0100 Subject: [PATCH 2/3] match go error --- gnovm/pkg/gnolang/go2gno.go | 4 ++-- gnovm/tests/files/parse_err0.gno | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 gnovm/tests/files/parse_err0.gno diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 43bf4065f09..1ef23a0079e 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -433,10 +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("*ast.FuncDecl has missing receiver") + 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..1c241716fce --- /dev/null +++ b/gnovm/tests/files/parse_err0.gno @@ -0,0 +1,8 @@ +package main + +func () A() + +func main() {} + +// Error: +// method has no receiver From 1f684f18f3a5116dc363bb005c19df225d53d2a7 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 13 Feb 2025 17:29:07 +0100 Subject: [PATCH 3/3] remove test --- gnovm/pkg/gnolang/go2gno_test.go | 12 ------------ gnovm/tests/files/parse_err0.gno | 2 ++ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/gnovm/pkg/gnolang/go2gno_test.go b/gnovm/pkg/gnolang/go2gno_test.go index be98823af18..920c5acd9c8 100644 --- a/gnovm/pkg/gnolang/go2gno_test.go +++ b/gnovm/pkg/gnolang/go2gno_test.go @@ -25,15 +25,3 @@ func main(){ fmt.Printf("AST:\n%#v\n\n", n) fmt.Printf("AST.String():\n%s\n", n.String()) } - -// Issue https://github.com/gnolang/gno/issues/3727 -func TestParseFile_wonkyFunctionDeclarationConfusesReceiver(t *testing.T) { - t.Parallel() - - gocode := `package main -func() A() -func main() {}` - _, err := ParseFile("main.go", gocode) - assert.Error(t, err) - assert.Contains(t, err.Error(), "missing receiver") -} diff --git a/gnovm/tests/files/parse_err0.gno b/gnovm/tests/files/parse_err0.gno index 1c241716fce..88a8e0df132 100644 --- a/gnovm/tests/files/parse_err0.gno +++ b/gnovm/tests/files/parse_err0.gno @@ -1,3 +1,5 @@ +// https://github.com/gnolang/gno/issues/3727 + package main func () A()