Skip to content

Commit

Permalink
fix(gnolang): make Go2Gno return a prespective error instead of sudde…
Browse files Browse the repository at this point in the history
…n/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 <[email protected]>
  • Loading branch information
odeke-em and thehowl authored Feb 13, 2025
1 parent 70b8a70 commit 041f56d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion gnovm/pkg/gnolang/go2gno.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions gnovm/tests/files/parse_err0.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://github.com/gnolang/gno/issues/3727

package main

func () A()

func main() {}

// Error:
// method has no receiver

0 comments on commit 041f56d

Please sign in to comment.