Skip to content

Commit

Permalink
refactore(gnovm): some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy committed Dec 26, 2024
1 parent c45f63e commit 7083830
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
42 changes: 28 additions & 14 deletions gnovm/pkg/gnolang/type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,28 +248,32 @@ Main:
switch currExpr := currExpr.(type) {
case *NameExpr:
t := evalStaticTypeOf(store, last, currExpr)
_, okArray := t.(*ArrayType)
_, okCallExpr := parentExpr.(*CallExpr)

if _, ok := t.(*TypeType); ok {
t = evalStaticType(store, last, currExpr)
}
// special case for len, cap
if okArray && okCallExpr {
if isParentCallExprWithArrayArg(t, parentExpr) {
break Main
}
panic(fmt.Sprintf("%s (variable of type %s) is not constant", currExpr.Name, t))
case *TypeAssertExpr:
ty := evalStaticTypeOf(store, last, currExpr)
_, okCallExpr := parentExpr.(*CallExpr)
_, okArray := ty.(*ArrayType)
if okCallExpr && okArray {
if _, ok := ty.(*TypeType); ok {
ty = evalStaticType(store, last, currExpr)
}

Check warning on line 263 in gnovm/pkg/gnolang/type_check.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/type_check.go#L262-L263

Added lines #L262 - L263 were not covered by tests
// special case for len, cap
if isParentCallExprWithArrayArg(ty, parentExpr) {
break Main

Check warning on line 266 in gnovm/pkg/gnolang/type_check.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/type_check.go#L266

Added line #L266 was not covered by tests
}
panic(fmt.Sprintf("%s (comma, ok expression of type %s) is not constant", currExpr.String(), currExpr.Type))
case *IndexExpr:
ty := evalStaticTypeOf(store, last, currExpr)
_, okCallExpr := parentExpr.(*CallExpr)
_, okArray := ty.(*ArrayType)
if okCallExpr && okArray {
// TODO: should add a test after the fix of https://github.com/gnolang/gno/issues/3409
if _, ok := ty.(*TypeType); ok {
ty = evalStaticType(store, last, currExpr)
}

Check warning on line 273 in gnovm/pkg/gnolang/type_check.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/type_check.go#L272-L273

Added lines #L272 - L273 were not covered by tests
// TODO: should add a test after the fix of https://github.com/gnolang/gno/issues/3409
// special case for len, cap
if isParentCallExprWithArrayArg(ty, parentExpr) {
break Main
}

Expand Down Expand Up @@ -350,11 +354,12 @@ Main:
panic(fmt.Sprintf("%s (variable of type %s) is not constant", currExpr.String(), tt))

Check warning on line 354 in gnovm/pkg/gnolang/type_check.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/type_check.go#L353-L354

Added lines #L353 - L354 were not covered by tests
case *PointerType, *DeclaredType, *StructType, *InterfaceType, *TypeType, *NativeType:
ty := evalStaticTypeOf(store, last, currExpr)
_, okCallExpr := parentExpr.(*CallExpr)
_, okArray := ty.(*ArrayType)
if _, ok := ty.(*TypeType); ok {
ty = evalStaticType(store, last, currExpr)
}

Check warning on line 359 in gnovm/pkg/gnolang/type_check.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/type_check.go#L358-L359

Added lines #L358 - L359 were not covered by tests

// special case for len, cap
if okCallExpr && okArray {
if isParentCallExprWithArrayArg(ty, parentExpr) {
break Main
}
panic(fmt.Sprintf("%s (variable of type %s) is not constant", currExpr.String(), ty))
Expand All @@ -371,6 +376,8 @@ Main:
}
case *ConstExpr:
case *BasicLitExpr:

Check warning on line 378 in gnovm/pkg/gnolang/type_check.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/type_check.go#L378

Added line #L378 was not covered by tests
case *CompositeLitExpr:
assertValidConstValue(store, last, currExpr.Type, parentExpr)
default:
ift := evalStaticTypeOf(store, last, currExpr)
if _, ok := ift.(*TypeType); ok {
Expand All @@ -380,6 +387,13 @@ Main:
}
}

func isParentCallExprWithArrayArg(currType Type, parentExpr Expr) bool {
_, okArray := baseOf(currType).(*ArrayType)
_, okCallExpr := parentExpr.(*CallExpr)

return okArray && okCallExpr
}

// checkValDefineMismatch checks for mismatch between the number of variables and values in a ValueDecl or AssignStmt.
func checkValDefineMismatch(n Node) {
var (
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/const32.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ func main() {
}

// Error:
// main/files/const32.gno:6:8: [](const-type string){} (variable of type []string) is not constant
// main/files/const32.gno:6:8: [](const-type string) (variable of type []string) is not constant
2 changes: 1 addition & 1 deletion gnovm/tests/files/const48.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ func main() {
}

// Error:
// main/files/const48.gno:4:8: map[(const-type string)] [](const-type int){(const ("arr" string)): (const-type []int){(const (1 int)), (const (2 int))}} (variable of type map[string][]int) is not constant
// main/files/const48.gno:4:8: map[(const-type string)] [](const-type int) (variable of type map[string][]int) is not constant
16 changes: 16 additions & 0 deletions gnovm/tests/files/const49.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "io"

type (
s [2]int
m s
)

func main() {
const v = len(m{1, 2})
println(v)
}

// Output:
// 2

0 comments on commit 7083830

Please sign in to comment.