From 0fa39be1ddb7587f21ce0a3d25640b68ced47400 Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Tue, 28 May 2024 00:36:11 +0200 Subject: [PATCH 1/2] fix: set Loc Line on Decls --- gnovm/cmd/gno/testdata/gno_test/lint_bad_import.txtar | 2 +- .../gno/testdata/gno_test/test_with-native-fallback.txtar | 2 +- gnovm/cmd/gno/testdata/gno_test/unknow_lib.txtar | 4 ++-- gnovm/pkg/gnolang/debugger_test.go | 8 ++++---- gnovm/pkg/gnolang/go2gno.go | 7 +++++++ gnovm/tests/files/const9.gno | 2 +- gnovm/tests/files/import6.gno | 2 +- gnovm/tests/files/var18.gno | 8 ++++++++ 8 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 gnovm/tests/files/var18.gno diff --git a/gnovm/cmd/gno/testdata/gno_test/lint_bad_import.txtar b/gnovm/cmd/gno/testdata/gno_test/lint_bad_import.txtar index 946e1bcba35..fc4039d38c6 100644 --- a/gnovm/cmd/gno/testdata/gno_test/lint_bad_import.txtar +++ b/gnovm/cmd/gno/testdata/gno_test/lint_bad_import.txtar @@ -16,4 +16,4 @@ func main() { -- stdout.golden -- -- stderr.golden -- -bad_file.gno:1: unknown import path python (code=2). +bad_file.gno:3: unknown import path python (code=2). diff --git a/gnovm/cmd/gno/testdata/gno_test/test_with-native-fallback.txtar b/gnovm/cmd/gno/testdata/gno_test/test_with-native-fallback.txtar index 42dae650a7e..5b807442abc 100644 --- a/gnovm/cmd/gno/testdata/gno_test/test_with-native-fallback.txtar +++ b/gnovm/cmd/gno/testdata/gno_test/test_with-native-fallback.txtar @@ -4,7 +4,7 @@ ! stdout .+ stderr 'panic: unknown import path net \[recovered\]' -stderr ' panic: gno.land/r/\w{8}/contract.gno:1: unknown import path net' +stderr ' panic: gno.land/r/\w{8}/contract.gno:3: unknown import path net' gno test -v --with-native-fallback . diff --git a/gnovm/cmd/gno/testdata/gno_test/unknow_lib.txtar b/gnovm/cmd/gno/testdata/gno_test/unknow_lib.txtar index 33fc85fd35f..3df7ab254e4 100644 --- a/gnovm/cmd/gno/testdata/gno_test/unknow_lib.txtar +++ b/gnovm/cmd/gno/testdata/gno_test/unknow_lib.txtar @@ -4,13 +4,13 @@ ! stdout .+ stderr 'panic: unknown import path foobarbaz \[recovered\]' -stderr ' panic: gno.land/r/\w{8}/contract.gno:1: unknown import path foobarbaz' +stderr ' panic: gno.land/r/\w{8}/contract.gno:3: unknown import path foobarbaz' ! gno test -v --with-native-fallback . ! stdout .+ stderr 'panic: unknown import path foobarbaz \[recovered\]' -stderr ' panic: gno.land/r/\w{8}/contract.gno:1: unknown import path foobarbaz' +stderr ' panic: gno.land/r/\w{8}/contract.gno:3: unknown import path foobarbaz' -- contract.gno -- package contract diff --git a/gnovm/pkg/gnolang/debugger_test.go b/gnovm/pkg/gnolang/debugger_test.go index ca50e243a5c..db1bb14c138 100644 --- a/gnovm/pkg/gnolang/debugger_test.go +++ b/gnovm/pkg/gnolang/debugger_test.go @@ -101,14 +101,14 @@ func TestDebug(t *testing.T) { {in: "p 'a'\n", out: "(97 int32)"}, {in: "p '界'\n", out: "(30028 int32)"}, {in: "p \"xxxx\"\n", out: `("xxxx" string)`}, - {in: "si\n", out: "sample.gno:4"}, - {in: "s\ns\n", out: "=> 33: num := 5"}, - {in: "s\n\n", out: "=> 33: num := 5"}, + {in: "si\n", out: "sample.gno:14"}, + {in: "s\ns\n", out: `=> 14: var global = "test"`}, + {in: "s\n\n", out: "=> 16: type T struct {"}, {in: "foo", out: "command not available: foo"}, {in: "\n\n", out: "dbg> "}, {in: "#\n", out: "dbg> "}, {in: "p foo", out: "Command failed: could not find symbol value for foo"}, - {in: "b +7\nc\n", out: "=> 11:"}, + {in: "b +7\nc\n", out: "=> 21: r := t.A[i]"}, {in: brk + "clear 0\n", out: "dbg> "}, {in: brk + "clear -1\n", out: "Command failed: invalid breakpoint id: -1"}, {in: brk + "clear\n", out: "dbg> "}, diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 5c74621c973..c8f8ee46ef7 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -802,6 +802,13 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { reflect.TypeOf(s))) } } + + if fs != nil { + for _, d := range ds { + setLoc(fs, gd.Pos(), d) + } + } + return ds } diff --git a/gnovm/tests/files/const9.gno b/gnovm/tests/files/const9.gno index 21bb7dbc753..a68b548d0c4 100644 --- a/gnovm/tests/files/const9.gno +++ b/gnovm/tests/files/const9.gno @@ -14,4 +14,4 @@ func main() { } // Error: -// main/files/const9.gno:1: constant definition loop with b +// main/files/const9.gno:3: constant definition loop with b diff --git a/gnovm/tests/files/import6.gno b/gnovm/tests/files/import6.gno index da5dbfbd3b2..831e892ad1e 100644 --- a/gnovm/tests/files/import6.gno +++ b/gnovm/tests/files/import6.gno @@ -7,4 +7,4 @@ func main() { } // Error: -// github.com/gnolang/gno/_test/c2/c2.gno:1: import cycle detected: "github.com/gnolang/gno/_test/c1" (through [github.com/gnolang/gno/_test/c1 github.com/gnolang/gno/_test/c2]) +// github.com/gnolang/gno/_test/c2/c2.gno:3: import cycle detected: "github.com/gnolang/gno/_test/c1" (through [github.com/gnolang/gno/_test/c1 github.com/gnolang/gno/_test/c2]) diff --git a/gnovm/tests/files/var18.gno b/gnovm/tests/files/var18.gno new file mode 100644 index 00000000000..9fc0cba3053 --- /dev/null +++ b/gnovm/tests/files/var18.gno @@ -0,0 +1,8 @@ +package main + +func main() { + a, b, c := 1, 2 +} + +// Error: +// main/files/var18.gno:4: should not happen From eaab6e11e1eea0b39a2f84ac3dcad20afe7f4429 Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Wed, 29 May 2024 22:37:11 +0200 Subject: [PATCH 2/2] fix: set loc don't use line from group of declaration --- gnovm/pkg/gnolang/debugger_test.go | 2 +- gnovm/pkg/gnolang/go2gno.go | 14 ++++++-------- gnovm/tests/files/const9.gno | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/gnovm/pkg/gnolang/debugger_test.go b/gnovm/pkg/gnolang/debugger_test.go index db1bb14c138..5ac035d2f94 100644 --- a/gnovm/pkg/gnolang/debugger_test.go +++ b/gnovm/pkg/gnolang/debugger_test.go @@ -103,7 +103,7 @@ func TestDebug(t *testing.T) { {in: "p \"xxxx\"\n", out: `("xxxx" string)`}, {in: "si\n", out: "sample.gno:14"}, {in: "s\ns\n", out: `=> 14: var global = "test"`}, - {in: "s\n\n", out: "=> 16: type T struct {"}, + {in: "s\n\n", out: "=> 33: num := 5"}, {in: "foo", out: "command not available: foo"}, {in: "\n\n", out: "dbg> "}, {in: "#\n", out: "dbg> "}, diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index c8f8ee46ef7..4ae65211231 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -767,6 +767,7 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { Const: true, } cd.SetAttribute(ATTR_IOTA, si) + setLoc(fs, s.Pos(), cd) ds = append(ds, cd) } else { var names []NameExpr @@ -785,6 +786,7 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { Values: values, Const: false, } + setLoc(fs, s.Pos(), vd) ds = append(ds, vd) } case *ast.ImportSpec: @@ -792,10 +794,12 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { if err != nil { panic("unexpected import spec path type") } - ds = append(ds, &ImportDecl{ + im := &ImportDecl{ NameExpr: *Nx(toName(s.Name)), PkgPath: path, - }) + } + setLoc(fs, s.Pos(), im) + ds = append(ds, im) default: panic(fmt.Sprintf( "unexpected decl spec %v", @@ -803,12 +807,6 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { } } - if fs != nil { - for _, d := range ds { - setLoc(fs, gd.Pos(), d) - } - } - return ds } diff --git a/gnovm/tests/files/const9.gno b/gnovm/tests/files/const9.gno index a68b548d0c4..2dbfdca2da3 100644 --- a/gnovm/tests/files/const9.gno +++ b/gnovm/tests/files/const9.gno @@ -14,4 +14,4 @@ func main() { } // Error: -// main/files/const9.gno:3: constant definition loop with b +// main/files/const9.gno:5: constant definition loop with b