From e9f242a4409e947b04c83d6523c78c944eb90667 Mon Sep 17 00:00:00 2001 From: hthieu1110 Date: Tue, 10 Dec 2024 02:36:38 -0800 Subject: [PATCH] fix(gnovm): do not allow nil as type declaration (#3309) Fix https://github.com/gnolang/gno/issues/3307 --------- Co-authored-by: hieu.ha --- gnovm/pkg/gnolang/preprocess.go | 6 ++++++ gnovm/tests/files/type40.gno | 2 +- gnovm/tests/files/type41.gno | 9 +++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 gnovm/tests/files/type41.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 6e749053d72..a3e498710bb 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -4283,6 +4283,12 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) { if isBlankIdentifier(tx) { panic("cannot use _ as value or type") } + + // do not allow nil as type. + if tx.Name == "nil" { + panic("nil is not a type") + } + if tv := last.GetValueRef(store, tx.Name, true); tv != nil { t = tv.GetType() if dt, ok := t.(*DeclaredType); ok { diff --git a/gnovm/tests/files/type40.gno b/gnovm/tests/files/type40.gno index 65210798007..fe312e220e0 100644 --- a/gnovm/tests/files/type40.gno +++ b/gnovm/tests/files/type40.gno @@ -43,4 +43,4 @@ func main() { // 5 // 6 // 7 -// yo \ No newline at end of file +// yo diff --git a/gnovm/tests/files/type41.gno b/gnovm/tests/files/type41.gno new file mode 100644 index 00000000000..ea1a3b1df24 --- /dev/null +++ b/gnovm/tests/files/type41.gno @@ -0,0 +1,9 @@ +package main + +type A nil + +func main() { +} + +// Error: +// main/files/type41.gno:3:6: nil is not a type