Skip to content

Commit 041f56d

Browse files
odeke-emthehowl
andauthored
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 <[email protected]>
1 parent 70b8a70 commit 041f56d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

gnovm/pkg/gnolang/go2gno.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,10 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) {
433433
recv := FieldTypeExpr{}
434434
if isMethod {
435435
if len(gon.Recv.List) > 1 {
436-
panic("*ast.FuncDecl cannot have multiple receivers")
436+
panic("method has multiple receivers")
437+
}
438+
if len(gon.Recv.List) == 0 {
439+
panic("method has no receiver")
437440
}
438441
recv = *Go2Gno(fs, gon.Recv.List[0]).(*FieldTypeExpr)
439442
}

gnovm/tests/files/parse_err0.gno

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// https://github.com/gnolang/gno/issues/3727
2+
3+
package main
4+
5+
func () A()
6+
7+
func main() {}
8+
9+
// Error:
10+
// method has no receiver

0 commit comments

Comments
 (0)