From a67d677caa6052c3cf13d37b88adf98f50885fcb Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Mon, 4 Nov 2024 23:40:52 +0100 Subject: [PATCH] fix(gnovm): improve error message for nil assignment in variable declaration --- gnovm/pkg/gnolang/type_check.go | 2 +- gnovm/tests/files/assign29.gno | 10 ++++++++++ gnovm/tests/files/var31.gno | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 gnovm/tests/files/assign29.gno create mode 100644 gnovm/tests/files/var31.gno diff --git a/gnovm/pkg/gnolang/type_check.go b/gnovm/pkg/gnolang/type_check.go index f86c44e7921..8b1d1eb1e3d 100644 --- a/gnovm/pkg/gnolang/type_check.go +++ b/gnovm/pkg/gnolang/type_check.go @@ -289,7 +289,7 @@ func checkAssignableTo(xt, dt Type, autoNative bool) error { // case0 if xt == nil { // see test/files/types/eql_0f18 if !maybeNil(dt) { - panic(fmt.Sprintf("invalid operation, nil can not be compared to %v", dt)) + panic(fmt.Sprintf("cannot use nil as %v value in variable declaration", dt)) } return nil } else if dt == nil { // _ = xxx, assign8.gno, 0f31. else cases? diff --git a/gnovm/tests/files/assign29.gno b/gnovm/tests/files/assign29.gno new file mode 100644 index 00000000000..1856e85e49f --- /dev/null +++ b/gnovm/tests/files/assign29.gno @@ -0,0 +1,10 @@ +package main + +func main() { + a := 1 + a = nil + println(a) +} + +// Error: +// main/files/assign29.gno:5:2: cannot use nil as int value in variable declaration diff --git a/gnovm/tests/files/var31.gno b/gnovm/tests/files/var31.gno new file mode 100644 index 00000000000..d5571f148ca --- /dev/null +++ b/gnovm/tests/files/var31.gno @@ -0,0 +1,8 @@ +package main + +func main() { + var i int = nil +} + +// Error: +// main/files/var31.gno:4:6: cannot use nil as int value in variable declaration