Skip to content

Commit b915914

Browse files
mvertesjaekwon
authored andcommitted
Rewrite AST prior go type checking to avoid inter-realm statements.
1 parent fd67023 commit b915914

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

gnovm/pkg/gnolang/gotypecheck.go

+27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/gnolang/gno/gnovm"
1515
"go.uber.org/multierr"
16+
"golang.org/x/tools/go/ast/astutil"
1617
)
1718

1819
// type checking (using go/types)
@@ -140,6 +141,8 @@ func (g *gnoImporter) parseCheckMemPackage(mpkg *gnovm.MemPackage, fmt bool) (*t
140141
deleteOldIdents(delFunc, f)
141142
}
142143

144+
filterWithSwitchRealm(f)
145+
143146
// enforce formatting
144147
if fmt {
145148
var buf bytes.Buffer
@@ -179,3 +182,27 @@ func deleteOldIdents(idents map[string]func(), f *ast.File) {
179182
}
180183
}
181184
}
185+
186+
func filterWithSwitchRealm(f *ast.File) {
187+
astutil.Apply(f, nil, func(c *astutil.Cursor) bool {
188+
switch n := c.Node().(type) {
189+
case *ast.ExprStmt:
190+
if ce, ok := n.X.(*ast.CallExpr); ok {
191+
if id, ok := ce.Fun.(*ast.Ident); ok && id.Name == "switchrealm" {
192+
// Delete statement 'switchrealm()'.
193+
c.Delete()
194+
}
195+
}
196+
case *ast.CallExpr:
197+
if id, ok := n.Fun.(*ast.Ident); ok && id.Name == "withswitch" {
198+
// Replace expression 'withswitch(x)' by 'x'.
199+
var a ast.Node
200+
if len(n.Args) > 0 {
201+
a = n.Args[0]
202+
}
203+
c.Replace(a)
204+
}
205+
}
206+
return true
207+
})
208+
}

gnovm/pkg/test/filetest.go

+6
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ func (opts *TestOptions) runTest(m *gno.Machine, pkgPath, filename string, conte
262262
}
263263
orig, tx := m.Store, m.Store.BeginTransaction(nil, nil, nil)
264264
m.Store = tx
265+
266+
// Validate Gno syntax and type check.
267+
if err := gno.TypeCheckMemPackageTest(memPkg, m.Store); err != nil {
268+
return runResult{Error: err.Error()}
269+
}
270+
265271
// Run decls and init functions.
266272
m.RunMemPackage(memPkg, true)
267273
// Clear store cache and reconstruct machine from committed info

gnovm/tests/files/extern/timtadh/data_structures/types/string.gno

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func (self ByteSlice) Hash() int {
7070

7171
func hash(s []byte) int {
7272
res := sha256.Sum256(s)
73-
return int(s[0]) |
74-
int(s[1]<<8) |
75-
int(s[2]<<16) |
76-
int(s[3]<<24)
73+
return int(res[0]) |
74+
int(res[1]<<8) |
75+
int(res[2]<<16) |
76+
int(res[3]<<24)
7777
}

0 commit comments

Comments
 (0)