diff --git a/gnovm/pkg/gnolang/uverse_test.go b/gnovm/pkg/gnolang/uverse_test.go index 7a6c0567e45..e84f2ee66bf 100644 --- a/gnovm/pkg/gnolang/uverse_test.go +++ b/gnovm/pkg/gnolang/uverse_test.go @@ -4,14 +4,14 @@ import ( "testing" ) -type printlnTestCases struct { +type uverseTestCases struct { name string code string expected string } func TestIssue1337PrintNilSliceAsUndefined(t *testing.T) { - test := []printlnTestCases{ + test := []uverseTestCases{ { name: "print empty slice", code: `package test @@ -158,3 +158,54 @@ func TestIssue1337PrintNilSliceAsUndefined(t *testing.T) { }) } } + +func TestIssue2707PointerSliceAsParamInLen(t *testing.T) { + tests := []uverseTestCases{ + { + name: "pointer slice as param in len", + code: ` +package test + +func main() { + exp := [...]string{"HELLO"} + x := len(&exp) + println(x) +} + `, + expected: "1\n", + }, + { + name: "len of array", + code: ` +package test + +func main() { + exp := [...]string{"HELLO", "WORLD"} + println(len(exp)) +} + `, + expected: "2\n", + }, + { + name: "len of pointer to array", + code: ` +package test + +func main() { + exp := [...]int{1, 2, 3, 4, 5} + ptr := &exp + println(len(ptr)) +} + `, + expected: "5\n", + }, + } + + for _, tc := range tests { + m := NewMachine("test", nil) + n := MustParseFile("main.go", tc.code) + m.RunFiles(n) + m.RunMain() + assertOutput(t, tc.code, tc.expected) + } +} diff --git a/gnovm/pkg/gnolang/values.go b/gnovm/pkg/gnolang/values.go index 5da7c15bb05..0e2a378b855 100644 --- a/gnovm/pkg/gnolang/values.go +++ b/gnovm/pkg/gnolang/values.go @@ -2149,6 +2149,11 @@ func (tv *TypedValue) GetLength() int { return cv.GetLength() case *NativeValue: return cv.Value.Len() + case PointerValue: + if av, ok := cv.TV.V.(*ArrayValue); ok { + return av.GetLength() + } + panic(fmt.Sprintf("unexpected pointer value for len(): %s", tv.T.String())) default: panic(fmt.Sprintf("unexpected type for len(): %s", tv.T.String())) diff --git a/gnovm/tests/files/len1.gno b/gnovm/tests/files/len1.gno new file mode 100644 index 00000000000..f627fba190f --- /dev/null +++ b/gnovm/tests/files/len1.gno @@ -0,0 +1,10 @@ +package main + +func main() { + exp := [...]string{"HELLO"} + x := len(&exp) + println(x) +} + +// Output: +// 1