Skip to content

Commit

Permalink
add a failing test; fix, don't mark as heap defined until label
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Jul 1, 2024
1 parent d552d74 commit 30386a8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
6 changes: 6 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,12 @@ func findGotoLoopDefines(ctx BlockNode, bn BlockNode) {
// NOTE: ATTR_GOTOLOOP_STMT is not used.
Transcribe(bn,
func(ns []Node, ftype TransField, index int, n Node, stage TransStage) (Node, TransCtrl) {

Check failure on line 2294 in gnovm/pkg/gnolang/preprocess.go

View workflow job for this annotation

GitHub Actions / Run Main / Go Linter / lint

unnecessary leading newline (whitespace)

Check failure on line 2295 in gnovm/pkg/gnolang/preprocess.go

View workflow job for this annotation

GitHub Actions / Run Main / Go Linter / lint

File is not `gofumpt`-ed (gofumpt)
// nothing to do until label line.
if n.GetLine() < labelLine {
return n, TRANS_CONTINUE
}

switch stage {
case TRANS_ENTER:
// NOTE: called redundantly
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Store interface {
GetTypeSafe(tid TypeID) Type
SetCacheType(Type)
SetType(Type)
GetBlockNode(Location) BlockNode
GetBlockNode(Location) BlockNode // to get a PackageNode, use PackageNodeLocation().
GetBlockNodeSafe(Location) BlockNode
SetBlockNode(BlockNode)
// UNSTABLE
Expand Down
25 changes: 23 additions & 2 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func RunFileTest(rootDir string, path string, opts ...RunFileTestOption) error {
opt(&f)
}

directives, pkgPath, resWanted, errWanted, rops, maxAlloc, send := wantedFromComment(path)
directives, pkgPath, resWanted, errWanted, rops, maxAlloc, send, preWanted := wantedFromComment(path)
if pkgPath == "" {
pkgPath = "main"
}
Expand Down Expand Up @@ -373,6 +373,23 @@ func RunFileTest(rootDir string, path string, opts ...RunFileTestOption) error {
}
}
}
case "Preprocessed":
// check preprocessed AST.
pn := store.GetBlockNode(gno.PackageNodeLocation(pkgPath))
pns := pn.(*gno.PackageNode).FileSet.Files[0].String()
if preWanted != resWanted {
// panic so tests immediately fail (for now).
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(preWanted),
B: difflib.SplitLines(pns),
FromFile: "Expected",
FromDate: "",
ToFile: "Actual",
ToDate: "",
Context: 1,
})
panic(fmt.Sprintf("fail on %s: diff:\n%s\n", path, diff))
}
default:
return nil
}
Expand All @@ -390,7 +407,7 @@ func RunFileTest(rootDir string, path string, opts ...RunFileTestOption) error {
return nil
}

func wantedFromComment(p string) (directives []string, pkgPath, res, err, rops string, maxAlloc int64, send std.Coins) {
func wantedFromComment(p string) (directives []string, pkgPath, res, err, rops string, maxAlloc int64, send std.Coins, pre string) {
fset := token.NewFileSet()
f, err2 := parser.ParseFile(fset, p, nil, parser.ParseComments)
if err2 != nil {
Expand Down Expand Up @@ -432,6 +449,10 @@ func wantedFromComment(p string) (directives []string, pkgPath, res, err, rops s
rops = strings.TrimPrefix(text, "Realm:\n")
rops = strings.TrimSpace(rops)
directives = append(directives, "Realm")
} else if strings.HasPrefix(text, "Preprocessed:\n") {
pre = strings.TrimPrefix(text, "Preprocessed:\n")
pre = strings.TrimSpace(pre)
directives = append(directives, "Preprocessed")
} else {
// ignore unexpected.
}
Expand Down
18 changes: 18 additions & 0 deletions gnovm/tests/files/gotoloop1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

func main() {
c := 0
loop:
i := 1
println(i)
c += 1
if c < 10 {
goto loop
}
}

// This does not make 'i' NameExprTypeHeapDefine,
// because it is not actually used in a &ref or closure context.

// Preprocessed:
// file{ package main; func main() { c<VPBlock(1,0)> := (const (0 int)); i<VPBlock(1,1)> := (const (1 int)); (const (println func(xs ...interface{})()))(i<VPBlock(1,1)>); c<VPBlock(1,0)> += (const (1 int)); if c<VPBlock(2,0)> < (const (10 int)) { goto loop<1,1> } } }

0 comments on commit 30386a8

Please sign in to comment.