Skip to content

Commit

Permalink
fix(gnovm): add const type is basic check
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelVallenet committed Aug 1, 2024
1 parent 14b9015 commit 2933d8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions gnovm/pkg/gnolang/type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 2933d8a

Please sign in to comment.