From 2933d8a7b4062ec18e004971fcdbdc88a504abdd Mon Sep 17 00:00:00 2001 From: MikaelVallenet Date: Thu, 1 Aug 2024 17:37:59 +0700 Subject: [PATCH] fix(gnovm): add const type is basic check --- gnovm/pkg/gnolang/preprocess.go | 3 +++ gnovm/pkg/gnolang/type_check.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 5a710723b86..350853de495 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -461,6 +461,9 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { d := n.(Decl) if cd, ok := d.(*ValueDecl); ok { checkValDefineMismatch(cd) + if cd.Const { + checkValConstType(cd) + } } // recursively predefine dependencies. d2, ppd := predefineNow(store, last, d) diff --git a/gnovm/pkg/gnolang/type_check.go b/gnovm/pkg/gnolang/type_check.go index 31025fef152..6aa736d3e5e 100644 --- a/gnovm/pkg/gnolang/type_check.go +++ b/gnovm/pkg/gnolang/type_check.go @@ -215,6 +215,19 @@ func assertAssignableTo(xt, dt Type, autoNative bool) { } } +// TODO: check operands unary & binary expr are const +func checkValConstType(d *ValueDecl) { + for _, vx := range d.Values { + switch vx.(type) { + case *BasicLitExpr, *BinaryExpr, *UnaryExpr: + // Valid constant expression + break + default: + panic("const type should be a basic type") + } + } +} + // checkValDefineMismatch checks for mismatch between the number of variables and values in a ValueDecl or AssignStmt. func checkValDefineMismatch(n Node) { var (