From 6deb77a77a102a8f313a1b9ec375d4d7c590bbb2 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 6 Jul 2022 18:04:25 +0300 Subject: [PATCH] compiler: make interface{}() conversions possible --- docs/compiler.md | 3 +++ pkg/compiler/codegen.go | 5 +++++ pkg/compiler/convert_test.go | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/docs/compiler.md b/docs/compiler.md index bd9375cdd..7d5810177 100644 --- a/docs/compiler.md +++ b/docs/compiler.md @@ -25,6 +25,9 @@ a dialect of Go rather than a complete port of the language: in variables and returning the result. * lambdas are supported, but closures are not. * maps are supported, but valid map keys are booleans, integers and strings with length <= 64 + * converting value to interface type doesn't change the underlying type, + original value will always be used, therefore it never panics and always "succeeds"; + it's up to the programmer whether it's a correct use of a value ## VM API (interop layer) Compiler translates interop function calls into NEO VM syscalls or (for custom diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 937e52bf4..5375feb64 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -944,6 +944,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { ast.Walk(c, n.Args[0]) c.emitConvert(stackitem.BufferT) return nil + case *ast.InterfaceType: + // It's a type conversion into some interface. Programmer is responsible + // for the conversion to be appropriate, just load the arg. + ast.Walk(c, n.Args[0]) + return nil case *ast.FuncLit: isLiteral = true } diff --git a/pkg/compiler/convert_test.go b/pkg/compiler/convert_test.go index fd3552afd..2793bfef8 100644 --- a/pkg/compiler/convert_test.go +++ b/pkg/compiler/convert_test.go @@ -147,3 +147,13 @@ func TestTypeConversionString(t *testing.T) { }` eval(t, src, []byte("lamao")) } + +func TestInterfaceTypeConversion(t *testing.T) { + src := `package foo + func Main() int { + a := 1 + b := interface{}(a).(int) + return b + }` + eval(t, src, big.NewInt(1)) +}