From 8af9c870b1a7acda9b1ba007f6d771ee81bbd409 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Mon, 26 Jul 2021 15:34:07 +0300 Subject: [PATCH] compiler: remove `CompileWithDebugInfo` It is a simple wrapper over `CompileWithOptions` which we don't really need. Custom options can be specified explicitly instead of using some random default. This default was introduced in 1578904da, however tests written there use `CompileWithOptions` and all other tests pass on that commit even without this default. Signed-off-by: Evgeniy Stratonikov --- internal/testchain/transaction.go | 2 +- pkg/compiler/compiler.go | 9 +-------- pkg/compiler/compiler_test.go | 12 ++++++------ pkg/compiler/debug_test.go | 8 ++++---- pkg/compiler/function_call_test.go | 2 +- pkg/compiler/global_test.go | 2 +- pkg/compiler/interop_test.go | 6 +++--- pkg/compiler/native_test.go | 2 +- pkg/compiler/syscall_test.go | 4 ++-- pkg/compiler/vm_test.go | 2 +- pkg/core/helper_test.go | 2 +- pkg/neotest/compile.go | 4 ++-- pkg/vm/cli/cli.go | 2 +- pkg/vm/cli/cli_test.go | 2 +- 14 files changed, 26 insertions(+), 33 deletions(-) diff --git a/internal/testchain/transaction.go b/internal/testchain/transaction.go index c4636f08d..1d0d8570d 100644 --- a/internal/testchain/transaction.go +++ b/internal/testchain/transaction.go @@ -55,7 +55,7 @@ func NewDeployTx(bc blockchainer.Blockchainer, name string, sender util.Uint160, // nef.NewFile() cares about version a lot. config.Version = "0.90.0-test" - ne, di, err := compiler.CompileWithDebugInfo(name, r) + ne, di, err := compiler.CompileWithOptions(name, r, nil) if err != nil { return nil, util.Uint160{}, nil, err } diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 988f26825..7e93f0c8b 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -160,7 +160,7 @@ func getBuildInfo(name string, src interface{}) (*buildInfo, error) { // If `r != nil`, `name` is interpreted as a filename, and `r` as file contents. // Otherwise `name` is either file name or name of the directory containing source files. func Compile(name string, r io.Reader) ([]byte, error) { - f, _, err := CompileWithDebugInfo(name, r) + f, _, err := CompileWithOptions(name, r, nil) if err != nil { return nil, err } @@ -168,13 +168,6 @@ func Compile(name string, r io.Reader) ([]byte, error) { return f.Script, nil } -// CompileWithDebugInfo compiles a Go program into bytecode and emits debug info. -func CompileWithDebugInfo(name string, r io.Reader) (*nef.File, *DebugInfo, error) { - return CompileWithOptions(name, r, &Options{ - NoEventsCheck: true, - }) -} - // CompileWithOptions compiles a Go program into bytecode with provided compiler options. func CompileWithOptions(name string, r io.Reader, o *Options) (*nef.File, *DebugInfo, error) { ctx, err := getBuildInfo(name, r) diff --git a/pkg/compiler/compiler_test.go b/pkg/compiler/compiler_test.go index 02b6acd99..47e9c2933 100644 --- a/pkg/compiler/compiler_test.go +++ b/pkg/compiler/compiler_test.go @@ -36,7 +36,7 @@ func TestCompiler(t *testing.T) { name: "TestCompileDirectory", function: func(t *testing.T) { const multiMainDir = "testdata/multi" - _, di, err := compiler.CompileWithDebugInfo(multiMainDir, nil) + _, di, err := compiler.CompileWithOptions(multiMainDir, nil, nil) require.NoError(t, err) m := map[string]bool{} for i := range di.Methods { @@ -93,7 +93,7 @@ func compileFile(src string) error { func TestOnPayableChecks(t *testing.T) { compileAndCheck := func(t *testing.T, src string) error { - _, di, err := compiler.CompileWithDebugInfo("payable", strings.NewReader(src)) + _, di, err := compiler.CompileWithOptions("payable", strings.NewReader(src), nil) require.NoError(t, err) _, err = compiler.CreateManifest(di, &compiler.Options{}) return err @@ -129,7 +129,7 @@ func TestSafeMethodWarnings(t *testing.T) { src := `package payable func Main() int { return 1 }` - _, di, err := compiler.CompileWithDebugInfo("eventTest", strings.NewReader(src)) + _, di, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src), nil) require.NoError(t, err) _, err = compiler.CreateManifest(di, &compiler.Options{SafeMethods: []string{"main"}}) @@ -144,7 +144,7 @@ func TestEventWarnings(t *testing.T) { import "github.com/nspcc-dev/neo-go/pkg/interop/runtime" func Main() { runtime.Notify("Event", 1) }` - _, di, err := compiler.CompileWithDebugInfo("eventTest", strings.NewReader(src)) + _, di, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src), nil) require.NoError(t, err) t.Run("event it missing from config", func(t *testing.T) { @@ -188,7 +188,7 @@ func TestEventWarnings(t *testing.T) { return notify.Value }` - _, di, err := compiler.CompileWithDebugInfo("eventTest", strings.NewReader(src)) + _, di, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src), nil) require.NoError(t, err) _, err = compiler.CreateManifest(di, &compiler.Options{NoEventsCheck: true}) @@ -202,7 +202,7 @@ func TestEventWarnings(t *testing.T) { return 42 }` - _, di, err := compiler.CompileWithDebugInfo("eventTest", strings.NewReader(src)) + _, di, err := compiler.CompileWithOptions("eventTest", strings.NewReader(src), nil) require.NoError(t, err) _, err = compiler.CreateManifest(di, &compiler.Options{}) diff --git a/pkg/compiler/debug_test.go b/pkg/compiler/debug_test.go index c915b92af..4d0994cf4 100644 --- a/pkg/compiler/debug_test.go +++ b/pkg/compiler/debug_test.go @@ -67,7 +67,7 @@ func (ms *MyStruct) MethodOnPointerToStruct() { } func _deploy(data interface{}, isUpdate bool) { x := 1; _ = x } ` - buf, d, err := CompileWithOptions("foo.go", strings.NewReader(src), nil) + ne, d, err := CompileWithOptions("foo.go", strings.NewReader(src), nil) require.NoError(t, err) require.NotNil(t, d) @@ -162,8 +162,8 @@ func _deploy(data interface{}, isUpdate bool) { x := 1; _ = x } // basic check that last instruction of every method is indeed RET for i := range d.Methods { index := d.Methods[i].Range.End - require.True(t, int(index) < len(buf)) - require.EqualValues(t, opcode.RET, buf[index]) + require.True(t, int(index) < len(ne.Script)) + require.EqualValues(t, opcode.RET, ne.Script[index]) } t.Run("convert to Manifest", func(t *testing.T) { @@ -365,7 +365,7 @@ func TestManifestOverload(t *testing.T) { return 4 }` - _, di, err := CompileWithDebugInfo("foo", strings.NewReader(src)) + _, di, err := CompileWithOptions("foo", strings.NewReader(src), nil) require.NoError(t, err) m, err := di.ConvertToManifest(&Options{Overloads: map[string]string{"add3Aux": "add3"}}) diff --git a/pkg/compiler/function_call_test.go b/pkg/compiler/function_call_test.go index 6fed34dfd..c017fa542 100644 --- a/pkg/compiler/function_call_test.go +++ b/pkg/compiler/function_call_test.go @@ -302,7 +302,7 @@ func TestJumpOptimize(t *testing.T) { func Main() int { return Get3() }` - b, di, err := compiler.CompileWithDebugInfo("", strings.NewReader(src)) + b, di, err := compiler.CompileWithOptions("", strings.NewReader(src), nil) require.NoError(t, err) require.Equal(t, 6, len(di.Methods)) for _, mi := range di.Methods { diff --git a/pkg/compiler/global_test.go b/pkg/compiler/global_test.go index d0176c7ec..c4a444932 100644 --- a/pkg/compiler/global_test.go +++ b/pkg/compiler/global_test.go @@ -131,7 +131,7 @@ func TestContractWithNoMain(t *testing.T) { someLocal := 2 return someGlobal + someLocal + a }` - b, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(src)) + b, di, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) require.NoError(t, err) v := vm.New() invokeMethod(t, "Add3", b.Script, v, di) diff --git a/pkg/compiler/interop_test.go b/pkg/compiler/interop_test.go index f2d2447d7..9ef16964b 100644 --- a/pkg/compiler/interop_test.go +++ b/pkg/compiler/interop_test.go @@ -128,7 +128,7 @@ func TestAbort(t *testing.T) { } func spawnVM(t *testing.T, ic *interop.Context, src string) *vm.VM { - b, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(src)) + b, di, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) require.NoError(t, err) v := core.SpawnVM(ic) invokeMethod(t, testMainIdent, b.Script, v, di) @@ -141,7 +141,7 @@ func TestAppCall(t *testing.T) { func Get42() int { return 42 }` - barCtr, di, err := compiler.CompileWithDebugInfo("bar.go", strings.NewReader(srcDeep)) + barCtr, di, err := compiler.CompileWithOptions("bar.go", strings.NewReader(srcDeep), nil) require.NoError(t, err) mBar, err := di.ConvertToManifest(&compiler.Options{Name: "Bar"}) require.NoError(t, err) @@ -167,7 +167,7 @@ func TestAppCall(t *testing.T) { srcInner = fmt.Sprintf(srcInner, fmt.Sprintf("%#v", cinterop.Hash160(barH.BytesBE()))) - inner, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(srcInner)) + inner, di, err := compiler.CompileWithOptions("foo.go", strings.NewReader(srcInner), nil) require.NoError(t, err) m, err := di.ConvertToManifest(&compiler.Options{ Name: "Foo", diff --git a/pkg/compiler/native_test.go b/pkg/compiler/native_test.go index ecb9317ba..9aa5efa45 100644 --- a/pkg/compiler/native_test.go +++ b/pkg/compiler/native_test.go @@ -251,7 +251,7 @@ func runNativeTestCase(t *testing.T, ctr interop.ContractMD, name, method string v := vm.New() v.GasLimit = -1 - b, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(src)) + b, di, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) require.NoError(t, err) result := getTestStackItem(md.MD.ReturnType) diff --git a/pkg/compiler/syscall_test.go b/pkg/compiler/syscall_test.go index f2b71821f..e553e01c4 100644 --- a/pkg/compiler/syscall_test.go +++ b/pkg/compiler/syscall_test.go @@ -139,7 +139,7 @@ func runSyscallTestCase(t *testing.T, ic *interop.Context, goName string, tc sys } ss := strings.Split(goName, ".") src := fmt.Sprintf(srcTmpl, ss[0], goName, strings.Join(tc.params, ", ")) - b, _, err := compiler.CompileWithDebugInfo("foo", strings.NewReader(src)) + b, _, err := compiler.CompileWithOptions("foo", strings.NewReader(src), nil) require.NoError(t, err) v := ic.SpawnVM() @@ -198,7 +198,7 @@ func TestNotify(t *testing.T) { runtime.Notify("long event12345678901234567890123") }` - _, _, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(src)) + _, _, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) require.Error(t, err) }) } diff --git a/pkg/compiler/vm_test.go b/pkg/compiler/vm_test.go index 174ac5963..11f9f299f 100644 --- a/pkg/compiler/vm_test.go +++ b/pkg/compiler/vm_test.go @@ -71,7 +71,7 @@ func vmAndCompileInterop(t *testing.T, src string) (*vm.VM, *storagePlugin) { vm.GasLimit = -1 vm.SyscallHandler = storePlugin.syscallHandler - b, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(src)) + b, di, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) require.NoError(t, err) storePlugin.info = di diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 146860c51..633bd6eb3 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -187,7 +187,7 @@ func TestBug1728(t *testing.T) { func _deploy(_ interface{}, isUpdate bool) { runtime.Log("Deploy") }` - nf, di, err := compiler.CompileWithDebugInfo("foo", strings.NewReader(src)) + nf, di, err := compiler.CompileWithOptions("foo", strings.NewReader(src), nil) require.NoError(t, err) m, err := di.ConvertToManifest(&compiler.Options{Name: "TestContract"}) require.NoError(t, err) diff --git a/pkg/neotest/compile.go b/pkg/neotest/compile.go index cca0c8c41..8a81514d0 100644 --- a/pkg/neotest/compile.go +++ b/pkg/neotest/compile.go @@ -29,7 +29,7 @@ func CompileSource(t *testing.T, sender util.Uint160, src io.Reader, opts *compi // nef.NewFile() cares about version a lot. config.Version = "neotest" - ne, di, err := compiler.CompileWithDebugInfo(opts.Name, src) + ne, di, err := compiler.CompileWithOptions(opts.Name, src, opts) require.NoError(t, err) m, err := compiler.CreateManifest(di, opts) @@ -51,7 +51,7 @@ func CompileFile(t *testing.T, sender util.Uint160, srcPath string, configPath s // nef.NewFile() cares about version a lot. config.Version = "neotest" - ne, di, err := compiler.CompileWithDebugInfo(srcPath, nil) + ne, di, err := compiler.CompileWithOptions(srcPath, nil, nil) require.NoError(t, err) conf, err := smartcontract.ParseContractConfig(configPath) diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index fb4450090..ba2186a74 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -394,7 +394,7 @@ func handleLoadGo(c *ishell.Context) { c.Err(fmt.Errorf("%w: ", ErrMissingParameter)) return } - b, di, err := compiler.CompileWithDebugInfo(c.Args[0], nil) + b, di, err := compiler.CompileWithOptions(c.Args[0], nil, nil) if err != nil { c.Err(err) return diff --git a/pkg/vm/cli/cli_test.go b/pkg/vm/cli/cli_test.go index 435d647bb..3dcb3afd2 100644 --- a/pkg/vm/cli/cli_test.go +++ b/pkg/vm/cli/cli_test.go @@ -243,7 +243,7 @@ func TestLoad(t *testing.T) { t.Run("loadnef", func(t *testing.T) { config.Version = "0.92.0-test" - nefFile, di, err := compiler.CompileWithDebugInfo("test", strings.NewReader(src)) + nefFile, di, err := compiler.CompileWithOptions("test", strings.NewReader(src), nil) require.NoError(t, err) filename := filepath.Join(tmpDir, "vmtestcontract.nef") rawNef, err := nefFile.Bytes()